std.json.Json
type pub inline enum Json
A JSON value, such as true
or an array.
Constructors
Int
Int(Int)
Float
Float(Float)
String
String(String)
Array
Array(Array[Json])
Object
Object(Map[String, Json])
Bool
Bool(Bool)
Null
Null()
Static methods
parse
Show source codeHide source code
fn pub static parse[T: mut + Read](bytes: T) -> Result[Json, Error] {
Parser.new(bytes).parse
}
fn pub static parse[T: mut + Read](bytes: T: mut) -> Result[Json, Error]
Parses a Read
type into a JSON object.
Examples
Parsing a String
:
import std.json (Json)
import std.io (Buffer)
Json.parse(Buffer.new('[10]')) # => Result.Ok(Json.Array([Json.Int(10)]))
Parsing a ByteArray
:
import std.io (Buffer)
import std.json (Json)
Json.parse(Buffer.new('[10]'.to_byte_array)) # => Result.Ok(Json.Array([Json.Int(10)]))
Instance methods
!=
Show source codeHide source code
fn pub !=(other: ref Self) -> Bool {
(self == other).false?
}
fn pub !=(other: ref Self) -> Bool
Returns true
if self
and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: ref Json) -> Bool {
match (self, other) {
case (Int(a), Int(b)) -> a == b
case (Float(a), Float(b)) -> a == b
case (String(a), String(b)) -> a == b
case (Bool(a), Bool(b)) -> a == b
case (Array(a), Array(b)) -> a == b
case (Object(a), Object(b)) -> a == b
case (Null, Null) -> true
case _ -> false
}
}
fn pub ==(other: ref Json) -> Bool
Returns true
if self
and the given object are equal to each other.
This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
match self {
case Int(val) -> formatter.tuple('Int').field(val).finish
case Float(val) -> formatter.tuple('Float').field(val).finish
case String(val) -> formatter.tuple('String').field(val).finish
case Array(val) -> formatter.tuple('Array').field(val).finish
case Object(val) -> formatter.tuple('Object').field(val).finish
case Bool(val) -> formatter.tuple('Bool').field(val).finish
case Null -> formatter.tuple('Null').finish
}
}
fn pub fmt(formatter: mut Formatter)
Formats self
in a human-readable format for debugging purposes.
query
Show source codeHide source code
fn pub query -> Query {
Query(Option.Some(self))
}
fn pub query -> Query
Returns a new Query
that starts at self
.
See the documentation of the Query
type for more information.
Examples
import std.json (Json)
Json.Int(42).query.as_int # => Option.Some(42)
to_pretty_string
Show source codeHide source code
fn pub to_pretty_string -> String {
Generator.new(DEFAULT_PRETTY_INDENT).generate(self)
}
fn pub to_pretty_string -> String
Formats self
as a JSON string using indentation for nested objects.
This method uses two spaces per indentation. To customise the amount of
spaces you'll need to use the Generator
type directly.
Examples
import std.json (Json)
Json.Int(42).to_pretty_string # => '42'
Json.Array([Json.Int(42)]).to_pretty_string # => "[\n 42\n]"
to_string
Show source codeHide source code
fn pub to_string -> String {
Generator.new(0).generate(self)
}
fn pub to_string -> String
Formats self
as a JSON string.
Examples
import std.json (Json)
Json.Int(42).to_string # => '42'
Json.Array([Json.Int(42)]).to_string # => '[42]'
Implemented traits
Equal
impl Equal for Json
Format
impl Format for Json
ToString
impl ToString for Json