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
I needed a small tool to combine several JSON files into one that I wanted to try Odin for, but got met with a marshaling error "Unsupported type". This is because the original files contain objects with fields of value null that need to be roundtripped (see code example below). I suspect this is because the parsed value is a json.Null union variant, especially because using nil works, but directly using json.Null {} does not. The unsupported type error gets raised here
Let me know should there be a setting I haven't found.
Expected Behavior
null values can be roundtripped when first parsed and then marshaled.
Current Behavior
Unsupported type error.
Example
The original roundtripping code I wanted to write is in the last example block. The first 2 showcase the error when manually building a JSON object where nil works but json.Null {} doesn't.
package main
import"core:fmt"import"core:encoding/json"
main :: proc() {
// works
{
parsed: json.Object
parsed["id"] = 234
parsed["title"] = "asdf"
parsed["runtime"] = 56.9
parsed["creator"] = nil
bytes, err := json.marshal(parsed)
assert(err == nil)
str := cast(string)bytes
fmt.println(str)
}
// does not work
{
parsed: json.Object
parsed["id"] = 234
parsed["title"] = "asdf"
parsed["runtime"] = 56.9
parsed["creator"] = json.Null {} // this is what you get when parsing null
bytes, err := json.marshal(parsed)
assert(err == nil)
str := cast(string)bytes
fmt.println(str)
}
// does not work - original roundtripping code I wanted to write
{
content := `{ "id": 234, "title": "asdf", "runtime": 56.9, "creator": null}`
parsed, _ := json.parse(transmute([]u8)content)
bytes, err := json.marshal(parsed)
assert(err == nil)
str := cast(string)bytes
fmt.println(str)
}
}
The text was updated successfully, but these errors were encountered:
Null values are being parsed in as json.Null which is a distinct rawptr. I updated the json.marshal_to_writer proc to recognize null pointers. Your example is now working as expected.
Context
I needed a small tool to combine several JSON files into one that I wanted to try Odin for, but got met with a marshaling error "Unsupported type". This is because the original files contain objects with fields of value
null
that need to be roundtripped (see code example below). I suspect this is because the parsed value is ajson.Null
union variant, especially because usingnil
works, but directly usingjson.Null {}
does not. The unsupported type error gets raised hereOdin/core/encoding/json/marshal.odin
Line 178 in f390598
Let me know should there be a setting I haven't found.
Expected Behavior
null
values can be roundtripped when first parsed and then marshaled.Current Behavior
Unsupported type error.
Example
The original roundtripping code I wanted to write is in the last example block. The first 2 showcase the error when manually building a JSON object where
nil
works butjson.Null {}
doesn't.The text was updated successfully, but these errors were encountered: