-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rust version of sonic to serde-json (#427)
- Loading branch information
1 parent
4305017
commit f2172e6
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use serde::{Deserialize, Serialize, Serializer}; | ||
use std::fs; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let file_name = std::env::args_os() | ||
.nth(1) | ||
.and_then(|s| s.into_string().ok()) | ||
.unwrap_or("sample".to_string()); | ||
let n = std::env::args_os() | ||
.nth(2) | ||
.and_then(|s| s.into_string().ok()) | ||
.and_then(|s| s.parse().ok()) | ||
.unwrap_or(10); | ||
let mut json_str = fs::read(format!("{}.json", file_name))?; | ||
let json: GeoData = sonic_rs::from_slice(&mut json_str)?; | ||
|
||
print_hash(sonic_rs::to_vec(&json)?); | ||
let mut array = Vec::with_capacity(n); | ||
for _i in 0..n { | ||
let json: GeoData = sonic_rs::from_slice(&mut json_str)?; | ||
array.push(json); | ||
} | ||
print_hash(sonic_rs::to_vec(&array)?); | ||
Ok(()) | ||
} | ||
|
||
fn print_hash(bytes: impl AsRef<[u8]>) { | ||
let digest = md5::compute(&bytes); | ||
println!("{:x}", digest); | ||
} | ||
|
||
#[derive(Deserialize, Debug, Default)] | ||
struct MyF64(f64); | ||
|
||
impl Serialize for MyF64 { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
if self.0.fract() == 0.0 { | ||
serializer.serialize_i64(self.0 as i64) | ||
} else { | ||
serializer.serialize_f64(self.0) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct GeoData { | ||
r#type: String, | ||
features: Vec<Feature>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Feature { | ||
r#type: String, | ||
properties: Properties, | ||
geometry: Geometry, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Properties { | ||
name: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Geometry { | ||
r#type: String, | ||
coordinates: Vec<Vec<[MyF64; 2]>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ problems: | |
- 2.rs | ||
- 3.rs | ||
- 4-i.rs | ||
- 5-i.rs | ||
- name: coro-prime-sieve | ||
source: | ||
- 1.rs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters