0

The test that I'm running simply takes a .net object serializes it to a json string. I then take that string and attempt to deserialize it back into a .net object. The string that is being serialized looks good, but for some reason, deserialization is failing on two properties, an int and a DateTime

These are the .net properties being used:

public string TokenString { get; set; }
public TokenUsage TokenUsage { get; set; }
public string TokenType { get; set; }
public string UserId { get; set; }
public DateTime Expires { get; private set; }
public int ExpiresIn { get; private set; }

And this is the json that is being serialized:

{
    "TokenString":"encryptedtoken",
    "TokenUsage":300,
    "TokenType":"Bearer",
    "UserId":"myId",
    "Expires":"2013-07-05T11:28:18.4179133Z",
    "ExpiresIn":3600,
    "IsExpired":false
}

And here's the inspector, following deserialization of that very string:

enter image description here

As you can see, most everything is good. Token usage (an enum) seems to have even deserialized correctly. I'm not doing anything fancy in the serialize/deserialize area. Just JsonConvert.Serialize(myObject) and JsonConvert.Deserialize<MyObject>(jsonString)

Any ideas?

Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

1 Answers1

4

Those two fields have private setters. If you want to use private setters, then you can update your contract resolver to allow this, as described in this answer.

There are some other techniques described here.

The easiest of which is to mark your properties with [JsonProperty] like this:

[JsonProperty]
public DateTime Expires { get; private set; }

[JsonProperty]
public int ExpiresIn { get; private set; }
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575