You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
j := jsondiff.DefaultJSONOptions()
a := `{"foo":null}`
b := `{"foo":["bar"]}`
_, diff := jsondiff.Compare([]byte(a), []byte(b), &j)
fmt.Println("comparing null to array is broken")
fmt.Println(diff)
a = `{"foo":null}`
b = `{"foo":{"value": "bar"}}`
_, diff = jsondiff.Compare([]byte(a), []byte(b), &j)
fmt.Println("comparing null to object is broken")
fmt.Println(diff)
a = `{"foo":null}`
b = `{"foo":"bar"}`
_, diff = jsondiff.Compare([]byte(a), []byte(b), &j)
fmt.Println("comparing null to a non-nested value works!")
fmt.Println(diff)
comparing null to array is broken
{
"foo": {"changed":[null, []]}
}
comparing null to object is broken
{
"foo": {"changed":[null, {}]}
}
comparing null to a non-nested value works!
{
"foo": {"changed":[null, "bar"]}
}
If a field has a json 'null' value in either of the JSON objects, and the other value is a nested value (array or object), the comparison fails to print out the correct value for the other object field's non-null value. See the playground example above for an illustration.
One way to hack a fix is convert all nil slice values to empty slice values in the struct before serializing to json.
On second glance, it looks like the more general issue is with comparing any JSON values of different type: https://go.dev/play/p/fo0aUPTuAB8.
It would be nice if in this case, we emitted the string-fied value of the types, instead of the zero type.
The text was updated successfully, but these errors were encountered:
Yes, I agree with that this behaviour may not be ideal. But that's how it is. To be honest this lib is a very low priority for me and it's unlikely that I'm going to do anything about it anytime soon.
I can see you're using JSON config. This lib was never meant to provide a machine readable output. I don't even recall who contributed JSON config option and I never used it myself and not planning to. When printing out something like "null <= {}" the lib means that values are of different type and further comparison makes no sense. Same applies to arrays vs other values. The value is different already, thus no need to go deeper. That's what I had in mind when implementing it.
Maybe one day will do something about it (unlikely soon).
Toy code:
Run in playground: https://go.dev/play/p/XGfPuf_Px8a
If a field has a json 'null' value in either of the JSON objects, and the other value is a nested value (array or object), the comparison fails to print out the correct value for the other object field's non-null value. See the playground example above for an illustration.
One way to hack a fix is convert all nil slice values to empty slice values in the struct before serializing to json.
On second glance, it looks like the more general issue is with comparing any JSON values of different type: https://go.dev/play/p/fo0aUPTuAB8.
It would be nice if in this case, we emitted the string-fied value of the types, instead of the zero type.
The text was updated successfully, but these errors were encountered: