Is there any serialization framework for Javascript which would retain class and reference information like Python pickles? I.e. one could directly take a prototypial inherited class instance (not just JSON-like data) and serialize it like::
是否有任何Javascript的序列化框架可以保留像Python pickles这样的类和参考信息?即可以直接采用原型继承的类实例(不仅仅是类似JSON的数据)并将其序列化为::
// Somehow add serialization metadata to classes first
obj = new MyObject();
obj.referred = new MyObject2();
pickle = serializer.dump(obj) // Provides byte stream of the serialized object
The serializer could take care of
序列化器可以照顾
-
Encoding class information in the pickle - this should be somehow customizable due to different JS class systems in existence
在pickle中编码类信息 - 由于存在不同的JS类系统,这应该以某种方式可定制
-
Automatically follow and serialize referred objects
自动跟踪和序列化引用的对象
-
Provide hooks to add custom encoders/decoders for values (dates being the most common case)
提供钩子来为值添加自定义编码器/解码器(日期是最常见的情况)
This would making internal storing and transferring of complex data structures little bit easier. I am hoping to use this in a game engine. Like with pickles, the deserialization of data would not be possible without the orignal Javascript code providing the class definitions.
这将使复杂数据结构的内部存储和传输变得更容易一些。我希望在游戏引擎中使用它。与pickle一样,如果没有提供类定义的orignal Javascript代码,则无法对数据进行反序列化。
What kind of such frameworks exist for Javascript already exist or I am going to need to roll-out a custom system?
Javascript已存在哪种类型的框架,或者我需要推出自定义系统?
5 个解决方案
#1
4
It doesn't fit perfectly but you can try to use occamsrazor.js . Doing this you can use JSON serialization:
它并不完美,但您可以尝试使用occamsrazor.js。这样做可以使用JSON序列化:
// this is your costructor function
function Circle(attrs){
this.radius = attrs.radius;
}
Circle.prototype.area = function (){
return this.radius*this.radius*Math.PI;
}
Circle.prototype.perimeter = function (){
return 2*this.radius*Math.PI;
}
// this is a validator
function hasRadius(obj){
return radius in obj;
}
// this is your factory function
objFactory = occamsrazor().addContructor(hasRadius, Circle);
// this is your deserialized object
var json = '{"radius": 10}';
// circle now is a full fledged object
var circle = objFactory(JSON.parse(json));
The drawback is that you don't get a snapshot of an object like using pickle, you recreate a new object. But it may be convenient in some circunstances.
缺点是您没有像使用pickle那样获取对象的快照,而是重新创建一个新对象。但在某些环境中可能会很方便。
#2
2
You might want to look at hunterloftis/cryo. From the readme:
你可能想看看hunterloftis / cryo。从自述文件:
Built for node.js and browsers. Cryo is inspired by Python's pickle and works similarly to JSON.stringify() and JSON.parse(). Cryo.stringify() and Cryo.parse() improve on JSON in these circumstances:
专为node.js和浏览器而构建。 Cryo的灵感来自Python的pickle,其工作方式与JSON.stringify()和JSON.parse()类似。在这些情况下,Cryo.stringify()和Cryo.parse()改进了JSON:
- Undefined
- 未定义
- Date
- 日期
- Infinity
- 无穷
- Object references
- 对象引用
- Attached properties
- 附属物
- Functions
- 功能
- DOM Nodes
- DOM节点
There is a brief discussion with the author at r/programming.
在r /编程中与作者进行了简短的讨论。
The source is straightforward, no magic.
来源很简单,没有魔力。
I haven't tried it yet.
我还没有尝试过。
#3
1
Check out msgpack. While I have not used it for JavaScript objects, the example seems to imply that it will work for objects and not just JSONs. An added bonus: it's one of the fastest implementations that I've ever used for serialization.
查看msgpack。虽然我没有将它用于JavaScript对象,但该示例似乎暗示它将适用于对象,而不仅仅是JSON。一个额外的好处:它是我用过序列化的最快的实现之一。
#4
1
http://nanodeath.github.com/HydrateJS/ (blog article at http://blog.maxaller.name/2011/01/hydratejs-smarter-javascript-serialization) seems to fit some of your requirements, judging in particular by https://github.com/nanodeath/HydrateJS/blob/master/spec/HydrateSpec.js
http://nanodeath.github.com/HydrateJS/(博客文章http://blog.maxaller.name/2011/01/hydratejs-smarter-javascript-serialization)似乎符合您的一些要求,尤其要考虑https://github.com/nanodeath/HydrateJS/blob/master/spec/HydrateSpec.js
#5
1
Here's a few:
这里有几个:
Jason: Full disclosure: I made this one. But I use it, and it is my top recommendations for complete javascript serialization. You'll probably experience a lot less pain using this one than any of the other ones, and I would be ecstatic to make additions to the library if it doesn't fit your needs! Prototype/constructor information, multiple references to the same object, has hooks for user-defined types. Saves copy of prototype unless you explicitly add the prototype to the 'constants' list, in which case it uses the already-existing prototype when rebuilding. Has JSON-style replacer and ways of referencing objects that will exist at the time of parsing. Serializes Date, RegExp, null, NaN, Infinity, -Infinity, function, DOM Element, Event, and normal objects. Still not a tried and tested library, but if it doesn't work for you, you can email me and I'll try to fix it. https://github.com/johnlarson/Jason
杰森:完全披露:我做了这个。但我使用它,这是我完整的javascript序列化的最佳建议。使用这个可能会比其他任何一个人经历更少的痛苦,如果它不符合您的需要,我会欣喜若狂地添加到库中!原型/构造函数信息,对同一对象的多个引用,具有用户定义类型的钩子。保存原型的副本,除非您将原型明确地添加到'常量'列表中,在这种情况下,它在重建时使用已经存在的原型。具有JSON样式的替换器以及引用解析时将存在的对象的方法。序列化Date,RegExp,null,NaN,Infinity,-Infinity,函数,DOM元素,事件和普通对象。仍然不是一个久经考验的图书馆,但如果它不适合你,你可以给我发电子邮件,我会尝试解决它。 https://github.com/johnlarson/Jason
ResurrectJS: Prototype/constructor information, multiple references to the same object, no hooks. Uses 'constructor' property to get constructor, and there are certain restrictions with namespaces of constructors, which you can get around with a custom namespace resolver. Has JSON-style replacer. Serializes Date, RegExp, DOM Elements, NaN, Infinity, -Infinity, and normal objects. More mature and more tested by usage than Jason, but harder to use correctly. https://github.com/skeeto/resurrect-js
ResurrectJS:原型/构造函数信息,对同一对象的多个引用,无钩子。使用'constructor'属性来获取构造函数,并且构造函数的命名空间有一些限制,您可以使用自定义命名空间解析器来解决这些限制。有JSON风格的替代品。序列化Date,RegExp,DOM Elements,NaN,Infinity,-Infinity和普通对象。使用比Jason更成熟和更多测试,但更难正确使用。 https://github.com/skeeto/resurrect-js
SnapShot: I haven't tried this one. It looks promising, but was last updated 3 years ago. No automatic prototype/constructor information, as far as I can tell, but it has a hook api for defining custom serialization/deserialization behavior similar to JSON's toJSON and fromJSON. Handles multiple references to the same object. No JSON-style replacer, as far as I can tell. Serializes Date, RegExp, null, and function. https://github.com/mixu/snapshot
SnapShot:我没试过这个。它看起来很有希望,但最近更新于3年前。据我所知,没有自动原型/构造函数信息,但它有一个钩子api,用于定义类似于JSON的toJSON和fromJSON的自定义序列化/反序列化行为。处理对同一对象的多个引用。据我所知,没有JSON风格的替代品。序列化Date,RegExp,null和函数。 https://github.com/mixu/snapshot
JASON: Another promising library I haven't tried that was last updated 3 years ago. I don't know whether it does prototype/constructors. Handles multiple reference to same object. As far as I know, it doesn't have hooks. I don't know whether it has a JSON-style replacer. Serializes 'Dates, RegExps, Booleans, etc.' and 'all JS primitives, including undefined'. https://github.com/xk/JASON
JASON:我没有尝试过的另一个有前途的图书馆是3年前的最新更新。我不知道它是否是原型/构造函数。处理对同一对象的多个引用。据我所知,它没有钩子。我不知道它是否有JSON风格的替换器。序列化'日期,RegExps,布尔等等'和'所有JS原语,包括undefined'。 https://github.com/xk/JASON
#1
4
It doesn't fit perfectly but you can try to use occamsrazor.js . Doing this you can use JSON serialization:
它并不完美,但您可以尝试使用occamsrazor.js。这样做可以使用JSON序列化:
// this is your costructor function
function Circle(attrs){
this.radius = attrs.radius;
}
Circle.prototype.area = function (){
return this.radius*this.radius*Math.PI;
}
Circle.prototype.perimeter = function (){
return 2*this.radius*Math.PI;
}
// this is a validator
function hasRadius(obj){
return radius in obj;
}
// this is your factory function
objFactory = occamsrazor().addContructor(hasRadius, Circle);
// this is your deserialized object
var json = '{"radius": 10}';
// circle now is a full fledged object
var circle = objFactory(JSON.parse(json));
The drawback is that you don't get a snapshot of an object like using pickle, you recreate a new object. But it may be convenient in some circunstances.
缺点是您没有像使用pickle那样获取对象的快照,而是重新创建一个新对象。但在某些环境中可能会很方便。
#2
2
You might want to look at hunterloftis/cryo. From the readme:
你可能想看看hunterloftis / cryo。从自述文件:
Built for node.js and browsers. Cryo is inspired by Python's pickle and works similarly to JSON.stringify() and JSON.parse(). Cryo.stringify() and Cryo.parse() improve on JSON in these circumstances:
专为node.js和浏览器而构建。 Cryo的灵感来自Python的pickle,其工作方式与JSON.stringify()和JSON.parse()类似。在这些情况下,Cryo.stringify()和Cryo.parse()改进了JSON:
- Undefined
- 未定义
- Date
- 日期
- Infinity
- 无穷
- Object references
- 对象引用
- Attached properties
- 附属物
- Functions
- 功能
- DOM Nodes
- DOM节点
There is a brief discussion with the author at r/programming.
在r /编程中与作者进行了简短的讨论。
The source is straightforward, no magic.
来源很简单,没有魔力。
I haven't tried it yet.
我还没有尝试过。
#3
1
Check out msgpack. While I have not used it for JavaScript objects, the example seems to imply that it will work for objects and not just JSONs. An added bonus: it's one of the fastest implementations that I've ever used for serialization.
查看msgpack。虽然我没有将它用于JavaScript对象,但该示例似乎暗示它将适用于对象,而不仅仅是JSON。一个额外的好处:它是我用过序列化的最快的实现之一。
#4
1
http://nanodeath.github.com/HydrateJS/ (blog article at http://blog.maxaller.name/2011/01/hydratejs-smarter-javascript-serialization) seems to fit some of your requirements, judging in particular by https://github.com/nanodeath/HydrateJS/blob/master/spec/HydrateSpec.js
http://nanodeath.github.com/HydrateJS/(博客文章http://blog.maxaller.name/2011/01/hydratejs-smarter-javascript-serialization)似乎符合您的一些要求,尤其要考虑https://github.com/nanodeath/HydrateJS/blob/master/spec/HydrateSpec.js
#5
1
Here's a few:
这里有几个:
Jason: Full disclosure: I made this one. But I use it, and it is my top recommendations for complete javascript serialization. You'll probably experience a lot less pain using this one than any of the other ones, and I would be ecstatic to make additions to the library if it doesn't fit your needs! Prototype/constructor information, multiple references to the same object, has hooks for user-defined types. Saves copy of prototype unless you explicitly add the prototype to the 'constants' list, in which case it uses the already-existing prototype when rebuilding. Has JSON-style replacer and ways of referencing objects that will exist at the time of parsing. Serializes Date, RegExp, null, NaN, Infinity, -Infinity, function, DOM Element, Event, and normal objects. Still not a tried and tested library, but if it doesn't work for you, you can email me and I'll try to fix it. https://github.com/johnlarson/Jason
杰森:完全披露:我做了这个。但我使用它,这是我完整的javascript序列化的最佳建议。使用这个可能会比其他任何一个人经历更少的痛苦,如果它不符合您的需要,我会欣喜若狂地添加到库中!原型/构造函数信息,对同一对象的多个引用,具有用户定义类型的钩子。保存原型的副本,除非您将原型明确地添加到'常量'列表中,在这种情况下,它在重建时使用已经存在的原型。具有JSON样式的替换器以及引用解析时将存在的对象的方法。序列化Date,RegExp,null,NaN,Infinity,-Infinity,函数,DOM元素,事件和普通对象。仍然不是一个久经考验的图书馆,但如果它不适合你,你可以给我发电子邮件,我会尝试解决它。 https://github.com/johnlarson/Jason
ResurrectJS: Prototype/constructor information, multiple references to the same object, no hooks. Uses 'constructor' property to get constructor, and there are certain restrictions with namespaces of constructors, which you can get around with a custom namespace resolver. Has JSON-style replacer. Serializes Date, RegExp, DOM Elements, NaN, Infinity, -Infinity, and normal objects. More mature and more tested by usage than Jason, but harder to use correctly. https://github.com/skeeto/resurrect-js
ResurrectJS:原型/构造函数信息,对同一对象的多个引用,无钩子。使用'constructor'属性来获取构造函数,并且构造函数的命名空间有一些限制,您可以使用自定义命名空间解析器来解决这些限制。有JSON风格的替代品。序列化Date,RegExp,DOM Elements,NaN,Infinity,-Infinity和普通对象。使用比Jason更成熟和更多测试,但更难正确使用。 https://github.com/skeeto/resurrect-js
SnapShot: I haven't tried this one. It looks promising, but was last updated 3 years ago. No automatic prototype/constructor information, as far as I can tell, but it has a hook api for defining custom serialization/deserialization behavior similar to JSON's toJSON and fromJSON. Handles multiple references to the same object. No JSON-style replacer, as far as I can tell. Serializes Date, RegExp, null, and function. https://github.com/mixu/snapshot
SnapShot:我没试过这个。它看起来很有希望,但最近更新于3年前。据我所知,没有自动原型/构造函数信息,但它有一个钩子api,用于定义类似于JSON的toJSON和fromJSON的自定义序列化/反序列化行为。处理对同一对象的多个引用。据我所知,没有JSON风格的替代品。序列化Date,RegExp,null和函数。 https://github.com/mixu/snapshot
JASON: Another promising library I haven't tried that was last updated 3 years ago. I don't know whether it does prototype/constructors. Handles multiple reference to same object. As far as I know, it doesn't have hooks. I don't know whether it has a JSON-style replacer. Serializes 'Dates, RegExps, Booleans, etc.' and 'all JS primitives, including undefined'. https://github.com/xk/JASON
JASON:我没有尝试过的另一个有前途的图书馆是3年前的最新更新。我不知道它是否是原型/构造函数。处理对同一对象的多个引用。据我所知,它没有钩子。我不知道它是否有JSON风格的替换器。序列化'日期,RegExps,布尔等等'和'所有JS原语,包括undefined'。 https://github.com/xk/JASON