Skip to content

Commit

Permalink
let JsonSerializer implement Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 4, 2024
1 parent 09b630a commit f82ffb3
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 470 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions merde_core/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)),
Expand Down
10 changes: 5 additions & 5 deletions merde_core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -47,8 +47,8 @@ impl_from_for_event! {
u32 => U64,
u64 => U64,
// floats
f32 => Float,
f64 => Float,
f32 => F64,
f64 => F64,
// misc.
bool => Bool,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl<'s> Event<'s> {

pub fn into_f64(self) -> Result<f64, MerdeError<'static>> {
match self {
Event::Float(f) => Ok(f),
Event::F64(f) => Ok(f),
_ => Err(MerdeError::UnexpectedEvent {
got: EventType::from(&self),
expected: &[EventType::Float],
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/into_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions merde_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion merde_json/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit f82ffb3

Please sign in to comment.