C
C
Cyril2015-06-04 13:45:12
JSON
Cyril, 2015-06-04 13:45:12

Serializing an object with Date in unix format - how to do it?

Good afternoon!
I must say right away that the question is about Action Script. In the same C#, everything is done with a bang without a tambourine.
There is a Flash client (ActionScript, no Flex) and an ASP.NET server. Data in JSON is chasing between them. The model of the object is the simplest, but it is in it that there is a plug:

class Simple
{
    public var Name:String;
    public var Number: String;
    public var RegistrationDate: Date;
}

I have no way to serialize Simple in such a way that it is clear to the server. The server needs a Date serialized to something like Date(1433414354166) (Unix format, sort of), and the ActionScript JSON class serializes the date to 2015-04-06T13:43:18.605Z .
The server accepts data in Date(1433414354166) because the json deserializer (JavaScriptSerializer) is used there. Do not offer to rewrite the server.
What do I need to do so that the Date class from ActionScript 3 is normally converted to unix format, and even in JSON? I naturally did as the manuals advise:
Date.prototype.toJSON = function (k):* 
{ 
     return "\/Date(" + this.getTime() + ")\/"; // ЧТО ТУТ????
}

But the server does not accept this either: it says that the DateTime format is incorrect.
The second day already I fight... help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Rytikov, 2015-06-04
@chlp

See https : //developer.mozilla.org/en-US/docs/Web/JavaS... , https://msdn.microsoft.com/library/cc836459(v=vs.94).aspx

function replacer(key, value) {
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, replacer);

You can write a replacer function that will go through the properties of the passed object and if it encounters val instanceof Date , it will add JSON.stringify(+val) rather than JSON.stringify(val) to the JSON string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question