diff --git a/Cargo.lock b/Cargo.lock index d65f858..e221d2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,11 +303,13 @@ dependencies = [ name = "merde_json" version = "6.2.1" dependencies = [ + "itoa", "lexical-parse-float", "merde_core", "merde_loggingserializer", "num-bigint", "num-traits", + "ryu", ] [[package]] diff --git a/merde_core/src/deserialize.rs b/merde_core/src/deserialize.rs index f6a16f7..35c6f6a 100644 --- a/merde_core/src/deserialize.rs +++ b/merde_core/src/deserialize.rs @@ -224,7 +224,7 @@ impl<'s> Deserialize<'s> for i64 { let v: i64 = match de.next()? { Event::I64(i) => i, Event::U64(u) => u.try_into().map_err(|_| MerdeError::OutOfRange)?, - Event::Float(f) => f as _, + Event::F64(f) => f as _, ev => { return Err(MerdeError::UnexpectedEvent { got: EventType::from(&ev), @@ -245,7 +245,7 @@ impl<'s> Deserialize<'s> for u64 { let v: u64 = match de.next()? { Event::U64(u) => u, Event::I64(i) => i.try_into().map_err(|_| MerdeError::OutOfRange)?, - Event::Float(f) => f as u64, + Event::F64(f) => f as u64, ev => { return Err(MerdeError::UnexpectedEvent { got: EventType::from(&ev), @@ -353,7 +353,7 @@ impl<'s> Deserialize<'s> for f64 { D: Deserializer<'s> + ?Sized, { let v: f64 = match de.next()? { - Event::Float(f) => f, + Event::F64(f) => f, Event::I64(i) => i as f64, Event::U64(u) => u as f64, ev => { @@ -557,7 +557,7 @@ impl<'s> Deserialize<'s> for Value<'s> { match de.next()? { Event::I64(i) => Ok(Value::I64(i)), Event::U64(u) => Ok(Value::U64(u)), - Event::Float(f) => Ok(Value::Float(f.into())), + Event::F64(f) => Ok(Value::Float(f.into())), Event::Str(s) => Ok(Value::Str(s)), Event::Bytes(b) => Ok(Value::Bytes(b)), Event::Bool(b) => Ok(Value::Bool(b)), diff --git a/merde_core/src/event.rs b/merde_core/src/event.rs index a0c52e4..058652d 100644 --- a/merde_core/src/event.rs +++ b/merde_core/src/event.rs @@ -6,7 +6,7 @@ use crate::{CowBytes, CowStr, MerdeError}; pub enum Event<'s> { I64(i64), U64(u64), - Float(f64), + F64(f64), Str(CowStr<'s>), Bytes(CowBytes<'s>), Bool(bool), @@ -47,8 +47,8 @@ impl_from_for_event! { u32 => U64, u64 => U64, // floats - f32 => Float, - f64 => Float, + f32 => F64, + f64 => F64, // misc. bool => Bool, } @@ -121,7 +121,7 @@ impl From<&Event<'_>> for EventType { match event { Event::I64(_) => EventType::I64, Event::U64(_) => EventType::U64, - Event::Float(_) => EventType::Float, + Event::F64(_) => EventType::Float, Event::Str(_) => EventType::Str, Event::Bytes(_) => EventType::Bytes, Event::Bool(_) => EventType::Bool, @@ -167,7 +167,7 @@ impl<'s> Event<'s> { pub fn into_f64(self) -> Result> { match self { - Event::Float(f) => Ok(f), + Event::F64(f) => Ok(f), _ => Err(MerdeError::UnexpectedEvent { got: EventType::from(&self), expected: &[EventType::Float], diff --git a/merde_core/src/into_static.rs b/merde_core/src/into_static.rs index b3ee4f0..0358398 100644 --- a/merde_core/src/into_static.rs +++ b/merde_core/src/into_static.rs @@ -58,7 +58,7 @@ impl<'s> IntoStatic for Event<'s> { match self { Event::I64(v) => Event::I64(v), Event::U64(v) => Event::U64(v), - Event::Float(v) => Event::Float(v), + Event::F64(v) => Event::F64(v), Event::Str(v) => Event::Str(v.into_static()), Event::Bytes(v) => Event::Bytes(v.into_static()), Event::Bool(v) => Event::Bool(v), diff --git a/merde_core/src/serialize.rs b/merde_core/src/serialize.rs index 057c13e..fb8aa54 100644 --- a/merde_core/src/serialize.rs +++ b/merde_core/src/serialize.rs @@ -220,7 +220,7 @@ impl<'s> Serialize for Value<'s> { match self { Value::I64(i) => serializer.write(Event::I64(*i)).await, Value::U64(u) => serializer.write(Event::U64(*u)).await, - Value::Float(f) => serializer.write(Event::Float(f.into_inner())).await, + Value::Float(f) => serializer.write(Event::F64(f.into_inner())).await, Value::Str(s) => serializer.write(Event::Str(s.clone())).await, Value::Bytes(b) => serializer.write(Event::Bytes(b.clone())).await, Value::Null => serializer.write(Event::Null).await, diff --git a/merde_json/Cargo.toml b/merde_json/Cargo.toml index 7e12f0d..798a515 100644 --- a/merde_json/Cargo.toml +++ b/merde_json/Cargo.toml @@ -11,10 +11,12 @@ keywords = ["json", "serialization", "deserialization", "jiter"] categories = ["encoding", "parser-implementations"] [dependencies] +itoa = "1.0.11" lexical-parse-float = { version = "0.8.5", features = ["format"] } merde_core = { version = "7.0.0", path = "../merde_core" } num-bigint = { version = "0.4.6", optional = true } num-traits = { version = "0.2.19", optional = true } +ryu = "1.0.18" [features] default = [] diff --git a/merde_json/src/deserialize.rs b/merde_json/src/deserialize.rs index 629f4f1..9931b20 100644 --- a/merde_json/src/deserialize.rs +++ b/merde_json/src/deserialize.rs @@ -140,7 +140,7 @@ impl<'s> Deserializer<'s> for JsonDeserializer<'s> { if num.fract() == 0.0 && num >= i64::MIN as f64 && num <= i64::MAX as f64 { Event::I64(num as i64) } else { - Event::Float(num) + Event::F64(num) } } else if peek == Peek::String { let s = self diff --git a/merde_json/src/lib.rs b/merde_json/src/lib.rs index 8912dcf..a326335 100644 --- a/merde_json/src/lib.rs +++ b/merde_json/src/lib.rs @@ -8,12 +8,11 @@ mod jiter_lite; use jiter_lite::errors::JiterError; use merde_core::{ - Array, CowStr, Deserialize, DeserializeOwned, Deserializer, IntoStatic, Map, MerdeError, Value, + CowStr, Deserialize, DeserializeOwned, Deserializer, IntoStatic, MerdeError, Serialize, + Serializer, }; -use std::borrow::Cow; -use std::collections::HashMap; -use std::io::Write; +use std::{collections::VecDeque, io::Write}; /// Writes JSON to a `Vec`. None of its methods can fail, since it doesn't target /// an `io::Write`. You can provide your own buffer via `JsonSerializer::from_vec`. @@ -23,12 +22,123 @@ use std::io::Write; #[derive(Default)] pub struct JsonSerializer { buffer: Vec, + stack: VecDeque, +} + +enum StackFrame { + // the next item to be written is an array element + Array { first: bool }, + // the next item to be written is a map key + MapKey { first: bool }, + // the next item to be written is a map value + // (and needs a ":" before it) + MapValue, +} + +impl Serializer for JsonSerializer { + type Error = MerdeJsonError<'static>; + + async fn write(&mut self, ev: merde_core::Event<'_>) -> Result<(), Self::Error> { + let stack_top = self.stack.back_mut(); + if let Some(stack_top) = stack_top { + match stack_top { + StackFrame::Array { first } => { + if matches!(ev, merde_core::Event::ArrayEnd) { + self.buffer.push(b']'); + self.stack.pop_back(); + } else if *first { + *first = false + } else { + self.buffer.push(b','); + } + } + StackFrame::MapKey { first } => { + if matches!(ev, merde_core::Event::MapEnd) { + self.buffer.push(b'}'); + self.stack.pop_back(); + } else { + if !*first { + self.buffer.push(b','); + } + *stack_top = StackFrame::MapValue; + // and then let the value write itself + } + } + StackFrame::MapValue => { + self.buffer.push(b':'); + *stack_top = StackFrame::MapKey { first: false }; + } + } + } + + match ev { + merde_core::Event::Null => { + self.buffer.extend_from_slice(b"null"); + } + merde_core::Event::Bool(b) => { + self.buffer + .extend_from_slice(if b { b"true" } else { b"false" }); + } + merde_core::Event::I64(i) => { + let mut buf = itoa::Buffer::new(); + self.buffer.extend_from_slice(buf.format(i).as_bytes()); + } + merde_core::Event::U64(u) => { + let mut buf = itoa::Buffer::new(); + self.buffer.extend_from_slice(buf.format(u).as_bytes()); + } + merde_core::Event::F64(f) => { + let mut buf = ryu::Buffer::new(); + self.buffer.extend_from_slice(buf.format(f).as_bytes()); + } + merde_core::Event::Str(s) => { + // slow path + self.buffer.push(b'"'); + for c in s.chars() { + match c { + '"' => self.buffer.extend_from_slice(b"\\\""), + '\\' => self.buffer.extend_from_slice(b"\\\\"), + '\n' => self.buffer.extend_from_slice(b"\\n"), + '\r' => self.buffer.extend_from_slice(b"\\r"), + '\t' => self.buffer.extend_from_slice(b"\\t"), + c if c.is_control() => { + let _ = write!(self.buffer, "\\u{:04x}", c as u32); + } + c => self.buffer.extend_from_slice(c.to_string().as_bytes()), + } + } + self.buffer.push(b'"'); + } + merde_core::Event::MapStart(_) => { + self.buffer.push(b'{'); + self.stack.push_back(StackFrame::MapKey { first: true }); + } + merde_core::Event::MapEnd => { + self.buffer.push(b'}'); + } + merde_core::Event::ArrayStart(_) => { + self.buffer.push(b'['); + self.stack.push_back(StackFrame::Array { first: true }); + } + merde_core::Event::ArrayEnd => { + panic!("array end without array start"); + } + merde_core::Event::Bytes(_) => { + // figure out what to do with those? maybe base64, maybe an array of + // integers? unclear. maybe it should be a serializer setting. + } + } + Ok(()) + } } impl JsonSerializer { /// Uses the provided buffer as the target for serialization. pub fn from_vec(vec: Vec) -> Self { - JsonSerializer { buffer: vec } + JsonSerializer { + buffer: vec, + stack: Default::default(), + } } /// Allocates a new buffer for serialization. @@ -36,69 +146,6 @@ impl JsonSerializer { Self::default() } - /// Writes the JSON `null` value. - pub fn write_null(&mut self) { - self.buffer.extend_from_slice(b"null"); - } - - /// Writes the JSON `true` or `false` value. - pub fn write_bool(&mut self, value: bool) { - self.buffer - .extend_from_slice(if value { b"true" } else { b"false" }); - } - - /// Write a number as a JSON number. Numbers bigger than 2**53 might - /// not be parsed correctly by other implementations. - pub fn write_i64(&mut self, value: i64) { - let _ = write!(self.buffer, "{}", value); - } - - /// Write a floating-point number as a JSON number. - pub fn write_f64(&mut self, value: f64) { - let _ = write!(self.buffer, "{}", value); - } - - /// Write a string, with escaping. - pub fn write_str(&mut self, value: &str) { - self.buffer.push(b'"'); - for c in value.chars() { - match c { - '"' => self.buffer.extend_from_slice(b"\\\""), - '\\' => self.buffer.extend_from_slice(b"\\\\"), - '\n' => self.buffer.extend_from_slice(b"\\n"), - '\r' => self.buffer.extend_from_slice(b"\\r"), - '\t' => self.buffer.extend_from_slice(b"\\t"), - c if c.is_control() => { - let _ = write!(self.buffer, "\\u{:04x}", c as u32); - } - c => self.buffer.extend_from_slice(c.to_string().as_bytes()), - } - } - self.buffer.push(b'"'); - } - - /// This writes the opening brace of an object, and gives you - /// a guard object to write the key-value pairs. When the guard - /// is dropped, the closing brace is written. - pub fn write_map(&mut self) -> MapGuard<'_> { - self.buffer.push(b'{'); - MapGuard { - serializer: self, - first: true, - } - } - - /// This writes the opening bracket of an array, and gives you - /// a guard object to write the elements. When the guard - /// is dropped, the closing bracket is written. - pub fn write_arr(&mut self) -> ArrayGuard<'_> { - self.buffer.push(b'['); - ArrayGuard { - serializer: self, - first: true, - } - } - /// Get back the internal buffer pub fn into_inner(self) -> Vec { self.buffer @@ -112,397 +159,21 @@ impl JsonSerializer { } } -/// Allows writing JSON objects -pub struct MapGuard<'a> { - serializer: &'a mut JsonSerializer, - first: bool, -} - -impl<'a> MapGuard<'a> { - /// Writes a key-value pair to the object. - #[inline] - pub fn pair(&mut self, key: &str, value: &dyn JsonSerialize) -> &mut Self { - if !self.first { - self.serializer.buffer.push(b','); - } - self.first = false; - self.serializer.write_str(key); - self.serializer.buffer.push(b':'); - value.json_serialize(self.serializer); - self - } -} - -impl<'a> Drop for MapGuard<'a> { - #[inline] - fn drop(&mut self) { - self.serializer.buffer.push(b'}'); - } -} - -/// A guard object for writing an array. -pub struct ArrayGuard<'a> { - serializer: &'a mut JsonSerializer, - first: bool, -} - -impl<'a> ArrayGuard<'a> { - /// Writes an element to the array. - #[inline] - pub fn elem(&mut self, value: &dyn JsonSerialize) -> &mut Self { - if !self.first { - self.serializer.buffer.push(b','); - } - self.first = false; - value.json_serialize(self.serializer); - self - } -} - -impl<'a> Drop for ArrayGuard<'a> { - #[inline] - fn drop(&mut self) { - self.serializer.buffer.push(b']'); - } -} - -/// Implemented by anything that can be serialized to JSON. -/// -/// Default implementations are provided for primitive types, strings, arrays, -/// HashMap, Option, and slices of tuples (for when you don't _need_ the -/// "hash" part of the HashMap). -/// -/// `u64` and `i64` numbers, even those bigger than 2**53, are written as numbers, not strings, -/// which might trip up other JSON parsers. If that's a concern, consider writing numbers -/// as strings yourself, or sticking to `u32`. -/// -/// Empty maps and vectors are written as `{}` and `[]`, respectively, not omitted. -/// -/// `None` Options are omitted, not written as `null`. There is no way to specify a -/// struct field that serializes to `null` at the moment (a custom implementation could -/// use `Value::Null` internally). -pub trait JsonSerialize { - /// Write self to a `JsonSerializer`. - fn json_serialize(&self, s: &mut JsonSerializer); - +/// Adds convenience methods to serializable objects to write them as JSON +pub trait JsonSerialize: Serialize + Sized { /// Allocate a new `Vec` and serialize self to it. - fn to_json_bytes(&self) -> Vec { + fn to_json_bytes(&self) -> Result, MerdeJsonError<'static>> { let mut s = JsonSerializer::new(); - self.json_serialize(&mut s); - s.into_inner() + s.serialize(self)?; + Ok(s.into_inner()) } /// Serialize self to a `String`. - fn to_json_string(&self) -> String { + fn to_json_string(&self) -> Result> { // SAFETY: This is safe because we know that the JSON serialization // produced by `to_json_bytes` will always be valid UTF-8. - unsafe { String::from_utf8_unchecked(self.to_json_bytes()) } - } -} - -impl JsonSerialize for Value<'_> { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - match self { - Value::Null => serializer.write_null(), - Value::Bool(b) => serializer.write_bool(*b), - Value::I64(i) => serializer.write_i64(*i), - Value::U64(u) => serializer.write_i64(*u as i64), - Value::Float(f) => serializer.write_f64(f.into_inner()), - Value::Str(s) => serializer.write_str(s), - Value::Bytes(_b) => { - // TODO: we're going to need to make json_serialize return - // an error at some point, aren't we. - panic!("cannot serialize bytes as JSON") - } - Value::Array(arr) => arr.json_serialize(serializer), - Value::Map(map) => map.json_serialize(serializer), - } - } -} - -impl JsonSerialize for Map<'_> { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_map(); - for (key, value) in self.iter() { - guard.pair(key, value); - } - } -} - -impl JsonSerialize for Array<'_> { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - for value in self.iter() { - guard.elem(value); - } - } -} - -impl JsonSerialize for &T -where - T: ?Sized + JsonSerialize, -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let this: &T = self; - JsonSerialize::json_serialize(this, serializer) - } -} - -impl JsonSerialize for String { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_str(self) - } -} - -impl<'s> JsonSerialize for &'s str { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_str(self) - } -} - -impl<'s> JsonSerialize for Cow<'s, str> { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_str(self) - } -} - -impl<'s> JsonSerialize for CowStr<'s> { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_str(self) - } -} - -impl JsonSerialize for u8 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for u16 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for u32 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for u64 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for i8 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for i16 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for i32 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for i64 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self); - } -} - -impl JsonSerialize for usize { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for isize { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_i64(*self as i64); - } -} - -impl JsonSerialize for f32 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_f64(*self as f64); - } -} - -impl JsonSerialize for f64 { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_f64(*self); - } -} - -impl JsonSerialize for bool { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - serializer.write_bool(*self); - } -} - -impl, V: JsonSerialize, S> JsonSerialize for HashMap { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_map(); - for (key, value) in self { - guard.pair(key.as_ref(), value); - } - } -} - -impl JsonSerialize for Vec { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - for value in self { - guard.elem(value); - } - } -} - -impl JsonSerialize for Option { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - match self { - Some(value) => value.json_serialize(serializer), - None => serializer.write_null(), - } - } -} - -impl JsonSerialize for &[T] { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - for value in *self { - guard.elem(value); - } - } -} - -impl JsonSerialize for (T1,) { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - } -} - -impl JsonSerialize for (T1, T2) { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - } -} - -impl JsonSerialize for (T1, T2, T3) { - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - } -} - -impl JsonSerialize - for (T1, T2, T3, T4) -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - guard.elem(&self.3); - } -} - -impl< - T1: JsonSerialize, - T2: JsonSerialize, - T3: JsonSerialize, - T4: JsonSerialize, - T5: JsonSerialize, - > JsonSerialize for (T1, T2, T3, T4, T5) -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - guard.elem(&self.3); - guard.elem(&self.4); - } -} - -impl< - T1: JsonSerialize, - T2: JsonSerialize, - T3: JsonSerialize, - T4: JsonSerialize, - T5: JsonSerialize, - T6: JsonSerialize, - > JsonSerialize for (T1, T2, T3, T4, T5, T6) -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - guard.elem(&self.3); - guard.elem(&self.4); - guard.elem(&self.5); - } -} - -impl< - T1: JsonSerialize, - T2: JsonSerialize, - T3: JsonSerialize, - T4: JsonSerialize, - T5: JsonSerialize, - T6: JsonSerialize, - T7: JsonSerialize, - > JsonSerialize for (T1, T2, T3, T4, T5, T6, T7) -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - guard.elem(&self.3); - guard.elem(&self.4); - guard.elem(&self.5); - guard.elem(&self.6); - } -} - -impl< - T1: JsonSerialize, - T2: JsonSerialize, - T3: JsonSerialize, - T4: JsonSerialize, - T5: JsonSerialize, - T6: JsonSerialize, - T7: JsonSerialize, - T8: JsonSerialize, - > JsonSerialize for (T1, T2, T3, T4, T5, T6, T7, T8) -{ - fn json_serialize(&self, serializer: &mut JsonSerializer) { - let mut guard = serializer.write_arr(); - guard.elem(&self.0); - guard.elem(&self.1); - guard.elem(&self.2); - guard.elem(&self.3); - guard.elem(&self.4); - guard.elem(&self.5); - guard.elem(&self.6); - guard.elem(&self.7); + let res = unsafe { String::from_utf8_unchecked(self.to_json_bytes()?) }; + Ok(res) } } @@ -521,6 +192,9 @@ pub enum MerdeJsonError<'s> { /// The JSON source, if available source: Option>, }, + + /// Tried to serialize bytes to JSON + JsonDoesNotSupportBytes, } impl<'s> MerdeJsonError<'s> { @@ -532,6 +206,7 @@ impl<'s> MerdeJsonError<'s> { MerdeJsonError::JiterError { err, source: _ } => { MerdeJsonError::JiterError { err, source: None } } + MerdeJsonError::JsonDoesNotSupportBytes => MerdeJsonError::JsonDoesNotSupportBytes, } } @@ -544,6 +219,7 @@ impl<'s> MerdeJsonError<'s> { err, source: source.map(|s| s.into_static()), }, + MerdeJsonError::JsonDoesNotSupportBytes => MerdeJsonError::JsonDoesNotSupportBytes, } } } @@ -588,6 +264,12 @@ impl<'s> std::fmt::Display for MerdeJsonError<'s> { } Ok(()) } + MerdeJsonError::JsonDoesNotSupportBytes => { + write!( + f, + "tried to serialize bytes to JSON (bytes are not supported)" + ) + } } } } @@ -626,12 +308,12 @@ where } /// Serialize the given data structure as a String of JSON. -pub fn to_string(value: &T) -> String { +pub fn to_string(value: &T) -> Result> { value.to_json_string() } /// Serialize the given data structure as a JSON byte vector. -pub fn to_vec(value: &T) -> Vec { +pub fn to_vec(value: &T) -> Result, MerdeJsonError<'static>> { value.to_json_bytes() } diff --git a/merde_msgpack/src/lib.rs b/merde_msgpack/src/lib.rs index 146e6b4..ac30daa 100644 --- a/merde_msgpack/src/lib.rs +++ b/merde_msgpack/src/lib.rs @@ -123,8 +123,8 @@ impl<'s> merde_core::Deserializer<'s> for MsgpackDeserializer<'s> { 0xd1 => self.read_i16().map(|v| Event::I64(v as i64)), 0xd2 => self.read_i32().map(|v| Event::I64(v as i64)), 0xd3 => self.read_i64().map(Event::I64), - 0xca => self.read_f32().map(|v| Event::Float(v as f64)), - 0xcb => self.read_f64().map(Event::Float), + 0xca => self.read_f32().map(|v| Event::F64(v as f64)), + 0xcb => self.read_f64().map(Event::F64), 0xa0..=0xbf => { let len = (byte & 0x1f) as usize; self.read_str(len) diff --git a/merde_yaml/src/lib.rs b/merde_yaml/src/lib.rs index 2bd01e1..a85f85d 100644 --- a/merde_yaml/src/lib.rs +++ b/merde_yaml/src/lib.rs @@ -114,7 +114,7 @@ impl<'s> Deserializer<'s> for YamlDeserializer<'s> { }), }, "float" => match s.parse::() { - Ok(v) => Ok(Event::Float(v)), + Ok(v) => Ok(Event::F64(v)), Err(_) => Err(MerdeYamlError::ParseError { expected_type: "float", }), @@ -137,7 +137,7 @@ impl<'s> Deserializer<'s> for YamlDeserializer<'s> { } else if let Ok(v) = s.parse::() { Ok(Event::I64(v)) } else if let Ok(v) = s.parse::() { - Ok(Event::Float(v)) + Ok(Event::F64(v)) } else if s == "~" || s == "null" { Ok(Event::Null) } else {