Skip to content

DateTime Conversion #88

@badboy22

Description

@badboy22

I want to use NetJSON as a replacement of JSON.NET. Performance of NetJSON is really awesome. But i have some trouble about DateTime conversion.

First of all;

I try to serialize & deserialize, but it works only if i use NetJSON.NetJSONDateFormat.EpochTime, using default or iso settings failed.

You can try with this code;

public class Dummy
{
public DateTime? DateTimeStamp;
public int IntegerValue;
public string StringValue;
public List ListValue;
public Dictionary<int, Dummy> DictionaryValue;
public Dummy DummyObjValue;

public Dummy()
{
    ListValue = new List<Dummy>();
    DictionaryValue = new Dictionary<int, Dummy>();
    DateTimeStamp = DateTime.UtcNow;
}

public Dummy(DateTime? dateTime)
{
    ListValue = new List<Dummy>();
    DictionaryValue = new Dictionary<int, Dummy>();
    DateTimeStamp = dateTime ?? DateTime.UtcNow;
}

}

private void Test()
{
NetJSON.NetJSON.IncludeFields = true;
NetJSON.NetJSON.DateFormat = NetJSON.NetJSONDateFormat.EpochTime;

var dummyObject = new Dummy(new DateTime(2015, 11, 08, 19, 18, 00));
dummyObject.DictionaryValue.Add(1, new Dummy() { IntegerValue = 1 });
dummyObject.DictionaryValue.Add(2, new Dummy() { IntegerValue = 2, DummyObjValue = new Dummy() { StringValue = "SubClass2" } });
dummyObject.DictionaryValue.Add(3, new Dummy() { IntegerValue = 3, DummyObjValue = new Dummy() { StringValue = "SubClass3" } });
dummyObject.DictionaryValue.Add(4, new Dummy() { IntegerValue = 4, DummyObjValue = new Dummy() { StringValue = "SubClass4" } });
dummyObject.DictionaryValue.Add(5, new Dummy() { IntegerValue = 5, DummyObjValue = new Dummy() { StringValue = "SubClass5" } });
dummyObject.IntegerValue = 22;
dummyObject.StringValue = "Test";
dummyObject.ListValue.Add(new Dummy() { StringValue = "A", DummyObjValue = new Dummy() { StringValue = "SubClass1" } });
dummyObject.ListValue.Add(new Dummy() { StringValue = "B", DummyObjValue = new Dummy() { StringValue = "SubClass2" } });
dummyObject.ListValue.Add(new Dummy() { StringValue = "C", DummyObjValue = new Dummy() { StringValue = "SubClass3" } });
dummyObject.ListValue.Add(new Dummy() { StringValue = "D", DummyObjValue = new Dummy() { StringValue = "SubClass4" } });
dummyObject.ListValue.Add(new Dummy() { StringValue = "E", DummyObjValue = new Dummy() { StringValue = "SubClass5" } });
dummyObject.ListValue.Add(new Dummy() { StringValue = "F" });
dummyObject.DummyObjValue = new Dummy() { IntegerValue = 55, StringValue = "Dummy" };

var tmpJson = NetJSON.NetJSON.Serialize(dummyObject);
var tmpObj = NetJSON.NetJSON.Deserialize<Dummy>(tmpJson);

}

Another problem; JSON.NET (6.0.6) has two DateTime formatter handling settings; IsoDateFormat, MicrosoftDateFormat.

I tried to;
Serialize using JSON.NET with IsoDateFormat, Deserialize using NetJSON with Iso => Success
Serialize using NetJSON with Iso, Deserialize using JSON.NET with IsoDateFormat => Failed ({"Could not convert string to DateTime: 2015-11-8T19:18:0.0Z. Path 'DateTimeValue', line 1, position 39."})

You can try with this code;

public class Simple
{
public DateTime DateTimeValue;

public Simple()
{
    DateTimeValue = new DateTime(2015, 11, 08, 19, 18, 00);
}

}

public void NetJSONtoJSONNetTest()
{
NetJSON.NetJSON.IncludeFields = true;
NetJSON.NetJSON.DateFormat = NetJSON.NetJSONDateFormat.ISO;
var tmpJson = NetJSON.NetJSON.Serialize(new Simple())
var tmpObj = Newtonsoft.Json.JsonConvert.DeserializeObject(tmpJson, new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat });
}

public void JSONNettoNetJSONTest()
{
NetJSON.NetJSON.IncludeFields = true;
NetJSON.NetJSON.DateFormat = NetJSON.NetJSONDateFormat.ISO;
var tmpJson = Newtonsoft.Json.JsonConvert.SerializeObject(new Simple(), new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat });
var tmpObj = NetJSON.NetJSON.Deserialize(tmpJson);
}

I have huge data that serialized using JSON.NET with MicrosoftDateFormat and NetJSON doesn't have an alternative built-in conversion format for that. Do you have any suggestions ?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions