-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Json stringify replacer #402
Conversation
8126de4
to
167e6d6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking pretty good for me. I like the idea of creating a new object if needed, but I would add a fast path in the case that args 1 and 2 are not passed, just returning the JSON of the provided object, it should improve performance in most cases.
dd229aa
to
3f1d992
Compare
Once I get the symbol thing figured out, I should be able to wrap this up pretty quickly. |
89b487d
to
7e06dd0
Compare
I checked the spec and thought of an arguably simpler design: pub fn stringify(_: &mut Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
let object = args.get(0).expect("cannot get argument for JSON.stringify");
if args.len() == 1 {
return Ok(Value::from(object.to_json().to_string()));
}
let replacer = args.get(1).expect("first element disappeared");
if !replacer.is_object() {
return Ok(Value::from(object.to_json().to_string()));
}
let replacer = replacer.as_object().expect("replacer was an object");
if replacer.is_callable() {
unimplemented!("replacer function");
} else if replacer.kind == ObjectKind::Array {
let mut obj_to_return = serde_json::Map::with_capacity(replacer.properties.len() - 1);
for field in replacer.properties.iter().filter_map(|(key, prop)| {
if key == "length" {
None
} else {
// TODO: this should be the abstract operation `Get`
prop.value.as_ref().map(Value::to_string)
}
}) {
if let Some(value) = object
.get_property(&field)
.map(|prop| prop.value.as_ref().map(|v| v.to_json()))
.flatten()
{
obj_to_return.insert(field, value);
}
}
Ok(Value::from(JSONValue::Object(obj_to_return).to_string()))
} else {
Ok(Value::from(object.to_json().to_string()))
}
} Note that I didn't implement the function replacer and that I repeated The idea behind this is to avoid all that nesting. Then, instead of directly looping through all properties, I filtered the property iterator and mapped it to just return the string values. Then, I just start creating a I did a bit of a heavy use of the |
7fc41a2
to
6fcacf2
Compare
@Razican -- thanks for taking the time to write something up. I've implemented the array replacer functionality per your suggestion. Still working on the function replacer.
That is what I was hoping to get rid of. |
Made an attempt at using Options in the function replacer branch. |
@Razican thoughts on how this is looking? |
I will check this today :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some comments, give them a look :)
f32e4ad
to
17b54dc
Compare
@Razican --- addressed all of your comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good! almost ready, I just added a couple of comments, one to improve a bit how we call the function and the other one to take advantage of a change that was just merged to master :)
17b54dc
to
600e560
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good to go for me :) we should at some point review how we write tests, they are a bit too wordy, and I think we can improve them, but it's not directly related to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! check my comments to see on how we can improve it :)
@HalidOdat -- addressed your comments. Added a test for the case where nothing is passed to stringify. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good, check my comments to improve readability a bit :)
@Razican @HalidOdat -- I think that I have all comments addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! :)
Closes #345
Fixes #403
Additionally, wrote and ignored some tests that should be addressed once #405 is fixed.