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 JSONError of exn * JSON.value
exception NotBool
exception NotInt
exception NotNumber
exception NotString
exception NotObject
exception FieldNotFound of string
exception NotArray
exception ArrayBounds of int
exception ElemNotFound
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 JSONError of exn * JSON.value-
raised when an error is detected during processing of a JSON value. The first argument, which is one of the exceptions described below, details the type of error and the second argument is the value that was being processed at the point of the error.
exception NotBool of JSON.value-
used by the
asBoolfunction when the argument is not a JSON boolean. exception NotInt of JSON.value-
used by the
asIntandasIntInffunctions when the argument is not a JSON integer number. exception NotNumber of JSON.value-
used by the
asNumberfunction when the argument is not a JSON number. exception NotString of JSON.value-
used by the
asStringfunction when the argument is not a JSON string. exception NotObject of JSON.value-
used by the
findFieldandlookupFieldfunctions when the argument is not a JSON object. exception FieldNotFound of JSON.value * string-
This exception is used when the given field is not found in an object.
exception NotArray of JSON.value-
This exception is used when trying to process a non-array value as an array.
exception ArrayBounds of JSON.value * int-
This exception is used when access to an array value is out of bounds.
exception ElemNotFound of JSON.value-
This exception is used when there is no element of an array that satisfies the predicate of a
FINDedge in a path. The argument will be the array in question. val exnMessage : exn -> string-
exnMessage exnreturns an error-message string for the exception valueexn. This function produces specialized messages for theJSONErrorwrapped around the other exceptions defined in this structure (or theFailexception). It 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 the exception valueJSONError(NotBool, jv)if the argumentjvis not a JSON boolean value. val asInt : JSON.value -> int-
asInt (JSON.INT n)returns the valuenconverted toint. This function raises the exception valueJSONError(NotInt, jv)if the argumentjvis not a JSON integer value. It may also raise theOverflowexception ifnis too large for the defaultinttype. val asIntInf : JSON.value -> IntInf.int-
asIntInf (JSON.INT n)returns the valuen. This function raises the exception valueJSONError(NotInt, jv)if the argumentjvis not a JSON integer value. val asNumber : JSON.value -> Real.real-
asNumber jvconverts the JSON numberjvto an SMLrealvalue. Thejvargument can either have the formJSON.INT n, in which casenis converted to therealtype and returned, orJSON.FLOAT f, in which casefis returned; otherwise, the exception valueJSONError(NotNumber, jv)is raised. val asString : JSON.value -> string-
asBool (JSON.STRING s)returns the values. This function raises the exception valueJSONError(NotString, jv)if the argumentjvis not a JSON string. val findField : JSON.value -> string -> JSON.value option-
findField (JSON.OBJECT flds) keyreturnsSOME jvwhen the list of fieldsfldscontains(key, jv)andNONEotherwise. IffindFieldis called on a valuevthat is not a JSON object, then it raises the exception valueJSONError(NotObject, v) val lookupField : JSON.value -> string -> JSON.value-
lookupField (JSON.OBJECT flds) keyreturnsjvwhen the list of fieldsfldscontains(key, jv)and raises the exception valueJSONError(FieldNotFound key, v)otherwise. IflookupFieldis called on a valuevthat is not a JSON object, then it raises the exception valueJSONError(NotObject, v). val hasField : string -> JSON.value -> bool-
hasField key vreturnstruewhen the valuevis a JSON object that has a field withkeyas its label andfalseotherwise. val testField : string -> (JSON.value -> bool) -> JSON.value -> bool-
testField key pred vreturns the result ofpred jvwhen the valuevis a JSON object that contains(key, jv). It returnsfalseotherwise. val asArray : JSON.value -> JSON.value vector-
asArray jvconverts the JSON array valuejvto an SML vector value. It raises the exception valueJSONError(NotArray, jv)whenjvis 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 exception value
JSONError(NotArray, v)if the second argumentvis 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 keyspecifies the value labeled bykeyin a JSON object. SUB of int-
SUB ispecifies theith element of a JSON array. FIND of JSON.value -> bool-
FIND predspecifies 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 ofjvnamed bypath. It raises theJSONErrorexception if there is an inconsistency between the path and the structure ofjv. The first argument to theJSONErrorexception will be one of theNotObject,NotArray,FieldNotFound, orElemNotFoundexceptions.