简体   繁体   English

JavaScriptSerializer默认情况下序列化为JSON吗?

[英]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. 看起来私有方法实际上接受了允许设置JavaScript序列化模式的参数,而不是默认的(JSON),但它从未公开...为了使这种情况更加奇怪,Serialize()愉快地接受字符串并将其编码为JavaScript字符串(转义比JSON标准要求的要安静一些),因为DateTime将被编码为JSON。

I am confused. 我很困惑。 Is there a derived class somewhere that does actual JavaScript serialization? 是否在某个地方进行了实际的JavaScript序列化的派生类?

I don't see anything odd about the string behavior. 我没有看到有关字符串行为的任何奇怪信息。 The JSON representation of a string is just a string. 字符串的JSON表示形式只是一个字符串。

You don't really see any JSON notation until you serialize objects with properties as opposed to simple types. 在序列化具有属性(而不是简单类型)的对象之前,您实际上看不到任何JSON表示法。

JSON is actual javascript though, so I'm not sure what sort of serialization you're looking for, exactly. JSON是实际的javascript ,因此我不确定您要查找的是哪种序列化。

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. “ Javascript”序列化格式似乎是第一次将日期序列化为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: 实际使用值的唯一地方是SerializeDateTime我想团队会根据链接文章确定这是一个坏主意之后才将这些方法私有化:

The first thing we tried was to inject Date constructors in the JSON string. 我们尝试的第一件事是将Date构造函数注入JSON字符串中。 This is a (very) bad idea for a number of reasons. 由于很多原因,这是一个(非常)坏主意。 First, it simply does not conform to the JSON specs. 首先,它根本不符合JSON规范。 Second, any JSON parser that validates its input before parsing it will cough on such a thing. 其次,任何在解析输入之前先验证其输入的JSON解析器都会发出类似的声音。 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. 这只会破坏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. 如果您真的只需要将单个DateTime序列化为Javascript,就没有理由不能窃取这两行代码,但是,如果要序列化一个复杂的对象,则有正当的理由,您不应该这样做。

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? 如果您需要的是在网页上实际使用ASP.NET的序列化日期,而您没有使用ASP.NET AJAX,则此问题涵盖了该问题: 如何格式化Microsoft JSON日期?

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. Javascript是一种编程语言。 JSON is a subset of Javascript that represents objects, arrays, strings, and numbers as literals which can be represented in a string. JSON是Javascript的子集 ,它表示对象,数组,字符串和数字为可以用字符串表示的文字。 All that to say that JSON is Javascript. 可以说JSON是Javascript。 If you want to serialize an object graph as Javascript, it is JSON. 如果要将对象图序列化为Javascript, 则为 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. 虽然JavaScriptSerializer可用于转义字符串或序列化DateTime,但我认为预期的用途是序列化对象图。 And, if it does escape more characters than JSON requires, I am sure it is still valid JSON. 而且,如果它确实转义了比JSON所需的字符更多的字符,那么我确信它仍然是有效的JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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