std.json.Generator
type pub Generator
A type for turning a Json
value into a JSON string.
This type is used by Json.to_string
and Json.to_pretty_string
. Unless you
want to use custom indentation, it's recommended to use these methods instead
of using Generator
directly.
Static methods
new
Show source codeHide source code
fn pub static new(indent: Int) -> Generator {
Generator(
pretty: indent > 0,
spaces: ' '.repeat(indent),
depth: 0,
buffer: StringBuffer.new,
)
}
fn pub static new(indent: Int) -> Generator
Returns a new Generator
using the given indentation level.
The indent
argument specifies the number of spaces to use per indentation
level. If this value is less than or equal to zero, no indentation is
applied.
Instance methods
generate
Show source codeHide source code
fn pub mut generate(value: ref Json) -> String {
generate_value(value)
@buffer.to_string
}
fn pub mut generate(value: ref Json) -> String
Generates a JSON string for the given Json
value.
Examples
import std.json (Generator)
Generator.new(indent: 2).generate(Json.Array([Json.Int(1)])) # => '[1]'