Find a javascript library for serialization & deserialization objects in JavaScript with property support

前端 未结 1 425
时光说笑
时光说笑 2021-01-26 04:10

Consider following class in JavaScript:

Tools.UserBase = Tools.Class.define(\"Tools.UserBase\", Tools.EntityBase, {
    UserId: { type: System.Int32, key: true,          


        
1条回答
  •  情歌与酒
    2021-01-26 04:56

    And I've no control on serialization process at all.

    Yes you do. Give you class a toJSON method that returns an object with the expected properties and it will be recognized by JSON.stringify.

    The deserialization process is the same, JSON.parse will create a plain object instead of typed objects.

    Because JSON does represent plain objects, that is only natural :-) Still, you can pass a reviver function to JSON.parse, which can manipulate the returned objects and exchange them for class instances. For convenience, add a (static) fromJSON function to your class that you can use in the reviver.

    Is there any JavaScript library which fits my needs?

    Most MVC JavaScript libraries have built-in serialisation/deserialisation methods for their models. You can either use one of them or get inspired by their code to extend your Tools.Class suite.

    0 讨论(0)
提交回复
热议问题