Skip to content

Commit

Permalink
Merge pull request #4779 from jkenda/master
Browse files Browse the repository at this point in the history
encoding/json: marshal enumerated arrays to objects with key-value pairs
  • Loading branch information
gingerBill authored Feb 8, 2025
2 parents a347af4 + 51b80c5 commit 4ea96bd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
16 changes: 13 additions & 3 deletions core/encoding/json/marshal.odin
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,23 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
opt_write_end(w, opt, ']') or_return

case runtime.Type_Info_Enumerated_Array:
opt_write_start(w, opt, '[') or_return
index_type := reflect.type_info_base(info.index)
enum_type := index_type.variant.(reflect.Type_Info_Enum)

opt_write_start(w, opt, '{') or_return
for i in 0..<info.count {
value := cast(runtime.Type_Info_Enum_Value)i
index, found := slice.linear_search(enum_type.values, value)
if !found {
continue
}

opt_write_iteration(w, opt, i == 0) or_return
opt_write_key(w, opt, enum_type.names[index]) or_return
data := uintptr(v.data) + uintptr(i*info.elem_size)
marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
}
opt_write_end(w, opt, ']') or_return
opt_write_end(w, opt, '}') or_return

case runtime.Type_Info_Dynamic_Array:
opt_write_start(w, opt, '[') or_return
Expand Down Expand Up @@ -667,4 +677,4 @@ cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
}

return u
}
}
48 changes: 47 additions & 1 deletion tests/core/encoding/json/test_core_json.odin
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,50 @@ map_with_integer_keys :: proc(t: ^testing.T) {
testing.expectf(t, runtime.string_eq(item, my_map2[key]), "Expected value %s to be present in unmarshaled map", key)
}
}
}
}

@test
enumerated_array :: proc(t: ^testing.T) {
Fruit :: enum { Apple, Banana, Pear }
Fruit_Stock :: [Fruit]uint {
.Apple = 14,
.Banana = 3,
.Pear = 513,
}

{ // test unmarshaling from array
marshaled := "[14,3,513]"

unmarshaled: [Fruit]uint
err := json.unmarshal_string(marshaled, &unmarshaled)
testing.expect_value(t, err, nil)
testing.expect_value(t, unmarshaled, Fruit_Stock)
}

Sparse_Fruit :: enum { Apple, Banana, Cherry = 23, Pear }
Sparse_Fruit_Stock :: #partial #sparse [Sparse_Fruit]uint {
.Apple = 14,
.Banana = 3,
.Pear = 513,
}

{ // test unmarshaling from object
marshaled := `{"Apple":14,"Banana":3,"Cherry":0,"Pear":513}`

unmarshaled: #sparse [Sparse_Fruit]uint
err := json.unmarshal_string(marshaled, &unmarshaled)
testing.expect_value(t, err, nil)
testing.expect_value(t, unmarshaled, Sparse_Fruit_Stock)
}

{ // test marshal -> unmarshal
marshaled, err_marshal := json.marshal(Sparse_Fruit_Stock)
defer delete(marshaled)
testing.expect_value(t, err_marshal, nil)

unmarshaled: #sparse [Sparse_Fruit]uint
err_unmarshal := json.unmarshal(marshaled, &unmarshaled)
testing.expect_value(t, err_unmarshal, nil)
testing.expect_value(t, unmarshaled, Sparse_Fruit_Stock)
}
}

0 comments on commit 4ea96bd

Please sign in to comment.