json-writer-rs
Simple and fast crate for writing JSON to a string without creating intermediate objects.
Usage
Basic usage:
use JSONObjectWriter;
let number: i32 = 42;
let mut object_str = Stringnew;
assert_eq!;
Various examples:
use ;
// Values
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Options of values
assert_eq!;
assert_eq!;
// Slices and vectors
let numbers: = ;
assert_eq!;
let numbers_vec: = vec!;
assert_eq!;
let strings: = ;
assert_eq!;
// Hash-maps:
let mut map = new;
map.insert;
assert_eq!;
// Objects:
let mut object_str = Stringnew;
let mut object_writer = new;
// Values
object_writer.value;
object_writer.value;
// Nested arrays
let mut nested_array = object_writer.array;
nested_array.value;
nested_array.value;
nested_array.end;
// Nested objects
let nested_object = object_writer.object;
nested_object.end;
object_writer.end;
assert_eq!;
Writing large files
You can manually flush the buffer to a file in order to write large files without running out of memory.
Example:
use JSONArrayWriter;
Limitations
Because there is no intermediate representations, all values must be written in the order they appear in the JSON output. The Borrow checker ensures sub-objects are closed before anything else can be written after them.
use JSONObjectWriter;
let mut object_str = Stringnew;
let mut object_writer = new;
let mut nested_a = object_writer.object;
let mut nested_b = object_writer.object;
// Compile error: The borrow checker ensures the values are appended in the correct order.
// You can only write one object at a time.
nested_a.value;
nested_b.value;
The writer does not check for duplicate keys
use JSONObjectWriter;
let mut object_str = Stringnew;
assert_eq!;