Skip to content

Commit b9c72f0

Browse files
committed
(feat) initial commit of json module
1 parent fb27fcb commit b9c72f0

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ resolver = "2"
33
members = [
44
"valuable",
55
"valuable-derive",
6+
"valuable-json",
67
"valuable-serde",
78
"tests",
89
]

valuable-json/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "valuable-json"
3+
version = "0.1.0"
4+
authors = ["Ning Sun <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT"
7+
description = "Valuable instrument for `serde_json::Value`"
8+
9+
[dependencies]
10+
valuable = { version = "0.1", path = "../valuable", default-features = false }
11+
serde_json = "1"

valuable-json/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use serde_json::{Value as JsonValue};
2+
use valuable::{Valuable, Value, Visit};
3+
4+
struct Json<'a>(&'a JsonValue);
5+
6+
impl<'a> Valuable for Json<'a> {
7+
fn as_value(&self) -> Value<'a> {
8+
match self.0 {
9+
// TODO: fixme
10+
JsonValue::Array(ref array) => Value::Listable(array),
11+
JsonValue::Bool(ref value) => Value::Bool(*value),
12+
JsonValue::Number(ref num) => {
13+
// TODO: check correctness for this
14+
if num.is_f64() {
15+
Value::F64(num.as_f64().unwrap())
16+
} else if num.is_i64() {
17+
Value::I64(num.as_i64().unwrap())
18+
} else if num.is_u64() {
19+
Value::U64(num.as_u64().unwrap())
20+
} else {
21+
unreachable!()
22+
}
23+
}
24+
JsonValue::Null => Value::Unit,
25+
JsonValue::String(ref s) => Value::String(s),
26+
JsonValue::Object(ref object) => {
27+
// TODO: make map valuable
28+
Value::Mappable(object),
29+
}
30+
}
31+
}
32+
33+
fn visit(&self, visit: &mut dyn Visit) {
34+
//TODO:
35+
}
36+
}

0 commit comments

Comments
 (0)