io
File & Path I/O
Read and write files, stream large inputs, and work with paths. Blocking I/O can run on background threads.
- Whole-file read/write
- Streaming handles
- Background I/O
- Path helpers
JSON Parse & Manipulate
Core Standard Library
The json module is what you use for config files, API payloads, and log lines. Parse a string into a mutable tree, change values, stringify back out. Nested keys are reachable with dot paths like user.address.city so you don't write three levels of bracket indexing.
merge combines two objects recursively (handy for default config + overrides), and clone copies a subtree without shared references.
Import
import "json" |
Quick start
import "json" |
|
let data = json.parse('{"user": {"name": "Niao", "tags": ["fast"]}}') |
json.set(data, "user.tags.1", "lean") |
print(json.stringify(data, { pretty: true })) |
High-level overview of what json is for.
Turn JSON text into Niao values and back. stringify accepts a pretty flag for indented output.
get and set by dot path, user.settings.theme instead of chaining ["user"]["settings"]["theme"].
merge walks both objects and combines keys; clone duplicates a value tree for safe mutation.
Indented stringify for configs and debug dumps you actually want to read.
15 functions and methods in json. Grouped by category from the standard library docs.
| Signature | Description |
|---|---|
| json.parse(text) | Parse JSON string → value |
| json.stringify(value) | Compact JSON text |
| json.stringify_pretty(value, indent?) | Pretty-print (default indent `2`) |
| json.valid(text) | `true` if text is valid JSON |
| json.type(value) | `"null"`, `"bool"`, `"number"`, `"string"`, `"array"`, `"object"` |
| json.is_json(value) | Whether value can be serialized to JSON |
| json.keys(object) | Sorted key list |
| json.has(object, key) | Key exists |
| json.get(value, path) | Nested read - e.g. `"user.items[0].id"` |
| json.set(object, path, value) | Set nested field in place |
| json.merge(target, source) | Deep-merge objects into `target` |
| json.clone(value) | Deep copy |
| json.equal(a, b) | Deep equality |
| json.array_len(array) | Array length |
| json.object_len(object) | Object field count |
More from Core Standard Library.
File & Path I/O
Read and write files, stream large inputs, and work with paths. Blocking I/O can run on background threads.
Wall-Clock & Time Zones
Timestamps, formatting, parsing, and time zones. Built on chrono and chrono-tz, DST handled correctly.
Regular Expressions
Regular expressions via Rust's regex crate. Match, split, replace, and compile patterns once for hot loops.