-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
75 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ resolver = "2" | |
members = [ | ||
"valuable", | ||
"valuable-derive", | ||
"valuable-json", | ||
"valuable-serde", | ||
"tests", | ||
] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use serde_json::{Map, Value as Json}; | ||
|
||
use crate::{Mappable, Valuable, Value, Visit}; | ||
|
||
impl Valuable for Json { | ||
fn as_value(&self) -> Value<'_> { | ||
match self { | ||
Json::Array(ref array) => array.as_value(), | ||
Json::Bool(ref value) => value.as_value(), | ||
Json::Number(ref num) => { | ||
// TODO: check correctness for this | ||
if num.is_f64() { | ||
Value::F64(num.as_f64().unwrap()) | ||
} else if num.is_i64() { | ||
Value::I64(num.as_i64().unwrap()) | ||
} else if num.is_u64() { | ||
Value::U64(num.as_u64().unwrap()) | ||
} else { | ||
unreachable!() | ||
} | ||
} | ||
Json::Null => Value::Unit, | ||
Json::String(ref s) => s.as_value(), | ||
Json::Object(ref object) => object.as_value(), | ||
} | ||
} | ||
|
||
fn visit(&self, visit: &mut dyn Visit) { | ||
match self { | ||
Json::Array(ref array) => array.visit(visit), | ||
Json::Bool(ref value) => value.visit(visit), | ||
Json::Number(ref num) => { | ||
// TODO: check correctness for this | ||
if num.is_f64() { | ||
num.as_f64().unwrap().visit(visit) | ||
} else if num.is_i64() { | ||
num.as_i64().unwrap().visit(visit) | ||
} else if num.is_u64() { | ||
num.as_u64().unwrap().visit(visit) | ||
} else { | ||
unreachable!() | ||
} | ||
} | ||
Json::Null => Value::Unit.visit(visit), | ||
Json::String(ref s) => s.visit(visit), | ||
Json::Object(ref object) => object.visit(visit), | ||
} | ||
} | ||
} | ||
|
||
impl Valuable for Map<String, Json> { | ||
fn as_value(&self) -> Value<'_> { | ||
Value::Mappable(self) | ||
} | ||
|
||
fn visit(&self, visit: &mut dyn Visit) { | ||
for (k, v) in self.iter() { | ||
visit.visit_entry(k.as_value(), v.as_value()); | ||
} | ||
} | ||
} | ||
|
||
impl Mappable for Map<String, Json> { | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let len = self.len(); | ||
(len, Some(len)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters