Skip to content

Instantly share code, notes, and snippets.

@Pzixel Pzixel/bad-json.rs
Last active May 7, 2019

Embed
What would you like to do?
#![allow(unused)]
use std::collections::{HashMap, HashSet};
use serde_json::Value;
use std::fs::File;
use std::io::BufReader;
use std::marker::PhantomData;
use std::fmt;
use serde::{Deserialize, Deserializer};
use serde::de::{self, Visitor, SeqAccess};
use std::str::FromStr;
const FILE_BUFFER_SIZE: usize = 50000;
struct BadU64Visitor;
impl<'de> Visitor<'de> for BadU64Visitor {
type Value = u64;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Expected integer string or number")
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(value)
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
value.parse().map_err(|e| E::custom(format!("Cannot parse: {:?}", e)))
}
}
fn deserialize_bad_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_any(BadU64Visitor)
}
fn deserialize_bad_u64_vec<'de, D>(deserializer: D) -> Result<Vec<u64>, D::Error>
where
D: de::Deserializer<'de>,
{
struct BadVecVisitor;
struct BadU64(u64);
impl<'de> Deserialize<'de> for BadU64 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_bad_u64(deserializer).map(Self)
}
}
impl<'de> Visitor<'de> for BadVecVisitor {
type Value = Vec<u64>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Expected vector or single number")
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
BadU64Visitor.visit_u64(value).map(|x| vec!(x))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
BadU64Visitor.visit_str(value).map(|x| vec!(x))
}
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut result = Vec::new();
let mut seq = seq;
while let Some(value) = seq.next_element::<BadU64>()? {
result.push(value.0)
}
Ok(result)
}
}
deserializer.deserialize_any(BadVecVisitor)
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum CompanyName {
Bare(String),
Wrapped { name: String },
}
#[derive(Deserialize, Debug)]
struct CompanyDebt {
#[serde(deserialize_with = "deserialize_bad_u64")]
pub debt: u64,
#[serde(deserialize_with = "deserialize_bad_u64_vec")]
pub phones: Vec<u64>,
pub company: CompanyName
}
struct Debetors {
}
impl<'de> Visitor<'de> for DebetorsVisitor
{
type Value = Debetors;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a nonempty sequence of numbers")
}
fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
where
S: SeqAccess<'de>,
{
// Update the max while there are additional values.
while let Some(debt) = seq.next_element::<CompanyDebt>()? {
println!("found new element {:?}", debt);
}
Ok(Debetors {})
}
}
struct DebetorsVisitor;
impl<'de> Deserialize<'de> for Debetors {
fn deserialize<D>(deserializer: D) -> Result<Debetors, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(DebetorsVisitor)
}
}
fn main() {
let filename = std::env::args().nth(1).expect("filename");
let tbegin = std::time::SystemTime::now();
let res = get_res(&filename);
println!("Done");
// for (di, d) in res.all.iter().enumerate() {
// println!("-------------------------------");
// println!("#{}: debt: {}", di, &d.debt);
// println!("companies: {:?}\nphones: {:?}", &d.companies, &d.phones);
// }
}
fn get_res(path: &str) {
let file = File::open(path).unwrap();
let reader = BufReader::with_capacity(FILE_BUFFER_SIZE, file);
let out: Debetors = serde_json::from_reader(reader).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.