简体   繁体   中英

JavaScriptSerializer serializes to JSON by default?

Isn't this a bit odd?
Looks like private methods actually accept parameter that allows to set JavaScript serialization mode instead of the default (JSON), but it is never exposed... To make this even more odd Serialize() happily accepts string and encodes it as JavaScript string (it escapes quiet a bit more than required by JSON standard), where as DateTime will be encoded as JSON.

I am confused. Is there a derived class somewhere that does actual JavaScript serialization?

I don't see anything odd about the string behavior. The JSON representation of a string is just a string.

You don't really see any JSON notation until you serialize objects with properties as opposed to simple types.

JSON is actual javascript though, so I'm not sure what sort of serialization you're looking for, exactly.

var s = new JavaScriptSerializer();
Console.WriteLine(s.Serialize(DateTime.Now));
Console.WriteLine(s.Serialize("I like things & stuff ' \" ."));
Console.WriteLine(s.Serialize(3.14));
/* Should serialize to JSON object */
Console.WriteLine(s.Serialize(new { String = "I like things & stuff ' \" .", Date = DateTime.Now, PI = 3.14 }));
/* Should serialize to array */
Console.WriteLine(s.Serialize(new object[] { "I like things & stuff ' \" .", DateTime.Now, 3.14 }));

Output:

"\/Date(1316374642273)\/"
"I like things & stuff \u0027 \" ."
3.14
{"String":"I like things & stuff \u0027 \" .","Date":"\/Date(1316374642278)\/","PI":3.14}
["I like things & stuff \u0027 \" .","\/Date(1316374642280)\/",3.14]

There is some backstory to the funny date format.

The 'Javascript' serialization format appears to be code left over from the first attempt at serializing dates into JSON. The only place that value is actually used is in SerializeDateTime I would imagine the team just made those methods private after determining that was a bad idea per the linked article:

The first thing we tried was to inject Date constructors in the JSON string. This is a (very) bad idea for a number of reasons. First, it simply does not conform to the JSON specs. Second, any JSON parser that validates its input before parsing it will cough on such a thing. Finally, it establishes a precedent: why would it be allowed for dates and not for arbitrary types? This would just defeat the purpose of JSON.

If you really just need to serialize an individual DateTime to Javascript, there's no reason you can't steal those couple of lines of code, but if you're serializing a complex object there are valid reasons you shouldn't be doing that.

public string SerializeDateTime(DateTime datetime)
{ 
    DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    DatetimeMinTimeTicks = time.Ticks;

    var sb = new StringBuilder();
    sb.Append("new Date(");
    sb.Append((long) ((datetime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 0x2710L));
    sb.Append(")");
    return sb.ToString();
}  

If what you need is to actually use a serialized date from ASP.NET on a webpage, and you aren't using ASP.NET AJAX, that is covered in this question: How do I format a Microsoft JSON date?

Serializing data is turning it into a string/stream, usually so it can be stored in a database, or transmitted over a network. Javascript is a programming language. JSON is a subset of Javascript that represents objects, arrays, strings, and numbers as literals which can be represented in a string. All that to say that JSON is Javascript. If you want to serialize an object graph as Javascript, it is JSON. What behavior are you expecting? While the JavaScriptSerializer can be used to escape strings or serialize a DateTime, I think the intended use is to serialize object graphs. And, if it does escape more characters than JSON requires, I am sure it is still valid JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM