项目作者: avocode

项目描述 :
Immutable.JS structure-aware JSON serializer/deserializer
高级语言: JavaScript
项目地址: git://github.com/avocode/json-immutable.git
创建时间: 2016-08-27T15:38:37Z
项目社区:https://github.com/avocode/json-immutable

开源协议:MIT License

下载


JsonImmutable

Build Status

Immutable.JS structure-aware JSON serializer/deserializer built around the native JSON API.

Motivation

By using the native JSON API, Immutable.JS structures are serialized as plain objects and arrays with no type information. The goal was to preserve both Immutable.JS Iterable types (maps, lists, etc.) and Record types. immutable.Map supports and preserves key types while plain JavaScript object keys are coerced to strings. These types should also preserved.

Usage

Plain Objects and Primitive Types

  1. const data = { 'a': 'b', 'c': 123, 'd': true }
  2. // Serialize
  3. const json = serialize(data)
  4. // json == '{"a":"b","c":123,"d":true}'
  5. // Deserialize
  6. const result = deserialize(json)

Native Object Types

  1. const data = { 'created_at': new Date('2016-09-08'), 'pattern': /iamnative/g }
  2. // Serialize
  3. const json = serialize(data)
  4. // json == '{"created_at":{"__date":"2016-09-08T00:00:00Z"},"pattern":{"__regexp":"/iamnative/g"}}'
  5. // Deserialize
  6. const result = deserialize(json)

Immutable Records

  1. const SampleRecord = immutable.Record(
  2. { 'a': 3, 'b': 4 },
  3. 'SampleRecord'
  4. )
  5. const data = {
  6. 'x': SampleRecord({ 'a': 5 }),
  7. }
  8. // Serialize
  9. const json = serialize(data)
  10. // json == '{"x":{"__record":"SampleRecord","data":{"a":5}}}'
  11. // Deserialize
  12. const result = deserialize(json, {
  13. recordTypes: {
  14. 'SampleRecord': SampleRecord
  15. }
  16. })

Record types can be named. This is utilized by the serializer/deserializer to revive immutable.Record objects. See the SampleRecord name passed into immutable.Record() as the second argument.

NOTE: When an unknown record type is encountered during deserialization, an error is thrown.

General Immutable Structures

  1. const data = {
  2. 'x': immutable.Map({
  3. 'y': immutable.List.of(1, 2, 3)
  4. }),
  5. }
  6. // Serialization
  7. const json = serialize(data)
  8. // json == '{"x":{"__iterable":"Map","data":[["y",{"__iterable":"List","data":[1,2,3]"}]]}}'
  9. // Deserialize
  10. const result = deserialize(json)
  • Immutable structures, plain objects and primitive data can be safely composed together.

  • immutable.Map key type information is preserved as opposed to the bare JSON API.

NOTE: When an unknown Immutable iterable type is encountered during deserialization, an error is thrown. The supported types are List, Map, OrderedMap, Set, OrderedSet and Stack.

API

  • serialize()

    Arguments:

    • data: The data to serialize.
    • options={}: Serialization options.

      • pretty=false: Whether to pretty-print the result (2 spaces).

      Return value:

    • string: The JSON representation of the input (data).

  • deserialize()

    Arguments:

    • json: A JSON representation of data.
    • options={}: Deserialization options.

      • recordTypes={}: immutable.Record factories.

      Return value:

    • any: Deserialized data.

Streaming API

  • createSerializationStream()

    Arguments:

    • data: The data to serialize.
    • options={}: Serialization options.

      • pretty=false: Whether to pretty-print the result (2 spaces).
      • bigChunks=false: Whether the serialized data should only be split into chunks based on the reader speed. By default, each data structure level is processed in its own event loop microtask which.
        • NOTE: When bigChunks=true, a (possibly substantial) portion of the data is serialized synchronously.

      Return value:

    • stream.PassThrough<!Buffer>: A readable stream emitting the JSON representation of the input (data).

Running Tests

  1. Clone the repository.
  2. npm install
  3. npm test