Ensure arrays and objects serialize correctly

This commit is contained in:
Tobie Morgan Hitchcock 2021-05-22 19:13:15 +01:00
parent 9ae2cc7cd1
commit 1f9cad9431

View file

@ -8,7 +8,9 @@ use crate::sql::literal::Literal;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
const NAME: &'static str = "Value";
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize)]
pub enum Value {
Literal(Literal),
Expression(Expression),
@ -54,3 +56,22 @@ impl dbs::Process for Value {
}
}
}
impl Serialize for Value {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if s.is_human_readable() {
match self {
Value::Literal(ref v) => s.serialize_some(v),
Value::Expression(ref v) => s.serialize_some(v),
}
} else {
match self {
Value::Literal(ref v) => s.serialize_newtype_variant(NAME, 0, "Literal", v),
Value::Expression(ref v) => s.serialize_newtype_variant(NAME, 1, "Expression", v),
}
}
}
}