|
9 | 9 | single: Tutorial; JSON Parsing |
10 | 10 | single: Tutorial; JSON Serialization |
11 | 11 | single: Tutorial; json_boost |
| 12 | + single: Tutorial; sprint_json |
12 | 13 |
|
13 | 14 | This tutorial covers ``daslib/json`` and ``daslib/json_boost`` — parsing, |
14 | 15 | building, writing, and querying JSON data in daslang. |
@@ -195,6 +196,129 @@ Broken JSON repair |
195 | 196 | let fixed = try_fixing_broken_json(bad) |
196 | 197 | var js = read_json(fixed, error) |
197 | 198 |
|
| 199 | +sprint_json |
| 200 | +=========== |
| 201 | + |
| 202 | +``sprint_json`` is a builtin function that serializes any daslang value |
| 203 | +directly to a JSON string — no ``JsonValue?`` intermediate. It handles |
| 204 | +structs, classes, variants, tuples, tables, arrays, enums, pointers, |
| 205 | +and all basic types:: |
| 206 | + |
| 207 | + struct Record { |
| 208 | + id : int |
| 209 | + tag : string |
| 210 | + data : Payload |
| 211 | + values : array<int> |
| 212 | + meta : table<string; int> |
| 213 | + coords : tuple<int; float> |
| 214 | + ptr : void? |
| 215 | + } |
| 216 | + |
| 217 | + var r <- Record(uninitialized |
| 218 | + id = 1, tag = "test", |
| 219 | + data = Payload(uninitialized code = 42), |
| 220 | + values = [1, 2, 3], |
| 221 | + meta <- { "x" => 10 }, |
| 222 | + coords = (7, 3.14), |
| 223 | + ptr = null |
| 224 | + ) |
| 225 | + let compact = sprint_json(r, false) |
| 226 | + // {"id":1,"tag":"test","data":{"code":42},"values":[1,2,3],...} |
| 227 | + |
| 228 | + let pretty = sprint_json(r, true) |
| 229 | + // human-readable with indentation |
| 230 | + |
| 231 | +The second argument controls human-readable formatting. Works with |
| 232 | +simple values too:: |
| 233 | + |
| 234 | + sprint_json(42, false) // 42 |
| 235 | + sprint_json("hello", false) // "hello" |
| 236 | + sprint_json([10, 20, 30], false) // [10,20,30] |
| 237 | + |
| 238 | +Field annotations |
| 239 | +================= |
| 240 | + |
| 241 | +Struct field annotations control how ``sprint_json`` serializes fields. |
| 242 | +These require ``options rtti`` to be enabled. |
| 243 | + |
| 244 | +- ``@optional`` — skip the field if it has a default or empty value |
| 245 | +- ``@embed`` — embed a string field as raw JSON (no extra quotes) |
| 246 | +- ``@unescape`` — don't escape special characters in the string |
| 247 | +- ``@enum_as_int`` — serialize an enum as its integer value, not a string |
| 248 | +- ``@rename="key"`` — use ``key`` as the JSON field name instead of the daslang field name |
| 249 | + |
| 250 | +:: |
| 251 | + |
| 252 | + struct AnnotatedConfig { |
| 253 | + name : string |
| 254 | + @optional debug : bool // omitted when false |
| 255 | + @optional tags : array<string> // omitted when empty |
| 256 | + @embed raw_data : string // embedded as raw JSON |
| 257 | + @unescape raw_path : string // no escaping of special chars |
| 258 | + pri : Priority // serialized as string |
| 259 | + @enum_as_int level : Priority // serialized as integer |
| 260 | + @rename="type" _type : string // appears as "type" in JSON |
| 261 | + } |
| 262 | + |
| 263 | + var c <- AnnotatedConfig(uninitialized |
| 264 | + name = "app", debug = false, |
| 265 | + raw_data = "[1,2,3]", |
| 266 | + raw_path = "C:\\Users\\test", |
| 267 | + pri = Priority.high, |
| 268 | + level = Priority.medium, |
| 269 | + _type = "widget" |
| 270 | + ) |
| 271 | + let json_str = sprint_json(c, false) |
| 272 | + // {"name":"app","raw_data":[1,2,3],"raw_path":"C:\Users\test","pri":"high","level":1,"type":"widget"} |
| 273 | + |
| 274 | +In this example: ``debug`` and ``tags`` are omitted (``@optional``), |
| 275 | +``raw_data`` is embedded as ``[1,2,3]`` not ``"[1,2,3]"`` (``@embed``), |
| 276 | +``raw_path`` keeps backslashes unescaped (``@unescape``), ``level`` |
| 277 | +is ``1`` instead of ``"medium"`` (``@enum_as_int``), and ``_type`` |
| 278 | +appears as ``"type"`` in the output (``@rename``). |
| 279 | + |
| 280 | +@rename annotation |
| 281 | +================== |
| 282 | + |
| 283 | +Use ``@rename="json_key"`` when the JSON key is a daslang reserved word |
| 284 | +or doesn't follow daslang naming conventions. The field keeps a safe name |
| 285 | +in code (e.g. ``_type``) but serializes as the desired key. ``@rename`` |
| 286 | +works with ``sprint_json``, ``JV``, and ``from_JV``:: |
| 287 | + |
| 288 | + struct ApiResponse { |
| 289 | + @rename="type" _type : string |
| 290 | + @rename="class" _class : int |
| 291 | + value : float |
| 292 | + } |
| 293 | + |
| 294 | + var resp = ApiResponse(_type = "widget", _class = 3, value = 1.5) |
| 295 | + sprint_json(resp, false) |
| 296 | + // {"type":"widget","class":3,"value":1.5} |
| 297 | + |
| 298 | + // from_JV maps renamed keys back to struct fields |
| 299 | + var js = read_json("{\"type\":\"button\",\"class\":5,\"value\":2.0}", error) |
| 300 | + var result = from_JV(js, type<ApiResponse>) |
| 301 | + // result._type == "button", result._class == 5 |
| 302 | + |
| 303 | +Class serialization |
| 304 | +=================== |
| 305 | + |
| 306 | +Both ``JV``/``from_JV`` and ``sprint_json`` work with classes. |
| 307 | +Classes serialize their fields just like structs:: |
| 308 | + |
| 309 | + class Animal { |
| 310 | + species : string |
| 311 | + legs : int |
| 312 | + } |
| 313 | + |
| 314 | + var a = new Animal(species = "cat", legs = 4) |
| 315 | + let json_str = sprint_json(*a, false) |
| 316 | + // {"species":"cat","legs":4} |
| 317 | + |
| 318 | + var js = JV(*a) |
| 319 | + print(write_json(js)) |
| 320 | + // {"legs":4,"species":"cat"} |
| 321 | + |
198 | 322 | .. seealso:: |
199 | 323 |
|
200 | 324 | Full source: :download:`tutorials/language/30_json.das <../../../../tutorials/language/30_json.das>` |
|
0 commit comments