https://github.com/puellaquae/rust-json
A simple JSON serializer and deserializer for learning rust
https://github.com/puellaquae/rust-json
json rust
Last synced: 4 months ago
JSON representation
A simple JSON serializer and deserializer for learning rust
- Host: GitHub
- URL: https://github.com/puellaquae/rust-json
- Owner: Puellaquae
- Created: 2021-08-13T06:06:35.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-12T13:51:04.000Z (over 1 year ago)
- Last Synced: 2025-06-30T20:05:24.802Z (12 months ago)
- Topics: json, rust
- Language: Rust
- Homepage: https://crates.io/crates/rust_json
- Size: 80.1 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rust_json
 [](https://crates.io/crates/rust_json)
学习 rust 所写的 JSON 解析与序列化工具。 JSON serializer and deserializer written for learning rust.
解析的编写参考了 miloyip 的 [json-tutorial](https://github.com/miloyip/json-tutorial)。 Parser is learned from miloyip's [json-tutorial](https://github.com/miloyip/json-tutorial).
Rust 宏的编写参考了 [Serde JSON](https://github.com/serde-rs/json)。 Marco is learned from [Serde JSON](https://github.com/serde-rs/json).
## 功能 / Feature
### 从字符串解析 json / Parse json from string
```rust
use rust_json::json_parse;
fn example() {
let j = json_parse(r#"
{
"S": [
1,
2.3,
{
"4": {
"5": {
"6": [
null,
true,
false
]
}
}
}
]
}"#);
println!("{}", j["S"]);
}
```
### 以 json 的风格构造 JsonElem / Construct JsonElem with json literal
```rust
use rust_json::json;
fn example() {
let j = json!(
{
"S": [
1.2,
"2.3",
{
"4": {
"5": {
"6": [
null,
true,
false
]
}
}
}
]
}
);
println!("{}", j["S"]);
}
```
### 以 js 的风格构造 JsonElem / Construct JsonElem with js object literal style
```rust
use rust_json::js_object;
fn proc(n: i32) -> i32 {
n * n + n / 2
}
fn main() {
let a = true;
let n = 32;
let j = js_object!([
{
a // 属性的简洁表示 Property Shorthand
},
{
// 使用表达式作为值 Using expression
proc_n: if n % 2 == 0 { proc(n) + 1 } else { 0 },
[n * 12]: n * 12 // 属性名表达式 Computed Property Names
}
]);
println!("{}", j);
}
```
### `#[derive(ToJson)]`
```rust
use rust_json::ToJson;
use rust_json_derive::ToJson;
#[derive(ToJson)]
struct Simple {
n: f64,
b: bool,
}
#[derive(ToJson)]
struct Nest {
a: Vec,
s: Simple,
}
#[derive(ToJson)]
enum Enum {
Unit,
One(i32),
Two(i32, i32),
Cmpx { a: i32, b: i32, c: i32 },
}
fn main() {
let s = Simple { n: 12.3, b: true };
println!("{}", s.to_json());
let n = Nest {
a: vec![1.2, 2.3],
s: s,
};
println!("{}", n.to_json());
let u = E::Unit;
let o = E::One(1);
let t = E::Two(1, 2);
let c = E::Cmpx { a: 1, b: 2, c: 3 };
println!("{}", u.to_json());
println!("{}", o.to_json());
println!("{}", t.to_json());
println!("{}", c.to_json());
}
```
### `#[derive(FromJson)]`
```rust
use rust_json::json_parse;
use rust_json_derive::FromJson;
#[derive(Debug, FromJson)]
struct Simple {
n: f64,
b: bool,
}
#[derive(Debug, FromJson)]
struct Nest {
a: Vec,
s: Simple,
}
#[derive(Debug, FromJson)]
enum Enum {
Unit,
One(i32),
Two(i32, i32),
Cmpx { a: i32, b: i32, c: i32 },
}
fn main() {
println!("{:?}", json_parse(r#"{"n": 12.3, "b": true}"#).get::());
println!("{:?}", json_parse(r#"
{
"a": [1.2, 2.3],
"s": {"n": 12.3, "b": true}
}
"#).get::());
println!("{:?}", json_parse(r#""Unit""#).get::());
println!("{:?}", json_parse(r#"{"One": 1}"#).get::());
println!("{:?}", json_parse(r#"
{
"Two": [1, 2]
}
"#).get::());
println!("{:?}", json_parse(r#"
{
"Cmpx": {"a": 1, "b": 2, "c": 3}
}
"#).get::());
}
```