Add json.unparse: RTTI-less conversion from json.Value to []u8 #4628
+187
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.