Building a JsonConstructor to be able to deserialize my object

727 views Asked by At

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?

1

There are 1 answers

0
Ufuk Hacıoğulları On BEST ANSWER

Here's an example JsonConverter to get you started.

public class DistanceConverter : JsonConverter
{
    private readonly IDictionary<string, DistanceType> _distanceTypeMap;

    public DistanceConverter()
    {
        _distanceTypeMap = new Dictionary<string, DistanceType>
        {
            {"Meters", DistanceType.Meter},
            {"Yards", DistanceType.Yard}
        };
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        Distance distance = value as Distance;
        if (distance == null)
        {
            writer.WriteNull();
            return;
        }

        writer.WriteStartObject();

        foreach (KeyValuePair<string, DistanceType> pair in _distanceTypeMap)
        {
            writer.WritePropertyName(pair.Key);
            writer.WriteValue(distance.GetValue(pair.Value));
        }

        writer.WriteEndObject();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        Distance result = null;

        while (reader.Read())
        {
            var key = reader.Value;
            string value = reader.ReadAsString();

            if (result == null && key != null)
            {
                DistanceType distanceType;
                if (_distanceTypeMap.TryGetValue(key.ToString(), out distanceType))
                {
                    double parsedValue = JToken.Parse(value).Value<double>();
                    result = new Distance(distanceType, parsedValue);
                }
            }
        }

        return result;
    }

    public override bool CanConvert(Type objectType)
    {
        return typeof(Distance) == objectType;
    }
}

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.

public class DistanceConverterTests
{
    private JsonSerializerSettings _jsonSerializerSettings;

    [SetUp]
    public void Setup()
    {
        _jsonSerializerSettings = new JsonSerializerSettings
        {
            Converters = new List<JsonConverter> { new DistanceConverter() }
        };
    }


    [Test]
    public void DeserializeTest()
    {
        string json = File.ReadAllText("data.json");

        var distance = JsonConvert.DeserializeObject<Distance>(json, _jsonSerializerSettings);

        distance.Meters.Should().BeInRange(0.360, 0.361);
    }

    [Test]
    public void SerializeTest()
    {
        Distance distance = new Distance(DistanceType.Meter, 2.20);

        string json = JsonConvert.SerializeObject(distance, _jsonSerializerSettings);

        Console.WriteLine(json);
    }
}