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

DateTime field in JObject Deserialization #99

Closed
alana1 opened this issue Aug 24, 2016 · 11 comments
Closed

DateTime field in JObject Deserialization #99

alana1 opened this issue Aug 24, 2016 · 11 comments
Labels

Comments

@alana1
Copy link

alana1 commented Aug 24, 2016

Hello,

I have a changefeed and returns JObject. In the JObject contains the DateTime field. However, the JObject returns the epoch time. When deserializing JObject to strongly type object, any properties with DateTime epoch time is not converted to the original datetime utc format we are expecting. How can I deserialize the jobject and return correct datetime. A sample of the json is below.

{
  "ChatQueueId": 1,
  "ChatRequestNo": 0,
  "DeliveredTime": {
    "$reql_type$": "TIME",
    "epoch_time": -62135596800,
    "timezone": "+00:00"
  },
  "EmailAddress": "[email protected]",
  "EndTime": {
    "$reql_type$": "TIME",
    "epoch_time": 1472074019.588,
    "timezone": "+00:00"
  },
  "EstablishedTime": {
    "$reql_type$": "TIME",
    "epoch_time": -62135596800,
    "timezone": "+00:00"
  },
  "FirstName": "Jerry",
  "InitialMessage": null,
  "KeyValues": [
    {
      "Key": "defid",
      "Value": "5454564"
    },
    {
      "Key": "defid",
      "Value": "5454564"
    },
    {
      "Key": "defid",
      "Value": "5454564"
    }
  ],
  "LastName": "McGiver",
  "RequestTime": {
    "$reql_type$": "TIME",
    "epoch_time": 1472070419.588,
    "timezone": "+00:00"
  },
  "SiteId": 0,
  "id": "70f6d8a0-4375-41f1-9db9-129683b7f3a1"
}
@bchavez
Copy link
Owner

bchavez commented Aug 24, 2016

Just a few questions:

  • DO include the driver version you are using.
  • DO describe the operating system for the Server and Client (Windows / Linux). Also, the run-time platform and versions (.NET Framework/.NET Core/Mono).

Also, are you customizing the Newtonsoft.Json serializer? If so, can you post your configuration? Thanks.

@alana1
Copy link
Author

alana1 commented Aug 24, 2016

Hello,

I am using C# driver version 2.3.9.0. RethinkDB for Windows version 2.3.4. I am not using customized serializer.

@bchavez
Copy link
Owner

bchavez commented Aug 24, 2016

  • Could you update to the latest driver version v2.3.14?
  • Are you using .NET Core or .NET Full?
  • Is the driver running on windows also?
  • What version is the Newtonsoft.Json dependency?

@bchavez
Copy link
Owner

bchavez commented Aug 24, 2016

Hey @alana1 ,

This must be due to something else... (like customizing the serializer) the following unit test passes in the C# driver currently:

[Test]
public void try_basic_datetime_deseralization()
{
    var obj = new JObject
        {
            ["Name"] = "Brian",
            ["dob"] = DateTime.Parse("8/24/2016")
        };

    var fromDb = R.Expr(obj).RunResult<JObject>(conn);
    var dateTimeValue = fromDb["dob"] as JValue;
    dateTimeValue.Type.Should().Be(JTokenType.Date);
    dateTimeValue.Value.Should().BeOfType<DateTime>();
}

Protocol Trace

TRACE JSON Send: Token: 3, JSON: [1,{"Name":"Brian","dob":{"$reql_type$":"TIME","epoch_time":1472022000.0,"timezone":"-07:00"}},{}]
TRACE JSON Recv: Token: 3, JSON: {"t":1,"r":[{"Name":"Brian","dob":{"$reql_type$":"TIME","epoch_time":1472022000,"timezone":"-07:00"}}]}

So we are converting the time pseudo types correctly with JObject. You'll have to send me a PR with a failing unit test in order to debug this more. 😕

⏳ 🔍 _"But I still haven't found what I'm for..."_

bchavez added a commit that referenced this issue Aug 24, 2016
@bchavez
Copy link
Owner

bchavez commented Aug 24, 2016

@alana1 by any chance, are you using .Run<List<JObject>>() or something like it? If so, this would be a gotcha.

https://github.com/bchavez/RethinkDb.Driver/wiki/GOTCHA!#reql_type-all-up-in-my-jobject


🚏 🚌 _"Fe, Fi, Fo, Fum... Well, I'll be darn here it comes... The Double Dutch Bus is on the street"_

@alana1
Copy link
Author

alana1 commented Aug 24, 2016

I am using RunChanges(conn). Is that okay?

@bchavez
Copy link
Owner

bchavez commented Aug 24, 2016

Ahhhh. Okay, that makes sense now. Yeah, that would be a bug in the driver.

.RunChanges is a wrapper for .RunCursor<Change<JObject>> which is just like the gotcha above; except this on is on meeee!!!! 🐛

Haha, okay, I'll work on getting a fix for you soon.

⌚ 🌆 _"I just can't wait... I just can't wait... for saturday night..."_

@bchavez bchavez added the bug label Aug 24, 2016
@alana1
Copy link
Author

alana1 commented Aug 24, 2016

Thank you and much appreciated.

@bchavez
Copy link
Owner

bchavez commented Aug 25, 2016

Hey @alana1 , as a temporary workaround, instead of using .RunChanges<JObject>(conn), could you use .RunCursor<JObject>(conn) instead? It's pretty much the same thing, and it will resolve your issue temporarily.

I'm going to need a little more time to think about how to resolve this issue effectively in C#; it's turning out to be a more tricky problem with the type system than I originally thought.

@alana1
Copy link
Author

alana1 commented Aug 25, 2016

Hello @bchavez, your suggestion worked. Thank you for your help on this.

@bchavez bchavez added gotcha and removed bug labels Aug 25, 2016
@bchavez
Copy link
Owner

bchavez commented Aug 25, 2016

Hey @alana1 so I gave this a lot of thought. I really tried every trick in my book to think this one through for a clean performant implementation....

I decided that we're not going to support JObject in the RunChanges<JObject> run helper. The reasoning is because the generic type parameter open-endedness really complicates the code to check for nested JToken types. For example,

.RunChanges<JObject>()
.RunChanges<List<JObject>>()
.RunChanges<Tuple<JObject, JArray>>>()
... and on. and on.

So, I think the best we can do is document this behavior in the GOTCHA! and recommend that developers use .RunCursor<JObject>() instead of .RunChanges<JObject> as I've shown you above. .RunChanges<T> should really be only for POCO strongly typed conversions from JSON to T POCO.

🔥 🌋 _"We're building it up, to break it back down..."_

@bchavez bchavez closed this as completed Aug 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants