Odin icon indicating copy to clipboard operation
Odin copied to clipboard

add quaternion marshal and unmarshal to `core:encoding/json`

Open nico-barbas opened this issue 4 months ago • 3 comments

Add JSON marshal/unmarshal support for quaternion types in core:encoding/json.

  • Marshal: Quaternions are serialized as JSON arrays with four float components in [x, y, z, w] order
  • Unmarshal: JSON arrays of exactly 4 numeric values are parsed back into quaternion types in [x, y, z, w] order
  • Tests: Add a corresponding test quaternion to the test suits tests/core/encoding/json

nico-barbas avatar Aug 06 '25 05:08 nico-barbas

We don't support BECAUSE the order might be different for foreign software. There isn't a good way to make this work properly because quaternions in other places could be xyzw or wxyz.

gingerBill avatar Aug 06 '25 15:08 gingerBill

Maybe the best option is to allow us to specify the order in the struct tags. e.g.

field_a: quaternion256 `json:"field_a,xyzw"`,
field_b: quaternion256 `json:"field_b,wxyz"`,

gingerBill avatar Aug 06 '25 15:08 gingerBill

We don't support BECAUSE the order might be different for foreign software. There isn't a good way to make this work properly because quaternions in other places could be xyzw or wxyz.

Yes, that's what I initially thought when I looked at core:encoding/json and had some order issues in my json file.

However, I asked around and looked at the core:encoding/cbor. In the cbor marshal and unmarshal, it is always decoded as xyzw. I thought it was worth doing the same for the json one, and also consistent with Odin (quaternion are xyzw if I'm not mistaken) as well as with the core library.

Maybe the best option is to allow us to specify the order in the struct tags. e.g.

field_a: quaternion256 `json:"field_a,xyzw"`,
field_b: quaternion256 `json:"field_b,wxyz"`,

I can change my PR to take that into account if you see this as a valid and more robust way to handle it.

nico-barbas avatar Aug 06 '25 18:08 nico-barbas