Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible with standard encoding/json regular expression marshaling #1007

Open
rwxrob opened this issue Nov 29, 2023 · 1 comment
Open

Comments

@rwxrob
Copy link

rwxrob commented Nov 29, 2023

See this comment #269 (comment)

package main

import (
  "fmt"
  "log"
  "regexp"

  "gopkg.in/yaml.v3"
)

type Foo struct {
  Thing string
  Regex *regexp.Regexp
}

func main() {

  /*

    f := Foo{Regex: regexp.MustCompile(`\s+some`)}
    byt, err := json.Marshal(f)
    if err != nil {
      log.Print(err)
      return
    }
    fmt.Println(string(byt))

    f2 := new(Foo)
    err = json.Unmarshal([]byte(`{"Regex": "\\s+some"}`), f2)
    if err != nil {
      log.Print(err)
      return
    }
    fmt.Println(f2.Regex.MatchString(`    some`))
  */

  f3 := new(Foo)
  err := yaml.Unmarshal([]byte("Regex: '\\s+some'\n"), f3)
  fmt.Println(f3)
  if err != nil {
    log.Print(err)
    return
  }
  fmt.Println(f3.Regex.MatchString(`    some`))
}
@dolmen
Copy link

dolmen commented Mar 12, 2024

On the Go Playground: https://go.dev/play/p/Tgl4GSNjN8O

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants