1.0.0 • Published 7 years ago

antireflection-json v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

checked conversion to/from JSON using antireflection

npm install antireflection-json

TypeScript version 2.2.1 or later is required

antireflection-json provides two functions toJSON() and fromJSON() that rely on antireflection type descriptions for doing conversion.

toJSON() will output only properties that exist in type descriptors:

import * as ar from 'antireflection';
import * as arj from 'antireflection-json';

const messageType = ar.object({
    text: ar.string,
    createdTime: ar.date
});

type Message = ar.Type<typeof messageType>;

const m = {
    text: 'abc',
    createdTime: new Date(Date.UTC(2017, 1, 1, 2, 3, 4, 0)),
    extra: 'stuff'
};

const json = arj.toJSON(messageType, m);

console.dir(json);
//{ text: 'abc', createdTime: '2017-02-01T02:03:04.000Z' }

fromJSON() will throw an exception if the value does not conform to the type:

json.createdTime = 'e';

try {
    const r = arj.fromJSON(messageType, json);
    console.dir(r);
} catch(e) {
    console.log(e.message);
    // createdTime: invalid date: e
}