json Library Reference ********************** .. current-library:: json .. current-module:: json The json module =============== Constants --------- .. constant:: $null Is what "null" parses to. Conditions ---------- .. class:: :open: :instantiable: All JSON errors are subclasses of this class. :superclasses: :class:`` :drm:`` .. class:: :instantiable: Any error signalled during parsing (except for file system errors) will be an instance of this. :superclasses: :class:`` ``parse-json`` -------------- .. generic-function:: parse-json :open: Parse json formatted text from the given *source*. This is the main user-visible entry point for parsing. *table-class*, if provided, should be a subclass of :class:`` to use when creating a json "object". :signature: parse-json (source, #key strict?, table-class) => (json) :parameter source: An :drm:``. :parameter #key strict?: An instance of :drm:``. :parameter #key table-class: Default to :class:``. :value json: A JSON :drm:`` :discussion: The parse is strict by default. If ``strict?:`` :drm:`#f` is used then: - `#` is allowed as a comment character - "\" is equivalent to ````, where is not a defined escape character. - trailing commas are allowed in arrays and objects .. method:: parse-json :specializer: Parse a JSON object from a :drm:``. :signature: parse-json (source, #key strict?, table-class) => (json) :parameter source: An instance of :drm:`` :parameter #key strict?: An instance of :drm:``. Default to :drm:`#t`. :parameter #key table-class: A subclass of :class:`
`. :value json: An instance of :drm:``. :example: .. code-block:: dylan let data = """{"a": 1, "b": 2,}"""; let parsed = parse-json(data, strict?: #f); let a = parsed["a"]; `Run this example `_ in https://play.opendylan.org Note the use of ``strict?: #f`` is needed since *data* has a trailing comma after the number 2. .. method:: parse-json :specializer: Parse a JSON object from a :class:``. :signature: parse-json (source, #key strict?, table-class) => (json) :parameter source: An instance of :class:``. :parameter #key strict?: An instance of :drm:``. Default to :drm:`#f`. :parameter #key table-class: A subclass of :class:`
`. :value json: An instance of :drm:``. :example: .. code-block:: dylan with-open-file (fs = "data.json") let data = parse-json(fs, strict?: #f); ... end; `Run an example `_ with a string stream in https://play.opendylan.org ``print-json`` -------------- .. function:: print-json Print an object in JSON format. :signature: print-json (object, stream, #key indent, sort-keys?) => () :parameter object: The object to print. An instance of :drm:``. :parameter stream: Stream on wich to do output. An instance of :class:``. :parameter #key indent: :drm:`#f` or an instance of :drm:``. :parameter #key sort-keys?: An instance of :drm:``. :discussion: If `indent` is false, *object* is printed with minimal whitespace. If an integer, then use pretty printing and output *indent* spaces for each indent level. If `sort-keys?:` is true, output object keys in lexicographical order. ``do-print-json`` ^^^^^^^^^^^^^^^^^ Override this to print your own objects in JSON format. It can be implemented by converting objects to built-in Dylan types (tables, collections, etc) and calling *print-json* on those objects, or by writing json syntax directly to *stream*. If `indent:` was passed to *print* then *stream* will be a pretty printing stream and the io:pprint module may be used to implement pretty printing. .. generic-function:: do-print-json :open: :signature: do-print-json (object, stream) => () :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: $null :parameter object: $null :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: Print an :drm:`` in JSON format. :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: Print a :drm:`` in JSON format. :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: Print a :drm:`` in JSON format. :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: Print a :drm:`` in JSON format. :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``. .. method:: do-print-json :specializer: Print a :drm:`` in JSON format. :parameter object: An instance of :drm:``. :parameter stream: An instance of :class:``.