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

added always_omitempty and always_omitnil flags to encoding_json.Marshal_Options #4857

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 42 additions & 32 deletions core/encoding/json/marshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -345,65 +354,66 @@ 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
if v == nil {
return true
ti := runtime.type_info_core(type_info_of(v.id))
#partial switch info in ti.variant {
case runtime.Type_Info_String,
runtime.Type_Info_Dynamic_Array,
runtime.Type_Info_Slice,
runtime.Type_Info_Map:
return reflect.length(v) == 0
}
return false
}

is_omitnil :: proc(v: any) -> bool {
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 == nil || x == ""
}
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:
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:
case runtime.Type_Info_String,
runtime.Type_Info_Any,
runtime.Type_Info_Type_Id,
runtime.Type_Info_Pointer,
runtime.Type_Info_Multi_Pointer,
runtime.Type_Info_Procedure,
runtime.Type_Info_Union,
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
}
Expand Down