1.2.1 โ€ข Published 7 months ago

recjson v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

RecJSON ๐Ÿ”„๐Ÿ”ข

๐Ÿ“ Documentation ๐Ÿ“ฆ NPM ๐Ÿงช Tests

๐Ÿ”„ JSON-based serialization with the ability to have circular/infinitely-recursive references ๐Ÿ”—

๐Ÿ“ฆ Table of Contents

๐Ÿš€ Setup

pnpm i recjson

๐Ÿ› ๏ธ Usage

๐Ÿ“ค Serialization

import { Serializer } from 'recjson';
const serializer = new Serializer();

const recursiveObject = {
  a: 1,
  b: {
    c: {
      d: {
        backToTop: null as unknown as any,
      },
    },
  },
};

recursiveObject.b.c.d.backToTop = recursiveObject;

const serialized = serializer.serialize(recursiveObject);
// { root: 0, obj: [ { a: 1, b: 2 }, 1, { c: 3 }, { d: 4 }, { backToTop: 0 } ] } - As a JS Object

const serializedAsString = serializer.serializeToJSON(recursiveObject);
// '{"root":0,"obj":[{"a":1,"b":2},1,{"c":3},{"d":4},{"backToTop":0}]}'

๐Ÿ“ฅ Deserialization/Parsing

import { Deserializer } from 'recjson';
const deserializer = new Deserializer();

const serialized = { root: 0, obj: [ { a: 1, b: 2 }, 1, { c: 3 }, { d: 4 }, { backToTop: 0 } ] }; // As a JS Object

const deserialized = deserializer.parse(serialized); // { a: 1, b: { c: { d: [Circular] } } }

const deserializedAsString = parser.parseFromJSON('{"root":0,"obj":[{"a":1,"b":2},1,{"c":3},{"d":4},{"backToTop":0}]}'); // { a: 1, b: { c: { d: [Circular] } } }

๐Ÿง™โ€โ™‚๏ธ๐Ÿ”ฎ What kind of sorcery is this?

๐Ÿ”„๐Ÿ“ RecJSON is a JSON-based serialization format that allows for circular/infinitely-recursive references. It does this by using a special syntax for references.

๐Ÿ”ข๐Ÿ”„ This syntax involves using an array of all objects, each with integer values, alongside primitives. When an object is referenced, it is replaced with the integer value of its index in the array. If it doesn't exist, it is added to the array.

๐Ÿ”„๐Ÿ”„ This allows for circular/infinitely-recursive references, as the object is only referenced by its index in the array, not by the object itself.

๐Ÿ”„๐Ÿ“ฅ When deserializing, the array is used to replace the integer values with the actual objects.

๐Ÿ“œ License

This project is licensed under the ๐Ÿ“„ MIT License

1.2.0

7 months ago

1.2.2-rc1

7 months ago

1.2.1

7 months ago

1.1.0

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago