[165] | 1 | .. _api-zoo-format-json: |
---|
| 2 | |
---|
| 3 | ZOO.Format.JSON |
---|
| 4 | =============== |
---|
| 5 | |
---|
| 6 | A parser to read/write JSON safely. |
---|
| 7 | |
---|
[192] | 8 | Inherits from |
---|
| 9 | |
---|
| 10 | - :ref:`ZOO.Format <api-zoo-format>` |
---|
| 11 | |
---|
[165] | 12 | Properties |
---|
| 13 | ---------- |
---|
| 14 | |
---|
| 15 | .. list-table:: |
---|
| 16 | :widths: 12 50 |
---|
| 17 | :header-rows: 1 |
---|
| 18 | |
---|
| 19 | * - NAME |
---|
| 20 | - DESCRIPTION |
---|
| 21 | * - :ref:`indent <indent>` |
---|
| 22 | - {String} For "pretty" printing, the indent string will be used once for each indentation level. |
---|
| 23 | * - :ref:`space <space>` |
---|
| 24 | - {String} For "pretty" printing, the space string will be used after the ":" separating a name/value pair. |
---|
| 25 | * - :ref:`newline <newline>` |
---|
| 26 | - {String} For "pretty" printing, the newline string will be used at the end of each name/value pair or array item. |
---|
| 27 | * - :ref:`level <level>` |
---|
| 28 | - {Integer} For "pretty" printing, this is incremented/decremented during serialization. |
---|
| 29 | * - :ref:`pretty <pretty>` |
---|
| 30 | - {Boolean} Serialize with extra whitespace for structure. |
---|
| 31 | |
---|
| 32 | Functions |
---|
| 33 | --------- |
---|
| 34 | |
---|
| 35 | .. list-table:: |
---|
| 36 | :widths: 14 50 |
---|
| 37 | :header-rows: 1 |
---|
| 38 | |
---|
| 39 | * - NAME |
---|
| 40 | - DESCRIPTION |
---|
| 41 | * - :ref:`ZOO.Format.JSON <ZOO.Format.JSON>` |
---|
| 42 | - Create a new parser for JSON. |
---|
| 43 | * - :ref:`read <read>` |
---|
| 44 | - Deserialize a json string. |
---|
| 45 | * - :ref:`write <write>` |
---|
| 46 | - Serialize an object into a JSON string. |
---|
| 47 | * - :ref:`writeIndent <writeIndent>` |
---|
| 48 | - Output an indentation string depending on the indentation level. |
---|
| 49 | * - :ref:`writeNewline <writeNewline>` |
---|
| 50 | - Output a string representing a newline if in pretty printing mode. |
---|
| 51 | * - :ref:`writeSpace <writeSpace>` |
---|
| 52 | - Output a string representing a space if in pretty printing mode. |
---|
| 53 | |
---|
| 54 | Serialize Properties |
---|
| 55 | -------------------- |
---|
| 56 | |
---|
| 57 | .. list-table:: |
---|
| 58 | :widths: 14 50 |
---|
| 59 | :header-rows: 1 |
---|
| 60 | |
---|
| 61 | * - NAME |
---|
| 62 | - DESCRIPTION |
---|
| 63 | * - :ref:`serialize <serialize>` |
---|
| 64 | - Object with properties corresponding to the serializable data types. |
---|
| 65 | |
---|
| 66 | Serialize Functions |
---|
| 67 | ------------------- |
---|
| 68 | |
---|
| 69 | .. list-table:: |
---|
| 70 | :widths: 14 50 |
---|
| 71 | :header-rows: 1 |
---|
| 72 | |
---|
| 73 | * - NAME |
---|
| 74 | - DESCRIPTION |
---|
| 75 | * - :ref:`serialize.object <serialize.object>` |
---|
| 76 | - Transform an object into a JSON string. |
---|
| 77 | * - :ref:`serialize.array <serialize.array>` |
---|
| 78 | - Transform an array into a JSON string. |
---|
| 79 | * - :ref:`serialize.string <serialize.string>` |
---|
| 80 | - Transform a string into a JSON string. |
---|
| 81 | * - :ref:`serialize.number <serialize.number>` |
---|
| 82 | - Transform a number into a JSON string. |
---|
| 83 | * - :ref:`serialize.boolean <serialize.boolean>` |
---|
| 84 | - Transform a boolean into a JSON string. |
---|
| 85 | * - :ref:`serialize.date <serialize.date>` |
---|
| 86 | - Transform a date into a JSON string. |
---|
| 87 | |
---|
| 88 | **Properties** |
---|
| 89 | |
---|
| 90 | .. _indent: |
---|
| 91 | |
---|
| 92 | indent |
---|
[175] | 93 | ``{String}`` For "pretty" printing, the indent string will be used once for each indentation level. |
---|
[165] | 94 | |
---|
| 95 | .. _space: |
---|
| 96 | |
---|
| 97 | space |
---|
[175] | 98 | ``{String}`` For "pretty" printing, the space string will be used after the ":" separating a name/value pair. |
---|
[165] | 99 | |
---|
| 100 | .. _newline: |
---|
| 101 | |
---|
| 102 | newline |
---|
[175] | 103 | ``{String}`` For "pretty" printing, the newline string will be used at the end of each name/value pair or array item. |
---|
[165] | 104 | |
---|
| 105 | .. _level: |
---|
| 106 | |
---|
| 107 | level |
---|
[175] | 108 | ``{Integer}`` For "pretty" printing, this is incremented/decremented during serialization. |
---|
[165] | 109 | |
---|
| 110 | .. _pretty: |
---|
| 111 | |
---|
| 112 | pretty |
---|
[175] | 113 | ``{Boolean}`` Serialize with extra whitespace for structure. This is set by the :ref:`write <write>` method. |
---|
[165] | 114 | |
---|
| 115 | **Functions** |
---|
| 116 | |
---|
| 117 | .. _ZOO.Format.JSON: |
---|
| 118 | |
---|
| 119 | ZOO.Format.JSON |
---|
| 120 | Create a new parser for JSON. |
---|
[175] | 121 | |
---|
| 122 | *Parameters* |
---|
[165] | 123 | |
---|
[175] | 124 | ``options {Object}`` An optional object whose properties will be set on this instance. |
---|
| 125 | |
---|
[165] | 126 | .. _read: |
---|
| 127 | |
---|
| 128 | read |
---|
[175] | 129 | :: |
---|
| 130 | |
---|
| 131 | read: function(json,filter) |
---|
| 132 | |
---|
[165] | 133 | Deserialize a json string. |
---|
[175] | 134 | |
---|
| 135 | *Parameters* |
---|
[165] | 136 | |
---|
[175] | 137 | | ``json {String}`` A JSON string |
---|
| 138 | | ``filter {Function}`` A function which will be called for every key and value at every level of the final result. Each value will be replaced by the result of the filter function. This can be used to reform generic objects into instances of classes, or to transform date strings into Date objects. |
---|
| 139 | |
---|
| 140 | *Returns* |
---|
| 141 | |
---|
| 142 | ``{Object}`` An object, array, string, or number. |
---|
| 143 | |
---|
[165] | 144 | .. _write: |
---|
| 145 | |
---|
| 146 | write |
---|
[175] | 147 | :: |
---|
| 148 | |
---|
| 149 | write: function(value,pretty) |
---|
| 150 | |
---|
[165] | 151 | Serialize an object into a JSON string. |
---|
[175] | 152 | |
---|
| 153 | *Parameters* |
---|
[165] | 154 | |
---|
[175] | 155 | | ``value {String}`` The object, array, string, number, boolean or date to be serialized. |
---|
| 156 | | ``pretty {Boolean}`` Structure the output with newlines and indentation. Default is false. |
---|
| 157 | |
---|
| 158 | *Returns* |
---|
| 159 | |
---|
| 160 | ``{String}`` The JSON string representation of the input value. |
---|
| 161 | |
---|
[165] | 162 | .. _writeIndent: |
---|
| 163 | |
---|
| 164 | writeIndent |
---|
[175] | 165 | :: |
---|
| 166 | |
---|
| 167 | writeIndent: function() |
---|
| 168 | |
---|
[165] | 169 | Output an indentation string depending on the indentation level. |
---|
| 170 | |
---|
[175] | 171 | *Returns* |
---|
| 172 | |
---|
| 173 | ``{String}`` An appropriate indentation string. |
---|
| 174 | |
---|
[165] | 175 | .. _writeNewline: |
---|
| 176 | |
---|
| 177 | writeNewline |
---|
[175] | 178 | :: |
---|
| 179 | |
---|
| 180 | writeNewline: function() |
---|
| 181 | |
---|
[165] | 182 | Output a string representing a newline if in pretty printing mode. |
---|
| 183 | |
---|
[175] | 184 | *Returns* |
---|
| 185 | |
---|
| 186 | ``{String}`` A string representing a new line. |
---|
| 187 | |
---|
[165] | 188 | .. _writeSpace: |
---|
| 189 | |
---|
| 190 | writeSpace |
---|
[175] | 191 | :: |
---|
[165] | 192 | |
---|
[175] | 193 | writeSpace: function() |
---|
| 194 | |
---|
| 195 | Output a string representing a space if in pretty printing mode. |
---|
| 196 | |
---|
| 197 | *Returns* |
---|
| 198 | |
---|
| 199 | ``{String}`` A space. |
---|
| 200 | |
---|
[165] | 201 | **Serialize Properties** |
---|
| 202 | |
---|
| 203 | .. _serialize: |
---|
| 204 | |
---|
| 205 | serialize |
---|
[175] | 206 | Object with properties corresponding to the serializable data types. Property |
---|
| 207 | values are functions that do the actual serializing. |
---|
[165] | 208 | |
---|
| 209 | **Serialize Functions** |
---|
| 210 | |
---|
| 211 | .. _serialize.object: |
---|
| 212 | |
---|
| 213 | serialize.object |
---|
| 214 | Transform an object into a JSON string. |
---|
[175] | 215 | |
---|
| 216 | *Parameters* |
---|
| 217 | |
---|
| 218 | ``object {Object}`` The object to be serialized. |
---|
[165] | 219 | |
---|
[175] | 220 | *Returns* |
---|
| 221 | |
---|
| 222 | ``{String}`` A JSON string representing the object. |
---|
| 223 | |
---|
[165] | 224 | .. _serialize.array: |
---|
| 225 | |
---|
| 226 | serialize.array |
---|
| 227 | Transform an array into a JSON string. |
---|
[175] | 228 | |
---|
| 229 | *Parameters* |
---|
[165] | 230 | |
---|
[175] | 231 | ``array {Array}`` The array to be serialized |
---|
| 232 | |
---|
| 233 | *Returns* |
---|
| 234 | |
---|
| 235 | ``{String}`` A JSON string representing the array. |
---|
| 236 | |
---|
[165] | 237 | .. _serialize.string: |
---|
| 238 | |
---|
| 239 | serialize.string |
---|
| 240 | Transform a string into a JSON string. |
---|
[175] | 241 | |
---|
| 242 | *Parameters* |
---|
[165] | 243 | |
---|
[175] | 244 | ``string {String}`` The string to be serialized |
---|
| 245 | |
---|
| 246 | *Returns* |
---|
| 247 | |
---|
| 248 | ``{String}`` A JSON string representing the string. |
---|
| 249 | |
---|
[165] | 250 | .. _serialize.number: |
---|
| 251 | |
---|
| 252 | serialize.number |
---|
| 253 | Transform a number into a JSON string. |
---|
[175] | 254 | |
---|
| 255 | *Parameters* |
---|
[165] | 256 | |
---|
[175] | 257 | ``number {Number}`` The number to be serialized. |
---|
| 258 | |
---|
| 259 | *Returns* |
---|
| 260 | |
---|
| 261 | ``{String}`` A JSON string representing the number. |
---|
| 262 | |
---|
[165] | 263 | .. _serialize.boolean: |
---|
| 264 | |
---|
| 265 | serialize.boolean |
---|
| 266 | Transform a boolean into a JSON string. |
---|
[175] | 267 | |
---|
| 268 | *Parameters* |
---|
[165] | 269 | |
---|
[175] | 270 | ``bool {Boolean}`` The boolean to be serialized. |
---|
| 271 | |
---|
| 272 | *Returns* |
---|
| 273 | |
---|
| 274 | ``{String}`` A JSON string representing the boolean. |
---|
| 275 | |
---|
[165] | 276 | .. _serialize.date: |
---|
| 277 | |
---|
| 278 | serialize.date |
---|
[175] | 279 | Transform a date into a JSON string. |
---|
| 280 | |
---|
| 281 | *Parameters* |
---|
| 282 | |
---|
| 283 | ``date {Date}`` The date to be serialized. |
---|
| 284 | |
---|
| 285 | *Returns* |
---|
| 286 | |
---|
| 287 | ``{String}`` A JSON string representing the date. |
---|