Saving/restoring with LengthUnit (or any enum) changing #1114
Replies: 1 comment
-
Hi, we recently tackled the issue of volatile enum integer values. So as of v4.133.0, at least the enum values should now be stable. However, occasionally we do rename units by deprecating it and creating a new unit, to fix typos or ambiguous naming such as I haven't thought much about this in a long time, but I see a few ways to handle it: 1. Map to your own types for serializationCreate your own enum types and your own DTO types, similar to Pros:
Cons:
2. Shared forward-mapping of deprecated units for everyone to useSay, if I believe a switch statement and recursively calling itself until a non-deprecated unit is resolved should do the trick. However, in v5 we remove several deprecated units, so that complicates the mapping if the serialized string "Gallon" no longer exists as a unit enum. I assume UnitsNet.Serialization.JsonNet will fail to deserialize units that have been removed, so the mapping would have to be performed either before deserialization or the converter could be modified to fix it during deserialization. UnitsNet.Serialization.JsonNet would requires some name-based mapping, while other custom serializations might require integer-based mapping, so I guess we would have to support both. As of 4.133.0 the integer values are now stable at least, also when removing units. This mapping could be maintained in UnitsNet repo, so if you do decide to take this approach it would be great if you could contribute it to the repo so it can be maintained there. Pros:
Cons:
Let me know what you think, and whether you would be interested in attempting a pull request for option 2. |
Beta Was this translation helpful? Give feedback.
-
In our application, we heavily use UnitsNet for most of our data. We use a file based approach for data storage and serialize/deserialize everything, including UnitsNet information like the unit. We've gone back and forth on the "correct" way to do this, whether it be saving a LengthUnit enum string or the backing int value and have run into problems in both cases as UnitsNet changes. In our latest upgrade of the package, we have some data saved before the upgrade with a LengthUnit value of 10 which now pointing to a LengthUnit.Fathom instead of a LengthUnit.Foot which throws things off for us. We've also had a similar case where the enum string has changed which causes the same issue.
Do you have any suggestions for how to deal with the LengthUnit enum order/strings changing? We've previously mapped the old strings to new strings and can do something similar for the order change but I just wanted to check in and see if there's a known better way to do this or any other ideas we haven't considered yet.
Thank you,
Matthew
Beta Was this translation helpful? Give feedback.
All reactions