-
-
Notifications
You must be signed in to change notification settings - Fork 664
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
core/encoding/json/unparse.odin
Outdated
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) |
There was a problem hiding this comment.
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.
core/encoding/json/unparse.odin
Outdated
|
||
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) { |
There was a problem hiding this comment.
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 {
core/encoding/json/unparse.odin
Outdated
} | ||
else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else {
BTW, some of the styling issues Bill pointed out can be caught by compiler flag |
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.