Home icon indicating copy to clipboard operation
Home copied to clipboard

Unable to deserialize JSON in some cases

Open torbacz opened this issue 2 years ago • 0 comments

Library/API/IoT binding

nanoFramework.Json

Visual Studio version

No response

.NET nanoFramework extension version

No response

Target name(s)

No response

Firmware version

No response

Device capabilities

No response

Description

Examples (we faced this kind of issues in our project and we had to fork nanoframework.Json):

  • DateTime - by default nanoFramework.Json requires milliseconds in json date time to successfully convert it into DateTime object. By default .net6 API is returning DateTime without milliseconds .
  • Deserialize JSON value based on other JSON field (one field has value for object, second type of object to create). In fact deserialization of object mapped to interfaces.

This kind of issues can be resolved via converters in NET6: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0

Solution in nano:

  • Create base class for converters (read/write methods)
  • Create static Hashtable to store all converters
  • Create few methods for adding/replacing converters from outside of nanoFramework.Json
  • Call the converter code if the type is mapped

After the converted feature will be confirmed to work - replace current code:

if (rootElementType == null
    && rootType.FullName == "System.Collections.Hashtable")
rootElementType == null
   && rootType.FullName == "System.Collections.ArrayList"

with converters. This will result in much cleaner code and being able to test converters logic separately.

How to reproduce

No response

Expected behaviour

No response

Screenshots

No response

Sample project or code

No response

Aditional information

No response

torbacz avatar Aug 07 '22 10:08 torbacz