Q: How do I make Json.NET successfully convert this json date structure to C# DateTime?
Hello, I am trying to deserialize a return value from a method call on an existing Meteor.js app using the DDP protocol into a known/strict return structure.
I am using dynamics to implement most basic things, but, moving on to strict structures to get benefit from type safety and intelisense on C#s side.
However it is failing to successfully deserialize the Javascripts Date() into C#s DateTime using the ddp serialization structure for the javascripts Date():
"when": {
"$date": 1406886657338
}
Q: How do I make Json.NET successfully convert this json date structure to C# DateTime?
If a "protocol" middleware was possible, having DateTime to DDPs Date() would be awsome too.
My structures:
namespace xxxx.API.Structures
{
public struct loginParams
{
public string email;
public string apiClient;
}
public struct loginReturn
{
public string result;
public string session;
public string email;
public string user;
public DateTime when;
public string client;
}
}
The return value I want converted into loginReturn :
xxxx.DDP.Client.DDPClient.ConnectGS.AnonymousMethod__1 (err=(null), res={{
"result": "sucess",
"session": "v3gozkHgceoqGqsfd",
"email": "xxxx@gmail.com",
"user": "hueun3s8rKQWsoQDT",
"server": "Lnf3vAFaeoCiMWriY",
"when": {
"$date": 1406886657338
},
"client": "OfficialxxxxPlugin"
}}) in /Volumes/2TB/Files/Documents/Dropbox/Development/C#/xxxx/xxxx/xxxxAPI/xxxx.DDP.Client/DDPClient.cs:43
This isnt the best possible solution, and doesnt really add anything to the topic I wanted to discuss, but, for this specific problem, here is a fix that works.
private static Regex _dollarDateRegex = new Regex ("\"\\$date\\\":\\s?(\\d+)");
if (message.Contains ("$date")) {
var matches = _dollarDateRegex.Matches (message);
foreach (Match match in matches) {
string date = match.Groups [1].Value;
int startOfDate = message.IndexOf ("$date\":" + date, StringComparison.CurrentCulture);
int startOfObject = startOfDate;
int endOfObject = startOfDate;
// skip to first { behind
while (message [startOfObject--] != '{') {
}
// skip to last } in front
while (message [endOfObject++] != '}') {
}
var diff = endOfObject - startOfObject;
StringBuilder s = new StringBuilder (message);
s.Remove (startOfObject, diff);
s.Insert (startOfObject, ": new Date(" + date + ")");
message = s.ToString ();
}
}
This yields :
{
...
when: new Date(2191283823),
...
}