std.json.Json
class pub 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: Bytes](bytes: ref T) -> Result[Json, Error] {
Parser.new(bytes).parse
}
fn pub static parse[T: Bytes](bytes: ref T) -> Result[Json, Error]
Parses a Bytes
into a JSON object.
Examples
Parsing a String
:
import std.json (Json)
Json.parse('[10]') # => Result.Ok(Json.Array([Json.Int(10)]))
Parsing a ByteArray
:
import std.json (Json)
Json.parse('[10]'.to_byte_array) # => Result.Ok(Json.Array([Json.Int(10)]))
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> 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 {
case Int(lhs) -> {
match other {
case Int(rhs) -> lhs == rhs
case _ -> false
}
}
case Float(lhs) -> {
match other {
case Float(rhs) -> lhs == rhs
case _ -> false
}
}
case String(lhs) -> {
match other {
case String(rhs) -> lhs == rhs
case _ -> false
}
}
case Array(lhs) -> {
match other {
case Array(rhs) -> lhs == rhs
case _ -> false
}
}
case Object(lhs) -> {
match other {
case Object(rhs) -> lhs == rhs
case _ -> false
}
}
case Bool(lhs) -> {
match other {
case Bool(rhs) -> lhs == rhs
case _ -> false
}
}
case Null -> {
match other {
case 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[ref Json] for Json
Format
impl Format for Json
ToString
impl ToString for Json