Skip to main content

module lang::json::IO

rascal-0.34.0

(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 ((readJSON)).

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

function writeJSON

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)

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)