为什么会有JConstructor? [英] Why is there a JConstructor?

查看:96
本文介绍了为什么会有JConstructor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Json.NET定义了 JConstructor类型.

Json.NET defines a JConstructor type.

这令人困惑,因为(据我所知)构造函数不是JSON的一部分.我仔细检查了 JSON规范,浏览了 json.org ,但找不到任何内容.在网络上的任何地方,似乎也没有太多关于这种类型的文档.

This is confusing, because (to the best of my knowledge) constructors are not part of JSON. I double-checked the JSON spec and browsed json.org but couldn't find anything. There also doesn't seem to be much documentation about this type anywhere on the web.

由于Json.NET的使用如此广泛(甚至由Microsoft共同签署),我认为必须有某种合理的动机将这种表示形式包括在对象模型中.问题是,就我而言,试图确定动机的任何尝试都不过是猜测.

Because Json.NET is so widely used (it is even cosigned by Microsoft) I assume there has to be some reasonable motivation for including this representation in the object model. The problem is, any attempt on my part to determine that motivation is nothing but speculation.

我测试了类型及其序列化,明显的行为是只包装JavaScript代码,例如new constructorName(...),例如:

I tested out the type and its serialization, and the apparent behavior is to just wrap JavaScript code such as new constructorName(...), e.g.:

new JConstructor("ctorExample", 
    new JValue("value"), 
    new JObject(
        new JProperty("prop1", new JValue(1)), 
        new JProperty("prop2", new JValue(2)))
        )
.ToString()

输出

 new ctorExample( 
   "value", 
   { 
     "prop1": 1, 
     "prop2": 2 
   } 
 ) 

那么,JConstructor类型打算代表什么,为什么会存在?

So, what is the JConstructor type intended to represent and why does it exist?

推荐答案

Json.NET包含许多功能,这些功能不是JSON规范的一部分.特别是,它允许解析正式"无效的一些JSON文件.这包括未引用的属性,注释,构造函数等.它包括引用的序列化和许多其他功能.某些功能,例如注释或未引用的属性,稍后可以成为标准的一部分.

Json.NET includes many features which are not part of JSON specification. In particular, it allows parsing some JSON files which are "officially" invalid. This includes unquoted properties, comments, constructors etc. It includes serialization of references and many other features. Some features, like comments or unquoted properties, can later become part of the standard.

JConstructor允许产生要由JavaScript应用程序使用的代码.以这种方式序列化的数据是无效的JSON,但是有效的JavaScript代码.无法使用JSON.parse方法解析此类JSON文件,但eval将能够处理它们.在某些情况下,它可能有用,但可能是不好的作法,主要用于与现有JS脚本向后兼容,因此,这可能是它不作广告的原因.

JConstructor allows producing code to be used by JavaScript apps. Data serialized this way is invalid JSON, but valid JavaScript code. Such JSON files cannot be parsed using JSON.parse method, but eval will be able to handle them. In some cases it may be useful, it's probably bad practice though, mostly useful for backwards compatibility with existing JS scripts, so this is probably the reason it's not advertised.

这篇关于为什么会有JConstructor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆