|
| 1 | +**** |
1 | 2 | json |
2 | | -==== |
| 3 | +**** |
| 4 | + |
| 5 | +.. current-library:: json |
| 6 | +.. current-module:: json |
3 | 7 |
|
4 | | -.. current-library: json |
5 | | - |
6 | 8 | .. toctree:: |
7 | 9 | :maxdepth: 2 |
8 | 10 | :hidden: |
9 | 11 |
|
10 | | - reference |
| 12 | +This library provides essential functionality for working with JSON data. JSON |
| 13 | +(JavaScript Object Notation) is a lightweight data interchange format that is easy for |
| 14 | +humans to read and write, and easy for machines to parse and generate. |
| 15 | + |
| 16 | + |
| 17 | +The json Module |
| 18 | +=============== |
| 19 | + |
| 20 | +Constants |
| 21 | +--------- |
| 22 | + |
| 23 | +.. constant:: $null |
| 24 | + |
| 25 | + When parsing, JSON's "null" is converted to this value and when printing this value is |
| 26 | + printed as "null". |
| 27 | + |
| 28 | + |
| 29 | +Conditions |
| 30 | +---------- |
| 31 | + |
| 32 | +.. class:: <json-error> |
| 33 | + :open: |
| 34 | + :instantiable: |
| 35 | + |
| 36 | + All JSON errors are subclasses of this class. |
| 37 | + |
| 38 | + :superclasses: :class:`<format-string-condition>` :drm:`<error>` |
| 39 | + |
| 40 | +.. class:: <json-parse-error> |
| 41 | + :instantiable: |
| 42 | + |
| 43 | + Any error signalled during parsing (except for file system errors) will be an instance |
| 44 | + of this class. |
| 45 | + |
| 46 | + :superclasses: :class:`<json-error>` |
| 47 | + |
| 48 | + |
| 49 | +Parsing |
| 50 | +------- |
| 51 | + |
| 52 | +.. generic-function:: parse-json |
| 53 | + :open: |
| 54 | + |
| 55 | + Parse JSON formatted text from the given *source*. This is the |
| 56 | + main user-visible entry point for parsing. *table-class*, if |
| 57 | + provided, should be a subclass of :class:`<table>` to use when |
| 58 | + creating a json "object". |
| 59 | + |
| 60 | + :signature: parse-json (source, #key strict?, table-class) => (json) |
| 61 | + :parameter source: An :drm:`<object>`. |
| 62 | + :parameter #key strict?: An instance of :drm:`<boolean>`. |
| 63 | + :parameter #key table-class: Default to :class:`<string-table>`. |
| 64 | + :value json: A JSON :drm:`<object>` |
| 65 | + |
| 66 | + :discussion: |
| 67 | + |
| 68 | + The parse is strict by default. If ``strict?:`` :drm:`#f` is |
| 69 | + used then: |
| 70 | + |
| 71 | + - `#` is allowed as a comment character |
| 72 | + |
| 73 | + - ``\<c>`` is equivalent to ``<c>``, where ``<c>`` is not a defined |
| 74 | + escape character. |
| 75 | + |
| 76 | + - Trailing commas are allowed in arrays and objects. |
| 77 | + |
| 78 | +.. method:: parse-json |
| 79 | + :specializer: <string> |
| 80 | + |
| 81 | + Parse a JSON object from a :drm:`<string>`. |
| 82 | + |
| 83 | + :signature: parse-json (source, #key strict?, table-class) => (json) |
| 84 | + :parameter source: An instance of :drm:`<string>` |
| 85 | + :parameter #key strict?: An instance of :drm:`<boolean>`. The default is :drm:`#t`. |
| 86 | + :parameter #key table-class: A subclass of :class:`<table>`. |
| 87 | + :value json: An instance of :drm:`<object>`. |
| 88 | + |
| 89 | + :example: |
| 90 | + |
| 91 | + .. code-block:: dylan |
| 92 | +
|
| 93 | + let data = """{"a": 1, "b": 2,}"""; |
| 94 | + let parsed = parse-json(data, strict?: #f); |
| 95 | + let a = parsed["a"]; |
| 96 | + |
| 97 | + `Run this example <https://play.opendylan.org/shared/89037b0be1300a55>`_ |
| 98 | + in https://play.opendylan.org |
| 99 | + |
| 100 | + Note the use of ``strict?: #f`` is needed since *data* has a |
| 101 | + trailing comma after the number 2. |
| 102 | + |
| 103 | +.. method:: parse-json |
| 104 | + :specializer: <stream> |
| 105 | + |
| 106 | + Parse a JSON object from a :class:`<stream>`. |
| 107 | + |
| 108 | + :signature: parse-json (source, #key strict?, table-class) => (json) |
| 109 | + :parameter source: An instance of :class:`<stream>`. |
| 110 | + :parameter #key strict?: An instance of :drm:`<boolean>`. The default is :drm:`#f`. |
| 111 | + :parameter #key table-class: A subclass of :class:`<table>`. |
| 112 | + :value json: An instance of :drm:`<object>`. |
| 113 | + |
| 114 | + :example: |
| 115 | + |
| 116 | + .. code-block:: dylan |
| 117 | +
|
| 118 | + with-open-file (fs = "data.json") |
| 119 | + let data = parse-json(fs, strict?: #f); |
| 120 | + ... |
| 121 | + end; |
| 122 | + |
| 123 | + `Run an example |
| 124 | + <https://play.opendylan.org/shared/24c4ac32aaf6a5b5>`_ with a |
| 125 | + string stream in https://play.opendylan.org |
| 126 | + |
| 127 | + |
| 128 | +Printing |
| 129 | +-------- |
| 130 | + |
| 131 | +.. function:: print-json |
| 132 | + |
| 133 | + Print an object in JSON format. |
| 134 | + |
| 135 | + :signature: print-json (object, stream, #key indent, sort-keys?) => () |
| 136 | + :parameter object: The object to print. An instance of :drm:`<object>`. |
| 137 | + :parameter stream: Stream on wich to do output. An instance of :class:`<stream>`. |
| 138 | + :parameter #key indent: :drm:`#f` or an instance of :drm:`<integer>`. |
| 139 | + :parameter #key sort-keys?: An instance of :drm:`<boolean>`. |
| 140 | + |
| 141 | + :discussion: |
| 142 | + |
| 143 | + If ``indent`` is false, *object* is printed with minimal whitespace. If ``indent`` |
| 144 | + is an integer, then pretty printing is used, with *indent* spaces for each indent |
| 145 | + level. |
| 146 | + |
| 147 | + If ``sort-keys?`` is true, output object keys in lexicographical |
| 148 | + order. |
| 149 | + |
| 150 | + This function does some initial setup and then calls :gf:`do-print-json` to print |
| 151 | + ``object``. :gf:`do-print-json` has methods for most built-in Dylan types. |
| 152 | + |
| 153 | +.. generic-function:: do-print-json |
| 154 | + :open: |
| 155 | + |
| 156 | + :signature: do-print-json (object, stream) => () |
| 157 | + :parameter object: An instance of :drm:`<object>`. |
| 158 | + :parameter stream: An instance of :class:`<stream>`. |
| 159 | + |
| 160 | + :description: |
| 161 | + |
| 162 | + This method may be overridden for your own classes in order to print them in JSON |
| 163 | + format. Often the simplest way to implement your method will be to convert your |
| 164 | + object to a :drm:`<table>` and then pass it to :func:`print-json` to print it on |
| 165 | + *stream*. |
| 166 | + |
| 167 | + It is also possible to write JSON syntax directly to *stream*. If `indent:` was |
| 168 | + passed to *print* then *stream* will be a pretty printing stream and the `pprint |
| 169 | + module <https://opendylan.org/library-reference/io/print.html#the-pprint-module>`_ |
| 170 | + in the IO library may be used to implement pretty printing. |
| 171 | + |
| 172 | +.. method:: do-print-json |
| 173 | + :specializer: == $null |
| 174 | + |
| 175 | + Prints "null" on the output stream. |
| 176 | + |
| 177 | +.. method:: do-print-json |
| 178 | + :specializer: <integer> |
| 179 | + |
| 180 | + Prints an :drm:`<integer>` on the output stream. |
| 181 | + |
| 182 | +.. method:: do-print-json |
| 183 | + :specializer: <float> |
| 184 | + |
| 185 | + Prints a :drm:`<float>` on the output stream. |
| 186 | + |
| 187 | +.. method:: do-print-json |
| 188 | + :specializer: <boolean> |
11 | 189 |
|
12 | | -This library provides essential functionality for working with JSON |
13 | | -data. JSON (JavaScript Object Notation) is a lightweight data |
14 | | -interchange format that is easy for humans to read and write, and easy |
15 | | -for machines to parse and generate. |
| 190 | + Prints a :drm:`<boolean>` on the output stream as "true" or "false". |
16 | 191 |
|
17 | | -The json library offers two primary methods to facilitate the |
18 | | -conversion between JSON strings and Dylan data structures, making it |
19 | | -straightforward to integrate JSON data handling into your Open Dylan |
20 | | -applications. This methods are: |
| 192 | +.. method:: do-print-json |
| 193 | + :specializer: <string> |
21 | 194 |
|
22 | | -:gf:`parse-json` |
23 | | - This method takes a JSON-formatted string and converts it into an |
24 | | - Open Dylan table. This function is useful when you need to process |
25 | | - JSON data received from external sources, such as web APIs or |
26 | | - configuration files. |
| 195 | + Prints a :drm:`<string>` on the output stream as a JSON compatible |
| 196 | + string. Specifically, this method limits the escape codes to those recognized by the |
| 197 | + JSON format and converts non-printable characters to Unicode escape sequences. |
27 | 198 |
|
28 | | -:gf:`print-json` |
29 | | - The ``print-json`` function takes an Open Dylan table and converts |
30 | | - it into a formatted JSON string. This is useful for serializing |
31 | | - Open Dylan data structures into JSON format for storage, |
32 | | - transmission, or display purposes. |
| 199 | +.. method:: do-print-json |
| 200 | + :specializer: <collection> |
33 | 201 |
|
| 202 | + Prints a :drm:`<collection>` on the output stream as a JSON array. |
34 | 203 |
|
35 | | -Indices and tables |
36 | | -================== |
| 204 | +.. method:: do-print-json |
| 205 | + :specializer: <table> |
37 | 206 |
|
38 | | -* :ref:`genindex` |
| 207 | + Prints a :drm:`<table>` on the output stream as a JSON "object". |
0 commit comments