From 1f9cad943100dfa67bb3123fbbd9ff741b9a75f7 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 22 May 2021 19:13:15 +0100 Subject: [PATCH] Ensure arrays and objects serialize correctly --- src/sql/value.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/sql/value.rs b/src/sql/value.rs index 40019f13..e606c46f 100644 --- a/src/sql/value.rs +++ b/src/sql/value.rs @@ -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(&self, s: S) -> Result + 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), + } + } + } +}