rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
225 stars 29 forks source link

Invalid DateTime when deserializing from ISO-8601 #242

Closed antomys closed 4 months ago

antomys commented 1 year ago

Hello

The problem is that it doesn't deserialize the date in ISO-8601 format ("testDate":"2021-11-11T21:34:09.8494988Z")

See example below: image

(Top is what I expect. Bottom is deserialized)

Also using this NetJson settings: image

Note: data above is generated with Bogus in universal format.

See see example Json string:

[
   {
      "testString":"tivkou",
      "testStringArray":[
         "yscjfcx"
      ],
      "testInt":-743109272,
      "testIntArray":[
         2092180607
      ],
      "testDouble":0.8011138990823675,
      "testDoubleArray":[
         0.043662386039897916
      ],
      "testFloat":0.8328364,
      "testFloatArray":[
         0.6883843
      ],
      "testUInt":2205463946,
      "testUIntArray":[
         2856529429
      ],
      "testChar":"n",
      "testCharArray":[
         "l"
      ],
      "testByte":197,
      "testShort":2886,
      "testShortArray":[
         28477
      ],
      "testUShort":50262,
      "testUShortArray":[
         30809
      ],
      "testLong":4947650095307827828,
      "testLongArray":[
         2204728661568568098
      ],
      "testULong":11589561753318221824,
      "testULongArray":[
         13420881290243397632
      ],
      "testDate":"2022-03-22T05:40:42.8764516Z",
      "testTimeSpan":"1.21:14:55.6558187"
   }
]

So i do expect in this case date as : image

But i do get from NetJSON.NetJSON.Deserialize<T>(testString, JsonServiceExtensions.NetJsonOptions): image

antomys commented 1 year ago

For example, there is code that i use for tests https://github.com/antomys/CuriousBenchmarks/blob/62dec337ea30f2414dc61ab687181f57c43a8768/Json/Json.Tests/NetJsonTests.cs#L18

rpgmaker commented 1 year ago

Thanks for the detail breakdown. I will spend some time on going over it and get back to you.

On Tue, Jul 19, 2022, 7:23 AM Ihor Volokhovych @.***> wrote:

For example, there is code that i use for tests

https://github.com/antomys/CuriousBenchmarks/blob/62dec337ea30f2414dc61ab687181f57c43a8768/Json/Json.Tests/NetJsonTests.cs#L18

— Reply to this email directly, view it on GitHub https://github.com/rpgmaker/NetJSON/issues/242#issuecomment-1189119423, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEIF33ZDVS6AMOT3KRAKG3VU226NANCNFSM54ACRZMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

rpgmaker commented 1 year ago

@antomys , after looking much into it. The issue here is not related to serialization but rather the use of camelCase properties in your payload. The CamelCase is only used for serialization (Formatting, similar to using Format.Prettify) and not deserialization.

Refer to wiki to currently available options - https://github.com/rpgmaker/NetJSON/wiki/Quick-Guide

If you look at the other output of deserialization, you will notice that all the properties has default values. The reason the date does not match is because you are getting the default value which is set to the current date of 7/19/2022.

To solve this problem, i will need to support use of camelCase for deserialization or you can simply use


new NetJsonSettings
{
   CaseSensitive  = false,
   ... (Other options you are currently using)
}

Example is the following:


  [TestMethod]
  public void TestPersonClassWithMultipleNonDefaultConstructor() {
      var json = "{ \"name\": \"boss\", \"age\": 2, \"reasonForUnknownAge\": \"he is the boss\" }";
      var data = NetJSON.Deserialize<PersonTest>(json, new NetJSONSettings { CaseSensitive = false });
      Assert.IsTrue(data.Age == 2);
  }

public class PersonTest {
        public string Name { get; private set; }
        public int? Age { get; private set; }
        public string ReasonForUnknownAge { get; private set; }
        public PersonTest(string name, int age) {
            Age = age;
            Name = name;
        } // age is known
        public PersonTest(string name, string reasonForUnknownAge) {
            Name = name;
            ReasonForUnknownAge = reasonForUnknownAge;
        } // age is unknown, for some reason
    }
rpgmaker commented 1 year ago

Let me know if that make sense to you. Thanks

antomys commented 1 year ago

Thanks for the answer, @rpgmaker! The so, for now i will be using standart properties name, without came case. I thought that option CamelCase = true refers both to serialization and deserialization.

Tried to use it with CaseSensitive = false, as you suggested, failing with OutOfMemoryException in same test case (see photo below). As for now, it is not a big problem, so i'll just stick with native pascalCase.

image

rpgmaker commented 1 year ago

@antomys . Could you provide a full test project for me to repro the out of memory issue?

antomys commented 1 year ago

@rpgmaker sure. Here i created test project for you https://github.com/antomys/NetJsonOutOfMemoryException In program.cs placed todo above Deserialization method, that throws OutOfMemoryException. Thanks

rpgmaker commented 1 year ago

Awesome. Thanks again. I will try it out and see what I can do.

rpgmaker commented 1 year ago

Still looking into it. Strange that it is generating out of memory exception.

rpgmaker commented 1 year ago

Sorry I have not made much progress on this changes yet. Been busy on a lot of other things. I have not dropped this.

antomys commented 1 year ago

Hi! That's okay, don't worry. I'll try to investigate it too when i'll have some tive

rpgmaker commented 4 months ago

@antomys , This issue is already fixed in 1.4.3. It seem you are using a version from 2020. I will close this issue, let me know if you are still receiving the problem. Thanks