The JSONUtil
module defines a collection of utility functions for
working with JSON values. These include operations for testing
if a value is of a given type and navigating the structure of a
JSON value.
Synopsis
structure JSONUtil
Interface
exception NotBool of JSON.value
exception NotInt of JSON.value
exception NotNumber of JSON.value
exception NotString of JSON.value
exception NotObject of JSON.value
exception FieldNotFound of JSON.value * string
exception NotArray of JSON.value
exception ArrayBounds of JSON.value * int
exception ElemNotFound of JSON.value
val exnMessage : exn -> string
val asBool : JSON.value -> bool
val asInt : JSON.value -> Int.int
val asIntInf : JSON.value -> IntInf.int
val asNumber : JSON.value -> Real.real
val asString : JSON.value -> string
val findField : JSON.value -> string -> JSON.value option
val lookupField : JSON.value -> string -> JSON.value
val hasField : string -> JSON.value -> bool
val testField : string -> (JSON.value -> bool) -> JSON.value -> bool
val asArray : JSON.value -> JSON.value vector
val arrayMap : (JSON.value -> 'a) -> JSON.value -> 'a list
datatype edge
= SEL of string
| SUB of int
| FIND of JSON.value -> bool
type path = edge list
val get : JSON.value * path -> JSON.value
val replace : JSON.value * path * JSON.value -> JSON.value
val insert : JSON.value * path * string * JSON.value -> JSON.value
val append : JSON.value * path * JSON.value list -> JSON.value
Description
exception NotBool of JSON.value
-
raised by the
asBool
function when the argument is not a JSON boolean. exception NotInt of JSON.value
-
raised by the
asInt
andasIntInf
functions when the argument is not a JSON integer number. exception NotNumber of JSON.value
-
raised by the
asNumber
function when the argument is not a JSON number. exception NotString of JSON.value
-
raised by the
asString
function when the argument is not a JSON string. exception NotObject of JSON.value
-
raised by the
findField
andlookupField
functions when the argument is not a JSON object. exception FieldNotFound of JSON.value * string
-
This exception is raised when the given field is not found in an object.
exception NotArray of JSON.value
-
This exception is raised when trying to process a non-array value as an array.
exception ArrayBounds of JSON.value * int
-
This exception is raised when access to an array value is out of bounds.
exception ElemNotFound of JSON.value
-
This exception is raised when there is no element of an array that satisfies the predicate of a
FIND
edge in a path. The argument will be the array in question. val exnMessage : exn -> string
-
exnMessage exn
returns an error-message string for the exception valueexn
. This function produces specialized messages for the exceptions defined in theJSONUtil
structure and falls back to the General.exnMessage function for other exceptions. val asBool : JSON.value -> bool
-
asBool (JSON.BOOL b)
returns the valueb
. This function raises theNotBool
exception if the value is not a JSON Boolean value. val asInt : JSON.value -> int
-
asInt (JSON.INT n)
returns the valuen
converted toint
. This function raises theNotInt
exception if the value is not a JSON integer value. It may also raise theOverflow
exception ifn
is too large for the defaultint
type. val asIntInf : JSON.value -> IntInf.int
-
asIntInf (JSON.INT n)
returns the valuen
. This function raises theNotInt
exception if the value is not a JSON integer value. val asNumber : JSON.value -> Real.real
-
asNumber jv
converts the JSON numberjv
to an SMLreal
value. Thejv
argument can either have the formJSON.INT n
, in which casen
is converted to thereal
type and returned, orJSON.FLOAT f
, in which casef
is returned; otherwise, theNotNumber
exception is raised. val asString : JSON.value -> string
-
asBool (JSON.STRING s)
returns the values
. This function raises theNotString
exception if the value is not a JSON string value. val findField : JSON.value -> string -> JSON.value option
-
findField (JSON.OBJECT flds) key
returnsSOME jv
when the list of fieldsflds
contains(key, jv)
andNONE
otherwise. IffindField
is called on a value that is not a JSON object, then it raises theNotObject
exception. val lookupField : JSON.value -> string -> JSON.value
-
lookupField (JSON.OBJECT flds) key
returnsjv
when the list of fieldsflds
contains(key, jv)
and raises theFieldNotFound
exception otherwise. IflookupField
is called on a value that is not a JSON object, then it raises theNotObject
exception. val hasField : string -> JSON.value -> bool
-
hasField key v
returnstrue
when the valuev
is a JSON object that has a field withkey
as its label andfalse
otherwise. val testField : string -> (JSON.value -> bool) -> JSON.value -> bool
-
testField key pred v
returns the result ofpred jv
when the valuev
is a JSON object that contains(key, jv)
. It returnsfalse
otherwise. val asArray : JSON.value -> JSON.value vector
-
asArray jv
converts the JSON array valuejv
to an SML vector value. It raises theNotArray
exception whenjv
is not a JSON array. val arrayMap : (JSON.value -> 'a) -> JSON.value -> 'a list
-
map a conversion function over a JSON array to produce a list; this function raises the
NotArray
exception if the second argument is not an array. datatype edge = …
-
specifies an edge of a path into a JSON value. The constructors have the following meaning:
SEL of string
-
SEL key
specifies the value labeled bykey
in a JSON object. SUB of int
-
SUB i
specifies thei
th element of a JSON array. FIND of JSON.value -> bool
-
FIND pred
specifies the first element of a JSON array that satisfies the given predicate.
type path = edge list
-
specifies a path into a JSON value.
val get : JSON.value * path -> JSON.value
-
get (jv, path)
returns the component ofjv
named bypath
. It raises one of theNotObject
,NotArray
,FieldNotFound
, orElemNotFound
exceptions if there is an inconsistency between the path and the structure ofjv
.