2

we are trying to serialize a JSON object [as a string] into a custom class. While we rather not use any third-party packages such as Newtonsoft.Json or Json.NET, we tried to utilize DataContractJsonSerializer. The JSON object contains a DateTime property that is provided in the "yyyy-MM-dd HH:mm:ss" and when it comes to serialization the expectation below is thrown.

There was an error deserializing the object of type DateTime content '2020-05-29 09:05:39' does not start with '\/Date(' and end with ')\/' as required for JSON

IMPORTANT: the issue will be solved with NewtonSoft package and adding JsonSerializerSettings to JsonConvert.DeserializeObject. As the final product is is a COM Object, our final target dll must have no dependant DLLs.

You can find the technical details below:

JSON object is :

{
  ...,
  "export_time": "2020-05-29 09:05:39",
  "app_version": "1.1.0",
  "allowed_mac_addresses": [
    "XX-XX-XX-XX-XX-XX"
  ],
  "signature": ""
}

Target class:

[DataContract]
public class MainFractionatorConfigFile
{
    [DataMember]
    internal string[] allowed_mac_addresses;

    [DataMember]
    internal DateTime export_time;

    [DataMember]
    internal string app_version;

    [DataMember]
    internal string signature;
}

and the serializer method is :

public static MainFractionatorConfigFile ReadMainFractionatorConfigFile(string json)
{
    var deserializedUser = new MainFractionatorConfigFile();
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
    var ser = new DataContractJsonSerializer(deserializedUser.GetType());
    deserializedUser = ser.ReadObject(ms) as MainFractionatorConfigFile;
    ms.Close();
    return deserializedUser;
}

Attempt 1: There are some other suggestions in the community such as this solution but they demand other packages to be referenced.

Attempt 2 was trying to add DateTimeDataContractSurrogate to DataContractJsonSerializer but was unsuccessful too.

To sum up we are looking for an alternative method for the code below:

 var mainFObj = JsonConvert.DeserializeObject<MainFractionatorConfigFile>(myJson, new JsonSerializerSettings
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                });

Any help or idea will be appreciated.

Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
  • 1
    [`DataContractJsonSerializer.DateTimeFormat`](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer.datetimeformat?view=netframework-4.8#System_Runtime_Serialization_Json_DataContractJsonSerializer_DateTimeFormat) was added in .Net 4.5. If you're using an earlier version you are out of luck, you will need to create a surrogate property for each one or switch to a different serializer. – dbc Jun 01 '20 at 21:55
  • I believe that surrogates are documented to not work for primitives, see https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/data-contract-surrogates#getdatacontracttype and https://learn.microsoft.com/en-us/archive/blogs/carlosfigueira/wcf-extensibility-serialization-callbacks#fine-grained-control-of-serialization-format-for-primitives – dbc Jun 01 '20 at 21:59
  • unfortunately, due to some restrictions, the solution has to remain on .net 4 and can't be upgraded to a later version. :-/ – Mahyar Mottaghi Zadeh Jun 01 '20 at 22:06
  • Then I reckon you will need to either add surrogate `string` properties to `MainFractionatorConfigFile`, or create a surrogate for `MainFractionatorConfigFile` itself. Sorry I can't be of more help. `JavaScriptSerializer` uses the same `DateTime` format so I don't think it will help either. – dbc Jun 01 '20 at 22:08
  • Thank you for your help. I know it's a sort of strange situation that has been struggling with since this morning! – Mahyar Mottaghi Zadeh Jun 01 '20 at 22:16

1 Answers1

2

In the case of having a constraint about adding third-party NuGet packages, why not trying to clone/download the package (i.e. Newtonsoft.JSON) from Github and add them directly to your project. So that you won't have any third-party dll your deployment (bin folder).

Please note that the latest versions of the Newtonsoft.JSON are implemented in .netcore and while your project is on .net framework 4, you have to download the compatible version. You can use the tags to find the best version to download.

Amir Jelo
  • 347
  • 1
  • 7