Skip to main content

module lang::json::IO

rascal-0.40.16

(De)serialization of JSON values.

Usage

import lang::json::IO;

function toJSON

str toJSON(value v)

str toJSON(value v, bool compact)

function fromJSON

&T fromJSON(type[&T] typ, str src)

function readJSON

Reads JSON values from a stream In general the translation behaves as follows: Objects translate to map[str,value] by default, unless a node is expected (properties are then translated to keyword fields) Arrays translate to lists by default, or to a set if that is expected or a tuple if that is expected. Arrays may also be interpreted as constructors or nodes (see below) Booleans translate to bools If the expected type provided is a datetime then an int instant is mapped and if a string is found then the dateTimeFormat parameter will be used to configure the parsing of a date-time string If the expected type provided is an ADT then this reader will try to "parse" each object as a constructor for that ADT. It helps if there is only one constructor for that ADT. Positional parameters will be mapped by name as well as keyword parameters. If the expected type provided is a node then it will construct a node named "object" and map the fields to keyword fields. If num, int, real or rat are expected both strings and number values are mapped If loc is expected than strings which look like URI are parsed (containing :/) or a file:/// URI is build, or if an object is found each separate field of a location object is read from the respective properties: { scheme : str, authority: str?, path: str?, fragment: str?, query: str?, offset: int, length: int, begin: [bl, bc], end: [el, ec]}.

&T readJSON(type[&T] expected, loc src, str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZZZZZ", bool lenient=false, bool trackOrigins=false)

function parseJSON

Parses JSON values from a string In general the translation behaves as the same as for Read J S ON.

&T parseJSON(type[&T] expected, str src, str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZZZZZ", bool lenient=false, bool trackOrigins=false)

function writeJSON

Writes val to the location target.

void writeJSON(loc target, value val, bool unpackedLocations=false, str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZZZZZ", bool dateTimeAsInt=false, int indent=0, bool dropOrigins=true)

If dateTimeAsInt is set to true, the dateTime values are converted to an int that represents the number of milliseconds from 1970-01-01T00:00Z. If indent is set to a number greater than 0, the JSON file will be formatted with indent number of spaces as indentation.

function asJSON

str asJSON(value val, bool unpackedLocations=false, str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZZZZZ", bool dateTimeAsInt=false, int indent = 0, bool dropOrigins=true)