aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2024-09-04 13:27:25 +0100
committerGitHub <noreply@github.com>2024-09-04 13:27:25 +0100
commitb2c5998e78599db2e2ee20db508b96fe2d5acb10 (patch)
tree00027c1173eec53eb4575e71474b911780f17a93
parentc6b551d2c3b104b3bb7e81fdec42885685ad7754 (diff)
parent7487d507be57a467e48e8e3464bae4b579086967 (diff)
Merge pull request #4196 from jakubtomsu/unmarshal-json-bit-sets
Support unmarshalling bit_sets in `core:encoding/json`
-rw-r--r--core/encoding/json/unmarshal.odin25
1 files changed, 24 insertions, 1 deletions
diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin
index 127bce650..738e20c68 100644
--- a/core/encoding/json/unmarshal.odin
+++ b/core/encoding/json/unmarshal.odin
@@ -116,7 +116,30 @@ assign_int :: proc(val: any, i: $T) -> bool {
case int: dst = int (i)
case uint: dst = uint (i)
case uintptr: dst = uintptr(i)
- case: return false
+ case:
+ ti := type_info_of(v.id)
+ if _, ok := ti.variant.(runtime.Type_Info_Bit_Set); ok {
+ do_byte_swap := !reflect.bit_set_is_big_endian(v)
+ switch ti.size * 8 {
+ case 0: // no-op.
+ case 8:
+ x := (^u8)(v.data)
+ x^ = u8(i)
+ case 16:
+ x := (^u16)(v.data)
+ x^ = do_byte_swap ? intrinsics.byte_swap(u16(i)) : u16(i)
+ case 32:
+ x := (^u32)(v.data)
+ x^ = do_byte_swap ? intrinsics.byte_swap(u32(i)) : u32(i)
+ case 64:
+ x := (^u64)(v.data)
+ x^ = do_byte_swap ? intrinsics.byte_swap(u64(i)) : u64(i)
+ case:
+ panic("unknown bit_size size")
+ }
+ return true
+ }
+ return false
}
return true
}