std.json
Types
struct JsonError
Functions
[src]
pub fn parse<T>(input: string | Bytes | Read): T throws JsonError {
switch (let input = input.(type)) {
case string:
return try _json.parse::<T>(_create_native_read(Cursor.new(input)));
case Bytes:
return try _json.parse::<T>(_create_native_read(Cursor.new(input)));
case Read:
return try _json.parse::<T>(_create_native_read(input));
}
}
Parse a JSON into specific type.
[src]
pub fn parse<T>(input: string | Bytes | Read): T throws JsonError {
switch (let input = input.(type)) {
case string:
return try _json.parse::<T>(_create_native_read(Cursor.new(input)));
case Bytes:
return try _json.parse::<T>(_create_native_read(Cursor.new(input)));
case Read:
return try _json.parse::<T>(_create_native_read(input));
}
}
Parse a JSON into specific type.
Parse a JSON into specific type.
nv
use std.json;
struct User {
name: string,
id: int,
profile: Profile?
}
struct Profile {
city: string?
}
let user = try! json.parse::<User>(`{
"name": "Jason Lee",
"id": 123456,
"profile": {
"city": "Chengdu"
}
}`);
assert_eq user.name, "Jason Lee";
assert_eq user.id, 123456;
assert_eq user.profile?.city, "Chengdu";
[src]
pub fn to_string<T>(value: T, pretty: bool = false): string throws JsonError {
let buf = Bytes.new();
try _json.to_writer(_create_native_write(buf), value, pretty);
return buf.to_string();
}
Serialize the given value as JSON into a string.
[src]
pub fn to_string<T>(value: T, pretty: bool = false): string throws JsonError {
let buf = Bytes.new();
try _json.to_writer(_create_native_write(buf), value, pretty);
return buf.to_string();
}
Serialize the given value as JSON into a string.
Serialize the given value as JSON into a string.
- struct
Kw Args
pretty
: If true, the output will be pretty-printed.
nv
use std.json;
struct User {
name: string,
id: int,
profile: Profile?
}
struct Profile {
city: string?
}
let user = try! json.parse::<User>(`{
"name": "Jason Lee",
"id": 123456,
"profile": {
"city": "Chengdu"
}
}`);
assert_eq try! json.to_string(user), `{"name":"Jason Lee","id":123456,"profile":{"city":"Chengdu"}}`;
assert_eq try! json.to_string(user, pretty: true), `{\n "name": "Jason Lee",\n "id": 123456,\n "profile": {\n "city": "Chengdu"\n }\n}`;
[src]
pub fn to_writer<T>(writer: Write, value: T, pretty: bool = false) throws JsonError {
try _json.to_writer(_create_native_write(writer), value, pretty);
}
Serialize the given value as JSON to std.io.Write
.
[src]
pub fn to_writer<T>(writer: Write, value: T, pretty: bool = false) throws JsonError {
try _json.to_writer(_create_native_write(writer), value, pretty);
}
Serialize the given value as JSON to std.io.Write
.
Serialize the given value as JSON to std.io.Write
.
nv
use std.io.Bytes;
use std.json;
struct User {
name: string,
}
let buf = Bytes.new();
let user = User { name: "Jason Lee" };
try! json.to_writer(buf, user);
assert_eq buf.to_string(), `{"name":"Jason Lee"}`;