Skip to content

Commit c6df4ef

Browse files
committed
(feat) impl valuable for json
1 parent b9c72f0 commit c6df4ef

File tree

6 files changed

+75
-48
lines changed

6 files changed

+75
-48
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ resolver = "2"
33
members = [
44
"valuable",
55
"valuable-derive",
6-
"valuable-json",
76
"valuable-serde",
87
"tests",
98
]

valuable-json/Cargo.toml

Lines changed: 0 additions & 11 deletions
This file was deleted.

valuable-json/src/lib.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

valuable/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ std = ["alloc"]
1818
# Provide imps for types in Rust's `alloc` library.
1919
alloc = []
2020

21+
# Provides serde_json::Value instrument for valuable
22+
json = ["serde_json"]
23+
2124
[dependencies]
2225
valuable-derive = { version = "0.1.0", optional = true, path = "../valuable-derive" }
26+
serde_json = { version = "1.0.46", optional = true }
2327

2428
[dev-dependencies]
2529
criterion = "0.3"

valuable/src/json.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use serde_json::{Map, Value as Json};
2+
3+
use crate::{Mappable, Valuable, Value, Visit};
4+
5+
impl Valuable for Json {
6+
fn as_value(&self) -> Value<'_> {
7+
match self {
8+
Json::Array(ref array) => array.as_value(),
9+
Json::Bool(ref value) => value.as_value(),
10+
Json::Number(ref num) => {
11+
// TODO: check correctness for this
12+
if num.is_f64() {
13+
Value::F64(num.as_f64().unwrap())
14+
} else if num.is_i64() {
15+
Value::I64(num.as_i64().unwrap())
16+
} else if num.is_u64() {
17+
Value::U64(num.as_u64().unwrap())
18+
} else {
19+
unreachable!()
20+
}
21+
}
22+
Json::Null => Value::Unit,
23+
Json::String(ref s) => s.as_value(),
24+
Json::Object(ref object) => object.as_value(),
25+
}
26+
}
27+
28+
fn visit(&self, visit: &mut dyn Visit) {
29+
match self {
30+
Json::Array(ref array) => array.visit(visit),
31+
Json::Bool(ref value) => value.visit(visit),
32+
Json::Number(ref num) => {
33+
// TODO: check correctness for this
34+
if num.is_f64() {
35+
num.as_f64().unwrap().visit(visit)
36+
} else if num.is_i64() {
37+
num.as_i64().unwrap().visit(visit)
38+
} else if num.is_u64() {
39+
num.as_u64().unwrap().visit(visit)
40+
} else {
41+
unreachable!()
42+
}
43+
}
44+
Json::Null => Value::Unit.visit(visit),
45+
Json::String(ref s) => s.visit(visit),
46+
Json::Object(ref object) => object.visit(visit),
47+
}
48+
}
49+
}
50+
51+
impl Valuable for Map<String, Json> {
52+
fn as_value(&self) -> Value<'_> {
53+
Value::Mappable(self)
54+
}
55+
56+
fn visit(&self, visit: &mut dyn Visit) {
57+
for (k, v) in self.iter() {
58+
visit.visit_entry(k.as_value(), v.as_value());
59+
}
60+
}
61+
}
62+
63+
impl Mappable for Map<String, Json> {
64+
fn size_hint(&self) -> (usize, Option<usize>) {
65+
let len = self.len();
66+
(len, Some(len))
67+
}
68+
}

valuable/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,6 @@ pub use visit::{visit, Visit};
135135

136136
#[cfg(feature = "derive")]
137137
pub use valuable_derive::*;
138+
139+
#[cfg(feature = "json")]
140+
mod json;

0 commit comments

Comments
 (0)