uce/site/doc/pages/to_json.txt

35 lines
1.1 KiB
Plaintext

:sig
String DValue::to_json(char quote_char = '"') const
:params
quote_char : quote character used around string output
return value : the scalar as a single JSON token
:see
>types
0_DValue
json_encode
json_decode
to_string
:content
Renders a single scalar `DValue` value as a JSON token. This is a read accessor: it is `const`, never creates or modifies nodes, and dereferences internal references automatically.
Strings are escaped and quoted, numbers render as numeric literals, and booleans render as `true` / `false`.
This is **not** a serializer for nested data: map-shaped nodes render as the placeholder string `"(array)"`. Use `json_encode()` to serialize a whole tree.
## Example
```cpp
String token = context.props["label"].to_json(); // "\"Don't panic\""
String body = json_encode(context.props); // full tree serialization
```
The `quote_char` parameter exists for embedding output inside single-quoted contexts; for HTML attributes prefer double quotes plus `html_escape()`.
## Related Concepts
- JavaScript: `JSON.stringify(value)` for a single scalar
- PHP: `json_encode($scalar)`