You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a long-standing serde bug without an intention to fix, that causes all structs that use #[serde(flatten)] attribute to be interpreted as maps during serialization. Unfortunately, that means that all formats that differentiate between structs and maps cannot properly round-trip palette types through serde. It works on JSON, because it has no distinction between structs and maps.
An example format to test against would be RON (and here is it's related issue). For binary format that suffers from the same issue, check bincode.
In order to fix this, palette needs to implement serialization manually, and use serialize_struct with all the expected fields being serialized directly. That way serde will always treat those types as structs uniformly. This is not fully trivial due to generics, but should be possible with additional crate-private traits that operate on struct serializers adding all necessary fields.
Example RON behaviour of serializing Srgba with current setup:
{"red":0.5,"green":1.0,"blue":0.0,"alpha":1.0}
Deserialization of above fails, because generated deserializer expects identifiers in place of map keys, but ron always passes strings. In order to deserialze above struct properly, the representation must be manually changed to
{red:0.5, green:1.0, blue:0.0, alpha:1.0}
With manual serialization implementation, the ideal supported serialized form would be one of those, or ideally both:
Thank you for highlighting this! It should definitely be fixed. I'm all for supporting both formats if it can be done in a nice way (I haven't tried to do a manual implementation in quite a while, so I don't have it fresh in mind).
313: Improve serializing r=Ogeon a=Ogeon
This fixes a long-standing bug in how `Alpha` and `PreAlpha` are serialized. They used to be flattened to a map, which is fine in JSON but not in more expressive formats, such as RON. The replacement implementation will only handle a subset of possible values (structs, tuples and sequences), but they should be the most common ones. It should also be possible to extend it later.
I have also taken the opportunity to add some helpful utility functions for serializing and de-serializing.
## Closed Issues
* Fixes#130, using a custom implementation.
## Breaking Change
Technically breaking, since it changes the serialization format a bit, but the old behavior was buggy and not always usable. The serialized type and name for `Alpha` and `PreAlpha` is now inherited from the color's type. So, for example, `Srgba` will be serialized and de-serialized as an `Rgb` struct with an additional `alpha` field. Unit types get extended to newtypes, and newtypes get extended to tuples.
Co-authored-by: Erik Hedvall <erikwhedvall@gmail.com>
There is a long-standing serde bug without an intention to fix, that causes all structs that use
#[serde(flatten)]
attribute to be interpreted as maps during serialization. Unfortunately, that means that all formats that differentiate between structs and maps cannot properly round-trip palette types through serde. It works on JSON, because it has no distinction between structs and maps.An example format to test against would be RON (and here is it's related issue). For binary format that suffers from the same issue, check bincode.
In order to fix this,
palette
needs to implement serialization manually, and useserialize_struct
with all the expected fields being serialized directly. That way serde will always treat those types as structs uniformly. This is not fully trivial due to generics, but should be possible with additional crate-private traits that operate on struct serializers adding all necessary fields.Example RON behaviour of serializing
Srgba
with current setup:Deserialization of above fails, because generated deserializer expects identifiers in place of map keys, but ron always passes strings. In order to deserialze above struct properly, the representation must be manually changed to
With manual serialization implementation, the ideal supported serialized form would be one of those, or ideally both:
For reference, this is how amethyst implements transform deserialization as seq and as map to accept both formats as valid in a vector field.
The text was updated successfully, but these errors were encountered: