I am using JSON.net and am trying to serialize and deserialize a Distance object from the opensource UnitClassLibrary. Currently, I have an object serialized as the following JSON:
{
"ThirtySecondsOfAnInch": 454,
"SixteenthsOfAnInch": 227,
"EighthsOfAnInch": 113.5,
"QuartersOfAnInch": 56.75,
"HalvesOfAnInch": 28.375,
"Inches": 14.1875,
"Feet": 1.1822916666666667,
"Yards": 0.3940972222222222,
"Miles": 0.00022391887626262627,
"Millimeters": 360.36249999999995,
"Centimeters": 36.03625,
"Meters": 0.3603625,
"Kilometers": 0.0003603625,
"Architectural": "1'2 3/16\""
}
I can take any one of these and turn it into a distance object using this class. For example, using the last architectural string, I could use this constructor:
Distance newDistance = new Distance("1'2 3/16\"");
Or, using the 32nd of a inch, I could do:
Distance newDistance = new Distance(DistanceType.ThirtySecond, 454.0);
However, I am not sure the best way to write a JsonConstructor (the type of constructor that JSON.net can specifically use) to take either type of output upon passing a JSON string with JsonConvert.DeserializeObject<Distance>(json);
.
How can I write a constructor to take a new distance object?
Here's an example JsonConverter to get you started.
This has problems at the moment. I couldn't understand why do I get zero distance even if I create the object in deserialization. Here's a few tests to see the results:
Update: I fixed the bug while reading it again. It works at the moment but you may get exceptions when you add other units to the dictionary.