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

JSON package doesn't roundtrip null values #4895

Open
spahnke opened this issue Mar 2, 2025 · 1 comment · May be fixed by #4896
Open

JSON package doesn't roundtrip null values #4895

spahnke opened this issue Mar 2, 2025 · 1 comment · May be fixed by #4896

Comments

@spahnke
Copy link
Contributor

spahnke commented Mar 2, 2025

Context

Odin:    dev-2025-02-nightly:584fdc0
OS:      macOS Sequoia 15.3.1 (build 24D70, kernel 24.3.0)
CPU:     Apple M4
RAM:     24576 MiB
Backend: LLVM 18.1.8

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

case runtime.Type_Info_Pointer:

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)
	}
}
@cstrachan88 cstrachan88 linked a pull request Mar 2, 2025 that will close this issue
@cstrachan88
Copy link
Contributor

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.

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

Successfully merging a pull request may close this issue.

2 participants