Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom format to serialize the json #156

Merged
merged 5 commits into from
Dec 9, 2022

Conversation

gostkin
Copy link
Contributor

@gostkin gostkin commented Dec 5, 2022

  • Impl serialize
  • Impl deserialize

@gostkin gostkin marked this pull request as ready for review December 8, 2022 21:31
Copy link
Contributor

@rooooooooob rooooooooob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general just some small comments.


impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.serialize_str(self.clone().to_string().map_err(|err| std::fmt::Error::custom(format!("Can't serialize {:?}", err)))?.as_str())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a dumb question, but how does this work? We've got use std::fmt::{Display, Formatter}; and Display uses that one, so where do we get serialize_str from? I see it's in serde::fmt::Formatter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically if you can provide a string it will be interpreted / serialized as a string. so I use my to_string() to convert to string and then use the standard serialize_str. Not sure if it's the best approach, but it should work at least

for char in string.graphemes(true) {
let (reset_string, token) = match char {
"\"" => {
if !current_string.is_empty() && current_string.clone().graphemes(true).last().clone().unwrap() == "\\" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the clones necessary here to call graphemes()? The crate seems to take a reference although it's implemented on str not String.

rust/src/json_serialize.rs Show resolved Hide resolved
ParsedValue {
value: Value,
},
ParsedKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment that this contains the : token inside it I guess just to make it completely clear.

cml_arr.extend(vec![true, false].into_iter().map(|b| Value::Bool(b)));
cml_arr.extend(vec![0, u64::MAX].into_iter().map(|integer| Value::Number(BigInt::from(integer))));
cml_arr.extend(vec![0, i64::MAX, i64::MIN].into_iter().map(|integer| Value::Number(BigInt::from(integer))));
cml_arr.extend(vec!["supported_string", ""].into_iter().map(|str| Value::String(String::from(str))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a unicode test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there're some

("\"\\\"\"".to_string(), vec![JsonToken::Quote, JsonToken::String { raw: "y̆\"".to_string() }, JsonToken::Quote], Value::String("y̆\"".to_string())),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you have an idea for better test - pls tell

serde_arr
}

fn generate_array_of_primitives_with_unsupported() -> Vec<Value> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment might help to someone reading this later without knowing why we did this as to why it's called unsupported.

generate_array(generate_objects()),
generate_array(generate_arrays()),
generate_object(generate_arrays()),
generate_object(generate_arrays()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this meant to be generate_object(generate_objects()), to cover all 4 mixes?

@@ -0,0 +1,996 @@
use std::collections::{BTreeMap, VecDeque};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to document why we have this instead of using the serde dependency that we use in other parts of the codebase.

Copy link
Contributor

@rooooooooob rooooooooob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SebastienGllmt SebastienGllmt merged commit 9810a78 into develop Dec 9, 2022
@SebastienGllmt SebastienGllmt deleted the egostkin/change-serialization branch December 9, 2022 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants