Skip to content

json.RawMessage backwards compatibility #524

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

djeebus
Copy link

@djeebus djeebus commented Dec 30, 2020

We ran into this issue at work. I started digging into it, and found that someone had already fixed it in #474, but hadn't added any tests. Hopefully this gets it over the finish line!

greensea and others added 3 commits July 14, 2020 20:43
In encoding/json, there is MarshalJSON() and UnmarshaslJSON() for RawMessage. Missing this two method leads to different behavior while Marshal() jsoniter.RawMessage using std json package.

Example code below
```
package main
import (
    "fmt"
    "encoding/json"
    "github.com/json-iterator/go"
)

type T struct {
    Msg jsoniter.RawMessage
}

func main() {
    var j T
    
    raw := []byte(`{ "Msg": {"Foo": "Bar"}}`)
    
    jsoniter.Unmarshal(raw, &j)
        
    b1, _ := json.Marshal(j.Msg)
    fmt.Printf("%s\n", b1)                         /// Expect `{"Foo": "Bar"}` but got `"IHsiRm9vIjogIkJhciJ9"`
    
    b2, _ := jsoniter.Marshal(j.Msg)
    fmt.Printf("%s\n", b2)
}
```
@djeebus djeebus changed the title json.RawMessage compatibility Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants