Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hxmcn opened this issue Dec 18, 2021 · 8 comments
Open

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

hxmcn opened this issue Dec 18, 2021 · 8 comments

Comments

@hxmcn
Copy link

hxmcn commented Dec 18, 2021

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");
    }
@mgholam
Copy link
Owner

mgholam commented Dec 19, 2021

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

@hxmcn
Copy link
Author

hxmcn commented Dec 19, 2021

Yes. but the hour is different.

@mgholam
Copy link
Owner

mgholam commented Dec 26, 2021

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
Copy link
Author

hxmcn commented Dec 28, 2021

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;
    }
}

}

@mgholam
Copy link
Owner

mgholam commented Dec 29, 2021

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

@mgholam
Copy link
Owner

mgholam commented Dec 29, 2021

Try testing @12:00 noon

@mgholam
Copy link
Owner

mgholam commented Dec 29, 2021

also

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

@hxmcn
Copy link
Author

hxmcn commented Dec 29, 2021

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;
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants