aboutsummaryrefslogtreecommitdiff
path: root/core/encoding
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-09-29 15:02:51 +0100
committergingerBill <bill@gingerbill.org>2021-09-29 15:02:51 +0100
commit9d797ea225b272a64f52eeb1fd7462827eba9654 (patch)
tree35c6a83c0b1ed371a6b2e168fdc915d89adf7cc2 /core/encoding
parentaac290e36697399a95f2ff46e29789858b9342cd (diff)
Add more support for complex types
Diffstat (limited to 'core/encoding')
-rw-r--r--core/encoding/json/marshal.odin21
-rw-r--r--core/encoding/json/unmarshal.odin29
2 files changed, 29 insertions, 21 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin
index 66506ef51..ef78c0a19 100644
--- a/core/encoding/json/marshal.odin
+++ b/core/encoding/json/marshal.odin
@@ -109,14 +109,12 @@ marshal_to_writer :: proc(w: io.Writer, v: any) -> (err: Marshal_Error) {
io.write_byte(w, '"') or_return
case runtime.Type_Info_Float:
- val: f64
switch f in a {
- case f16: val = f64(f)
- case f32: val = f64(f)
- case f64: val = f64(f)
+ case f16: io.write_f16(w, f) or_return
+ case f32: io.write_f32(w, f) or_return
+ case f64: io.write_f64(w, f) or_return
+ case: return .Unsupported_Type
}
-
- write_f64(w, val, ti.size)
case runtime.Type_Info_Complex:
r, i: f64
@@ -124,13 +122,14 @@ marshal_to_writer :: proc(w: io.Writer, v: any) -> (err: Marshal_Error) {
case complex32: r, i = f64(real(z)), f64(imag(z))
case complex64: r, i = f64(real(z)), f64(imag(z))
case complex128: r, i = f64(real(z)), f64(imag(z))
+ case: return .Unsupported_Type
}
- io.write_byte(w, '[') or_return
- write_f64(w, r, ti.size/2) or_return
- io.write_string(w, ", ") or_return
- write_f64(w, i, ti.size/2) or_return
- io.write_byte(w, ']') or_return
+ io.write_byte(w, '[') or_return
+ io.write_f64(w, r) or_return
+ io.write_string(w, ", ") or_return
+ io.write_f64(w, i) or_return
+ io.write_byte(w, ']') or_return
case runtime.Type_Info_Quaternion:
return .Unsupported_Type
diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin
index 6eb1d54f5..5094f2f67 100644
--- a/core/encoding/json/unmarshal.odin
+++ b/core/encoding/json/unmarshal.odin
@@ -120,18 +120,27 @@ assign_int :: proc(val: any, i: $T) -> bool {
return true
}
@(private)
-assign_float :: proc(val: any, i: $T) -> bool {
+assign_float :: proc(val: any, f: $T) -> bool {
v := reflect.any_core(val)
switch dst in &v {
- case f16: dst = f16 (i)
- case f16le: dst = f16le(i)
- case f16be: dst = f16be(i)
- case f32: dst = f32 (i)
- case f32le: dst = f32le(i)
- case f32be: dst = f32be(i)
- case f64: dst = f64 (i)
- case f64le: dst = f64le(i)
- case f64be: dst = f64be(i)
+ case f16: dst = f16 (f)
+ case f16le: dst = f16le(f)
+ case f16be: dst = f16be(f)
+ case f32: dst = f32 (f)
+ case f32le: dst = f32le(f)
+ case f32be: dst = f32be(f)
+ case f64: dst = f64 (f)
+ case f64le: dst = f64le(f)
+ case f64be: dst = f64be(f)
+
+ case complex32: dst = complex(f16(f), 0)
+ case complex64: dst = complex(f32(f), 0)
+ case complex128: dst = complex(f64(f), 0)
+
+ case quaternion64: dst = quaternion(f16(f), 0, 0, 0)
+ case quaternion128: dst = quaternion(f32(f), 0, 0, 0)
+ case quaternion256: dst = quaternion(f64(f), 0, 0, 0)
+
case: return false
}
return true