gregsdennis / Manatee.Json

A fully object-oriented approach to JSON manipulation, validation, and serialization that focuses on modeling the JSON structure rather than mere string parsing and conversion.
MIT License
198 stars 32 forks source link

Incorrect data serialization for classes that extends Dictionary class #276

Closed 3axap4eHko closed 4 years ago

3axap4eHko commented 4 years ago

Describe the bug Incorrect data serialization for classes that extends Dictionary class

To Reproduce

  1. Define a class
    public class D : Dictionary<string, object>{ }
  2. Instantiate an object
    D objectValue = new D
    {
    ["property1"] = 20,
    ["property2"] = 30,
    };
  3. Serialize it
    var serializer = new JsonSerializer();
    var jsonValue = serializer.Serialize(objectValue);
  4. See error
    Console.WriteLine(jsonValue); // { }

Expected behavior If you replace class D to Dictionary<string, object> you will see

Console.WriteLine(jsonValue.Object.Count); // {"property1":20,"property2":30}

Desktop (please complete the following information):

gregsdennis commented 4 years ago

The serializer can't make assumptions on how to serialize a type based on the type's inheritance. If you were to add a property to D, then serializing the same way a dictionary is serialized would be wrong.

You need to tell the serializer how to handle that class, either by implementing IJsonSerializable on the class or by creating and registering an ISerializer implementation.