go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incompatible with standard `encoding/json` regular expression marshaling

rwxrob opened this issue · comments

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`))
}