serde: Implement conversion of CanonicalJsonValue to JsonValue
This commit is contained in:
parent
27dc07d538
commit
28a9083a26
@ -122,8 +122,8 @@ impl fmt::Display for CanonicalJsonValue {
|
|||||||
impl TryFrom<JsonValue> for CanonicalJsonValue {
|
impl TryFrom<JsonValue> for CanonicalJsonValue {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(json: JsonValue) -> Result<Self, Self::Error> {
|
fn try_from(val: JsonValue) -> Result<Self, Self::Error> {
|
||||||
Ok(match json {
|
Ok(match val {
|
||||||
JsonValue::Bool(b) => Self::Bool(b),
|
JsonValue::Bool(b) => Self::Bool(b),
|
||||||
JsonValue::Number(num) => Self::Integer(
|
JsonValue::Number(num) => Self::Integer(
|
||||||
Int::try_from(num.as_i64().ok_or(Error::IntConvert)?)
|
Int::try_from(num.as_i64().ok_or(Error::IntConvert)?)
|
||||||
@ -143,6 +143,23 @@ impl TryFrom<JsonValue> for CanonicalJsonValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<CanonicalJsonValue> for JsonValue {
|
||||||
|
fn from(val: CanonicalJsonValue) -> Self {
|
||||||
|
match val {
|
||||||
|
CanonicalJsonValue::Bool(b) => Self::Bool(b),
|
||||||
|
CanonicalJsonValue::Integer(int) => Self::Number(i64::from(int).into()),
|
||||||
|
CanonicalJsonValue::String(string) => Self::String(string),
|
||||||
|
CanonicalJsonValue::Array(vec) => {
|
||||||
|
Self::Array(vec.into_iter().map(Into::into).collect())
|
||||||
|
}
|
||||||
|
CanonicalJsonValue::Object(obj) => {
|
||||||
|
Self::Object(obj.into_iter().map(|(k, v)| (k, v.into())).collect())
|
||||||
|
}
|
||||||
|
CanonicalJsonValue::Null => Self::Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Serialize for CanonicalJsonValue {
|
impl Serialize for CanonicalJsonValue {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user