Type-safe JSON parsing/formatting

This project is part of Literium WEB-framework but can be used standalone.
Why
Usually in JavaScript we use functions like JSON.parse()
and JSON.stringify()
to exchange datas between application and world.
This way is quite simple but haven't allows to prevent expensive runtime errors related to inconsistency between data and model.
To solve this problem historically used three approaches:
- Validating data consistency manually
- Validating data using predefined schema
- Using parser combinators to get a valid data
The first approach needs too many hand-written code for complex models and suggested to use for simple checks only.
The second approach is powerful and preferred to use for validating complex models but not so convenient for applying within statically-typed languages.
The third approach is used in libraries like serde (Rust) and Aeson (Haskell) and so powerful as second but also works fine with automatic type inherence.
How
Each data type have corresponded JsonType
interface which implements parser and builder.
The parser gets untyped data do internal validation and returns JsonResult
which is either the data of corresponded type or an error string.
The builder gets typed data and like the parser after some checks returns JsonResult
which is either the untyped data or an error string.
API basics
The proposed API have two class of functions: the first works with string JSON representation, the second operates with untyped JS data (i.e. with any
type).
import { JsonType, build, parse, build_js, parse_js } from 'literium-json';
const json_model: JsonType<JSType>;
build(json_model)()
build_js(json_model)()
parse(json_model)("json-data")
parse_js(json_model)()
Model algebra
Atomic types
Basic types
There are some basic atomic types corresponded to JSON data model:
str
- JSON stringnum
- JSON numberbin
- JSON booleanund
- JSON null
import { str, num, build, parse } from 'literium-json';
parse(str)(`"abc"`)
parse(str)(`123`)
parse(num)(`123`)
parse(num)(`"abc"`)
build(str)("abc")
build(str)(123 as any)
build(num)(123)
build(num)("abc" as any)
Numeric types
The set of useful numeric types allows you to get more strict numbers validation:
fin
- finite numberspos
- positive numbersneg
- negative numbersint
- integer numbersnat
- natural numbers
import { nat, build, parse } from 'literium-json';
parse(nat)(`123`)
parse(nat)(`-123`)
parse(nat)(`12.3`)
parse(nat)(`"abc"`)
build(nat)(123)
build(nat)(-123)
build(nat)(12.3)
Container types
List combinator
The list
container corresponds to JSON array type.
import { str, list, parse, build } from 'literium-json';
const args = list(str);
parse(args)(`["arg1","arg2","arg3"]`)
parse(args)(`[]`)
parse(args)(`{}`)
parse(args)(`"arg"`)
build(args)(["arg1", "arg2", "arg3"])
build(args)({} as any)
build(args)("arg" as any)
Dictionary combinator
The dict
container corresponds to JSON object type.
import { str, num, dict, parse, build } from 'literium-json';
const opts = dict({ a: str, b: num });
parse(opts)(`{"a":"abc","b":123}`)
parse(opts)(`["a","b"]`)
parse(opts)(`{}`)
parse(opts)(`{"a":123}`)
parse(opts)(`{"a":"abc"}`)
build(opts)({a:"abc",b:123})
build(opts)("a" as any)
build(opts)({} as any)
build(opts)({a:123} as any)
build(opts)({a:"abc"} as any)
Tuple combinator
In some cases we prefer to use tuples instead of dictionaries.
import { str, num, tup, parse, build } from 'literium-json';
const args = tup(str, num);
parse(args)(`["abc",123]`)
parse(args)(`{"a":"abc","b":123}`)
parse(args)(`["abc"]`)
parse(args)(`["abc",123,true]`)
parse(args)(`[123,"abc"]`)
parse(args)(`["abc",null]`)
build(args)(["abc", 123])
build(args)("a" as any)
build(args)([] as any)
build(args)([1,2,3] as any)
build(args)([123, "abc"] as any)
build(args)(["abc",false] as any)
Type modifiers
Alternatives
Of course you can combine some number of types which can be used alternatively.
import { num, str, alt, parse, build } from 'literium-json';
const son = alt(str, num);
parse(son)(`"abc"`)
parse(son)(`123`)
parse(son)(`true`)
parse(son)(`[]`)
build(son)("abc")
build(son)(123)
build(son)(true as any)
build(son)([] as any)
Optional
The opt()
is useful for defining an optional values in model.
import { str, opt, parse, build } from 'literium-json';
const so = opt(str);
parse(so)(`"abc"`)
parse(so)(`null`)
parse(so)(`123`)
parse(so)(`true`)
parse(so)(`[]`)
build(so)("abc")
build(so)(undefined)
build(so)(123 as any)
build(so)(true as any)
build(so)([] as any)
Option
The option()
like the opt()
but works with literium's Option<T>
type.
import { str, option, parse, build } from 'literium-json';
const so = option(str);
parse(so)(`"abc"`)
parse(so)(`null`)
parse(so)(`123`)
parse(so)(`true`)
parse(so)(`[]`)
build(so)(some("abc"))
build(so)(none())
Defaults
Also you can add the optional values with default values using def()
.
import { str, def, parse, build } from 'literium-json';
const sd = def("def")(str);
parse(sd)(`"abc"`)
parse(sd)(`null`)
parse(sd)(`123`)
parse(sd)(`[]`)
build(sd)("abc")
build(sd)("def")
build(sd)(123 as any)
build(sd)([] as any)
Constant
Use val()
to add some constant value into model.
import { str, val, dict, parse, build } from 'literium-json';
const d = dict({
a: str,
b: val(123),
});
parse(d)(`{"a":"abc"}`)
parse(d)(`{"a":"abc","b":456}`)
parse(d)(`{}`)
build(d)({a:"abc"})
build(d)({a:"abc",b:123})
build(d)({})
Value mapping
In some cases you need simply to change the type of value or modify value but you would like to avoid implementing new parser. You can use mapping like bellow:
import { int, map, parse, build } from 'literium-json';
const start_from_one = map(
(v: number) => v + 1,
(v: number) => v - 1
);
const idx = start_from_one(int);
parse(idx)(`0`)
parse(idx)(`9`)
build(idx)(1)
build(idx)(10)
Advanced validation
In some advanced cases you need apply some extra validation to already parsed values. You can do it with then()
like so:
import { ok, err } from 'literium';
import { int, then, parse, build } from 'literium-json';
const validate_even = then(
(v: number) => v % 2 ? err('odd') : ok(v),
(v: number) => v % 2 ? err('odd') : ok(v),
);
const even = validate_even(int);
parse(even)(`0`)
parse(even)(`9`)
build(even)(0)
build(even)(9)
Type chaining
And of course you can chaining types using chain()
.
Let's rewrite the above example using this technique:
import { ok, err } from 'literium';
import { int, chain, parse, build } from 'literium-json';
const even_t: Type<number> = {
(v: number) => v % 2 ? err('odd') : ok(v),
(v: number) => v % 2 ? err('odd') : ok(v),
);
const even = chain(even_t)(int);
parse(even)(`0`)
parse(even)(`9`)
build(even)(0)
build(even)(9)
User-defined types and combinators
Custom type
One of the more helpful feature of this library is the possibility to define your own data types with fully customized validation.
For example, suppose we have enum type Order which has two values: Asc and Desc. We can simply use it in our data models when we defined corresponding JsonType for it.
import { ok, err } from 'literium';
import { str, parse, build, JsonType } from 'literium-json';
export const enum Order { Asc, Desc }
export const ord: JsonType<Order> = {
p(v) {
const s = str.p(v);
return !s.$ ? err(s._) : s._ == 'asc' ? ok(Order.Asc) : s._ == 'desc' ? ok(Order.Desc) : err("!'asc' & !'desc'");
},
b(v) {
return v === Order.Asc ? ok('asc') : v === Order.Desc ? ok('desc') : err("!Order");
},
};
parse(ord)(`"asc"`)
parse(ord)(`"desc"`)
parse(ord)(`"abc"`)
parse(ord)(`123`)
build(ord)(Order.Asc)
build(ord)(Order.Desc)
build(ord)(123)
build(ord)("abc")
Custom combinator
The example below demonstrates how to create custom combinator.
import { ok, err } from 'literium';
import { str, num, parse, build, JsonType } from 'literium-json';
export interface Pair<Key, Value> { $: Key, _: Value }
export function pair<Key, Value>(
tk: JsonType<Key>,
tv: JsonType<Value>
): JsonType<Pair<Key, Value>> {
return {
p(x) {
if (typeof x != 'object' ||
Array.isArray(x) ||
x == null) return err('!pair');
const k = tk.p(x.$);
if (!k.$) return err(`#key ${k._}`);
const v = tv.p(x._);
if (!v.$) return err(`#value ${v._}`);
return ok({$: k._, _: v._});
},
b(x) {
if (typeof x != 'object' ||
Array.isArray(x) ||
x == null) return err('!pair');
const k = tk.b(x.$);
if (!k.$) return err(`#key ${k._}`);
const v = tv.b(x._);
if (!v.$) return err(`#value ${v._}`);
return ok({$: k._, _: v._});
}
};
}
const snp = pair(str, num);
parse(snp)(`{"$":"abc","_":123}`)
parse(snp)(`["abc",123]`)
parse(snp)(`null`)
parse(snp)(`{"_":123}`)
parse(snp)(`{"$":123}`)
parse(snp)(`{"$":"abc","_":true}`)
build(snp)({$: "abc", _: 123})
build(snp)(["abc",123] as any)
build(snp)(null as any)
build(snp)({_: 123})
build(snp)({$: 123})
build(snp)({$: "abc", _: true})