mgholam/fastJSON

Deerialize utc-time different from the original field utc-time

hxmcn opened this issue · 8 comments

hxmcn commented

For a simple Object with DateTime field, assign UTC-Time to it and serialize the object to json-text, then deserialize an object from the same json-text, the UTC-Time does not match with the original value (your time zone should not be UTC to reproduce the bug).

Code Example:

public class MUtcTime
{
public long ID { get; set; }
public DateTime LogUtcTime { get; set; }
}

    public static void Test()
    {
        var m = new MUtcTime
        {
            ID = 100,
            LogUtcTime = DateTime.UtcNow,
        };

        var jText = fastJSON.JSON.ToJSON(m);
        var gBack = fastJSON.JSON.ToObject<MUtcTime>(jText);
        if (m.LogUtcTime == gBack.LogUtcTime)
            Console.Write("Pass");
        else
            Console.Write("Failed");
    }

Is the year, month, day...seconds the same?

hxmcn commented

Yes. but the hour is different.

If you add the following:

        Console.WriteLine(m.LogUtcTime);
        Console.WriteLine(gBack.LogUtcTime);

You will get :

2021-12-26 15:42:12
2021-12-26 15:42:12

Which shows the dates are the same, however the objects will not be since the nano seconds are not set on the deserialized dates, hence your condition will fail.

hxmcn commented

Sorry, I got different result:
Source:2021-12-28 23:19:58
GetBack:2021-12-29 07:19:58

Please see the picture below for details:
FastJson


Environment:
Windows 10 English (Time zone is UTC+8, Beijing time)
Visual Studio 2022 Community
.Net 6 Console Application with fastJSON 2.4.0.4 installed from Nuget:
PackageReference Include="fastJSON" Version="2.4.0.4"


Full source code:
namespace TestCode
{
public class TestClass
{
public class Model { public long ID { get; set; } public DateTime UtcTime { get; set; } }

    public static int Main()
    {
        // See https://aka.ms/new-console-template for more information
        Console.WriteLine("Hello, World!");

        var model = new Model { ID = 100, UtcTime = DateTime.UtcNow };
        var jsonText = fastJSON.JSON.ToJSON(model);
        var gBack = fastJSON.JSON.ToObject<Model>(jsonText);

        Console.WriteLine("Source:" + model.UtcTime.ToString("yyyy-MM-dd HH:mm:ss"));
        Console.WriteLine($"JsonText: {jsonText}");
        Console.WriteLine("GetBack:" + gBack.UtcTime.ToString("yyyy-MM-dd HH:mm:ss"));

        return 0;
    }
}

}

My results

***** tests.aaaaaaa
Source:2021-12-29 06:15:41
JsonText: {"$types":{"tests+Model, UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null":"1"},"$type":"1","ID":100,"UtcTime":"2021-12-29T06:15:41Z"}
GetBack:2021-12-29 06:15:41

Try testing @12:00 noon

also

        var jsonText = fastJSON.JSON.ToJSON(model, new JSONParameters { UseUTCDateTime =true});
        var gBack = fastJSON.JSON.ToObject<Model>(jsonText , new JSONParameters {  UseUTCDateTime=true});
hxmcn commented

Thanks @mgholam
However, my test shows set UseUTCDateTime = false is OK:
FastJsonJParams


Full source codes:
using fastJSON;

namespace TestCode
{
public class TestClass
{
public class Model { public long ID { get; set; } public DateTime UtcTime { get; set; } }

    public static int Main()
    {
        void Test(string notes, JSONParameters? jParams = null)
        {
            var model = new Model { ID = 100, UtcTime = DateTime.UtcNow };

            var jsonText = jParams == null ? JSON.ToJSON(model) : JSON.ToJSON(model, jParams);
            var gBack = jParams == null ? JSON.ToObject<Model>(jsonText) : JSON.ToObject<Model>(jsonText, jParams);

            Console.WriteLine(notes);
            Console.WriteLine($"\tSource:{model.UtcTime}");
            Console.WriteLine($"\tJsonText: {jsonText}");
            Console.WriteLine($"\tGetBack:{gBack.UtcTime}{Environment.NewLine}");
        }

        Test("Without JSONParameters:");
        Test("UseUTCDateTime = true:", new JSONParameters { UseUTCDateTime = true });
        Test("UseUTCDateTime = false:", new JSONParameters { UseUTCDateTime = false });

        Console.Read();
        return 0;
    }
}

}