-
-
Notifications
You must be signed in to change notification settings - Fork 704
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
added always_omitempty and always_omitnil flags to encoding_json.Marshal_Options #4857
Closed
+42
−32
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b65f90
added always_omitempty and always_omitnil flags to encoding_json.Mars…
jkenda 2ca4241
use reflect.is_nil and reflect.length for omitnil and omitempty
jkenda 48e33b0
marshal_struct_fields: fixed checking the wrong value
jkenda a716862
check type before calling reflect.length and reflect.is_nil
jkenda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,15 @@ Marshal_Options :: struct { | |
// NOTE: If a name isn't found it'll use the underlying value. | ||
use_enum_names: bool, | ||
|
||
// Always omit empty values including empty (c)strings, (dynamic) arrays, | ||
// slices, unions, bit sets, maps, and nil values. | ||
always_omitempty: bool, | ||
|
||
// Always omit nil values. | ||
// | ||
// Doesn't omit empty values, unlike always_omitempty. | ||
always_omitnil: bool, | ||
|
||
// Internal state | ||
indentation: int, | ||
mjson_skipped_first_braces_start: bool, | ||
|
@@ -345,6 +354,27 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: | |
|
||
case runtime.Type_Info_Struct: | ||
is_omitempty :: proc(v: any) -> bool { | ||
v := v | ||
ti := runtime.type_info_core(type_info_of(v.id)) | ||
#partial switch info in ti.variant { | ||
case runtime.Type_Info_String: | ||
switch x in v { | ||
case string: | ||
return x == "" | ||
case cstring: | ||
return x == "" | ||
} | ||
case runtime.Type_Info_Dynamic_Array: | ||
return (^runtime.Raw_Dynamic_Array)(v.data).len == 0 | ||
case runtime.Type_Info_Slice: | ||
return (^runtime.Raw_Slice)(v.data).len == 0 | ||
case runtime.Type_Info_Map: | ||
return (^runtime.Raw_Map)(v.data).len == 0 | ||
} | ||
return false | ||
} | ||
|
||
is_omitnil :: proc(v: any) -> bool { | ||
v := v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
if v == nil { | ||
return true | ||
|
@@ -353,57 +383,57 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: | |
#partial switch info in ti.variant { | ||
case runtime.Type_Info_String: | ||
switch x in v { | ||
case string: | ||
return x == "" | ||
case cstring: | ||
return x == nil || x == "" | ||
return x == nil | ||
} | ||
case runtime.Type_Info_Any: | ||
return v.(any) == nil | ||
case runtime.Type_Info_Type_Id: | ||
return v.(typeid) == nil | ||
case runtime.Type_Info_Pointer, | ||
runtime.Type_Info_Multi_Pointer, | ||
runtime.Type_Info_Procedure: | ||
runtime.Type_Info_Multi_Pointer, | ||
runtime.Type_Info_Procedure: | ||
return (^rawptr)(v.data)^ == nil | ||
case runtime.Type_Info_Dynamic_Array: | ||
return (^runtime.Raw_Dynamic_Array)(v.data).len == 0 | ||
case runtime.Type_Info_Slice: | ||
return (^runtime.Raw_Slice)(v.data).len == 0 | ||
case runtime.Type_Info_Union, | ||
runtime.Type_Info_Bit_Set, | ||
runtime.Type_Info_Soa_Pointer: | ||
runtime.Type_Info_Bit_Set, | ||
runtime.Type_Info_Soa_Pointer: | ||
return reflect.is_nil(v) | ||
case runtime.Type_Info_Map: | ||
return (^runtime.Raw_Map)(v.data).len == 0 | ||
} | ||
return false | ||
} | ||
|
||
|
||
marshal_struct_fields :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: Marshal_Error) { | ||
ti := runtime.type_info_base(type_info_of(v.id)) | ||
info := ti.variant.(runtime.Type_Info_Struct) | ||
first_iteration := true | ||
for name, i in info.names[:info.field_count] { | ||
omitempty := false | ||
omitempty := opt.always_omitempty | ||
omitnil := opt.always_omitnil || omitempty | ||
|
||
json_name, extra := json_name_from_tag_value(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json")) | ||
|
||
if json_name == "-" { | ||
continue | ||
} | ||
|
||
id := info.types[i].id | ||
data := rawptr(uintptr(v.data) + info.offsets[i]) | ||
the_value := any{data, id} | ||
|
||
for flag in strings.split_iterator(&extra, ",") { | ||
switch flag { | ||
case "omitempty": | ||
omitempty = true | ||
fallthrough | ||
case "omitnil": | ||
omitnil = true | ||
} | ||
} | ||
|
||
id := info.types[i].id | ||
data := rawptr(uintptr(v.data) + info.offsets[i]) | ||
the_value := any{data, id} | ||
|
||
if omitnil && is_omitnil(the_value) { | ||
continue | ||
} | ||
if omitempty && is_omitempty(the_value) { | ||
continue | ||
} | ||
|
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.
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.
Use
reflect.length
instead.