mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Merge pull request #4779 from jkenda/master
encoding/json: marshal enumerated arrays to objects with key-value pairs
This commit is contained in:
@@ -209,13 +209,23 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
|
|||||||
opt_write_end(w, opt, ']') or_return
|
opt_write_end(w, opt, ']') or_return
|
||||||
|
|
||||||
case runtime.Type_Info_Enumerated_Array:
|
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 {
|
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_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)
|
data := uintptr(v.data) + uintptr(i*info.elem_size)
|
||||||
marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
|
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:
|
case runtime.Type_Info_Dynamic_Array:
|
||||||
opt_write_start(w, opt, '[') or_return
|
opt_write_start(w, opt, '[') or_return
|
||||||
@@ -667,4 +677,4 @@ cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user