Skip to content

Commit

Permalink
Add streaming JSON serialization for BlkField with serde (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareFlo committed Jan 15, 2024
1 parent 820ac3e commit 045e0b2
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/blk/plaintext_serialize/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{collections::HashMap, mem, str::FromStr, sync::Arc};

use color_eyre::Report;
use serde::ser::{SerializeMap, SerializeSeq, SerializeStruct};

Check warning on line 4 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `SerializeMap`
use serde::Serializer;
use serde_json::{json, Number, Value};
use serde_json::ser::PrettyFormatter;

use crate::blk::{blk_structure::BlkField, blk_type::BlkType};
use crate::blk::blk_type::BlkString;
Expand Down Expand Up @@ -109,12 +113,60 @@ impl BlkField {
),
}
}

pub fn as_serde_json_streaming(self, w: &mut serde_json::Serializer<Vec<u8>, PrettyFormatter>, apply_overrides: bool) -> Result<(), Report> {
#[inline(always)]
fn std_num(num: f32) -> Value {
Value::Number(Number::from_str(&format!("{:?}", num)).expect("Infallible"))
}

match self {
BlkField::Value(k, v) => {

Check warning on line 124 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `k`
match v {
BlkType::Str(s) => {
w.serialize_str(&s).unwrap();
}
BlkType::Int(s) => {
w.serialize_i32(s).unwrap();
}
BlkType::Int2(s) => {
let mut seq = w.serialize_seq(Some(2)).unwrap();
seq.serialize_element(&s).unwrap();
SerializeSeq::end(seq).unwrap();
}
BlkType::Int3(s) => {

Check warning on line 137 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
()
}
BlkType::Long(s) => {

Check warning on line 140 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
()
}
BlkType::Float(s) => (),

Check warning on line 143 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
BlkType::Float2(s) => (),

Check warning on line 144 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
BlkType::Float3(s) => (),

Check warning on line 145 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
BlkType::Float4(s) => (),

Check warning on line 146 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
BlkType::Float12(s) => {}

Check warning on line 147 in src/blk/plaintext_serialize/json.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `s`
BlkType::Bool(s) => {}
BlkType::Color { r, g, b, a } => {}
}
}
BlkField::Struct(k, v) => {
let mut ser = w.serialize_struct("balls", v.len()).unwrap();
for value in v {
ser.serialize_field("test", &value)?;
}
SerializeSeq::end(ser)?;
}
BlkField::Merged(k, v) => {}
}
Ok(())
}
}

#[cfg(test)]
mod test {
use std::fs;
use serde_json::{Number, Value};

use serde_json::{Number, Serializer, Value};

use crate::blk::{blk_structure::BlkField, blk_type::BlkType, make_strict_test, util::blk_str};

Expand Down Expand Up @@ -257,6 +309,17 @@ mod test {
assert_ne!(blk.as_serde_obj(true), expected);
}

#[test]
fn streaming() {
let mut blk = make_strict_test();
// println!("Found: {:#?}", blk.as_serde_obj());
// println!("Expected: {:#?}", expected);
let buf = vec![];
let mut ser = Serializer::pretty(buf);
blk.as_serde_json_streaming(&mut ser, false).unwrap();
println!("{}", String::from_utf8(ser.into_inner()).unwrap());
}

#[test]
fn consistency() {
let mut sample = make_strict_test();
Expand Down

0 comments on commit 045e0b2

Please sign in to comment.