1.2.3 • Published 5 years ago

typedjson-fork v1.2.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Build Status

Example & how to use

There are no publicly available, dedicated docs yet for 1.0, but most methods are commented nicely, and here's a quick example on how to serialize various types (I recommend using reflect-metadata in your project, so you don't have to manually annotate the type of @jsonMember properties twice, see at the bottom of this page):

@jsonObject
class MyDataClass
{
    // Primitives serialization
    @jsonMember
    public prop1: number; // or string, boolean, etc.

    // Array serialization
    @jsonArrayMember(Number)
    public arrayProp: number[];

    // Map serialization
    @jsonMapMember(Number, String)
    public mapProp: Map<number, string>;

    // Set serialization
    @jsonSetMember(Number)
    public setProp: Set<number>;
}

Of course, all 4 serialization techniques (single, array, map, set) support nested objects (nested object class must be also decorated with @jsonObject for this to work, obviously). Example:

@jsonObject
class MySecondDataClass
{
    @jsonMember
    public prop1: number;

    @jsonMember
    public prop2: number;
}

@jsonObject
class MyDataClass
{
    @jsonMember
    public prop1: MySecondDataClass;
    
    @jsonArrayMember(MySecondDataClass)
    public arrayProp: MySecondDataClass[];

    @jsonMapMember(Number, MySecondDataClass)
    public mapProp: Map<number, MySecondDataClass>;
}

Additionally, there's built-in support for TypedArray objects (serialized as number[]), Date, ArrayBuffer (serialized as string at this time, so this might not be a good idea, prefer using a TypedArray instead), this is available by simply using @jsonMember. Serialization of Maps, Sets, and Arrays of root objects is also supported.

After annotating your objects as shown above, you simply consume them by creating a new TypedJSON object, supplying the constructor of the root data type to it:

let object = new MyDataClass(); // ...
let serializer = new TypedJSON(MyDataClass);

let json = serializer.stringify(object);
let object2 = serializer.parse(json);

How are these objects serialized?

Sets and arrays are simply serialized as arrays, Maps are serialized as arrays of key-value-pair objects, TypedArrays are serialized as numeric arrays.

How to use without reflect-metadata?

If you don't use reflect-metadata, you need to manually add the constructor reference to @jsonMember, e.g.:

@jsonObject
class MyDataClass
{
-   @jsonMember
+   @jsonMember({ constructor: Number })
    public prop1: number;

-   @jsonMember
+   @jsonMember({ constructor: MySecondDataClass })
    public prop2: MySecondDataClass;
}

This is not needed for @jsonArrayMember, @jsonMapMember, and @jsonSetMember, as those types already know the property type itself, as well as element/key types (although using reflect-metadata adds runtime-type checking to these decorators, to help you spot errors).

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.2.0-rc1

6 years ago

1.1.0

6 years ago

1.1.0-rc3

6 years ago

1.1.0-rc2

6 years ago

1.1.0-rc1

6 years ago

1.0.0-rc1

6 years ago