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

Add json.unparse: RTTI-less conversion from json.Value to []u8 #4628

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

Conversation

bullyingteen
Copy link

@bullyingteen bullyingteen commented Dec 25, 2024

This is a follow up PR to the smalltalk we had in Discord/General/Beginners.

Introduction

While trying out odin's encoding/json module I've found out that the only way to convert json.Value to []u8/string was to use json.marshal (docs lack that information).
Which feels very unnatural and misleading to me as a C/C++ developer. So I did implement json.unparse. It probably has a tiny bit of performance boost due to it being RTTI-less.

Added test case for json.unparse, tested it on my windows 11.

Named it json.unparse for consistency with json.parse, but naming suggestions are wellcome.

Bonus

Fixed an Unsupported_Type error in json.marshal on (rawptr)nil values.

P.S.

json.marshal uses Raw_Map routines when converting json.Object, but it requires runtime.Type_Info for accessing Map_Info hence I used simple k/v for-loop over map. Let me know if traversing Raw_Map buckets is preferred for some reason. I might implement it that way instead.

Copy link
Member

@gingerBill gingerBill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are loads of styling issues.

Comment on lines 37 to 43
case Null: return unparse_null_to_writer(w, opt)
case Integer: return unparse_integer_to_writer(w, uv, opt)
case Float: return unparse_float_to_writer(w, uv, opt)
case Boolean: return unparse_boolean_to_writer(w, uv, opt)
case String: return unparse_string_to_writer(w, uv, opt)
case Array: return unparse_array_to_writer(w, uv, opt)
case Object: return unparse_object_to_writer(w, uv, opt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not consistent with the Odin core style.


unparse_array_to_writer :: proc(w: io.Writer, v: Array, opt: ^Marshal_Options) -> io.Error {
opt_write_start(w, opt, '[') or_return
for i in 0..<len(v) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for e, i in v {

Comment on lines 97 to 98
}
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {

@karl-zylinski
Copy link
Contributor

BTW, some of the styling issues Bill pointed out can be caught by compiler flag -strict-style

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 this pull request may close these issues.

3 participants