Crates.io | jsonify |
lib.rs | jsonify |
version | 0.2.0 |
source | src |
created_at | 2021-03-07 21:17:11.814928 |
updated_at | 2021-03-07 21:23:35.778588 |
description | Simple struct to JSON library for Rust |
homepage | |
repository | https://github.com/lilspelunker/jsonify-rs |
max_upload_size | |
id | 365362 |
size | 3,696 |
A simple library to convert structs to JSON.
#[macro_use] extern crate jsonify;
use jsonify::Serializable;
struct Foo {
bar: String,
baz: i32
}
impl Serializable for Foo {
fn serialize(&self) -> String {
json!(
bar => self.bar,
baz => self.baz
)
}
}
fn main() {
let foo = Foo { bar: "Hello!".to_string(), baz: 30 };
println!("{}", foo.serialize()); // Output: {"bar": "Hello!", "baz": 30}
}
Import JSONify and Serializable trait
#[macro_use] extern crate jsonify;
use jsonify::Serializable;
Create struct Foo
struct Foo {
bar: String,
baz: i32
}
Implement Serializable
trait
impl Serializable for Foo {
fn serialize(&self) -> String {
// ...
}
}
Calls json! macro to create JSON string
/*
Usage:
json!(
key => value,
key2 => value2
)
*/
json!(
bar => self.bar,
baz => self.baz
)
Create instance of Foo
and serialize it
fn main() {
let foo = Foo { bar: "Hello!".to_string(), baz: 30 };
println!("{}", foo.serialize()); // Output: {"bar": "Hello!", "baz": 30}
}