Skip to content

Commit

Permalink
supports arrays of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
n14little committed May 14, 2020
1 parent 85b65a5 commit 8126de4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,34 @@ pub fn parse(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
pub fn stringify(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let object = args.get(0).expect("cannot get argument for JSON.stringify");
let object_to_return = Value::new_object(None);
if let Some(arg) = args.get(1) {
match arg.data() {
ValueData::Object(ref obj) => {
let derefed_obj = (*obj).deref();
let borrowed_derefed_obj = derefed_obj.borrow();
if borrowed_derefed_obj.kind == ObjectKind::Array {
for (key, value) in borrowed_derefed_obj.properties.iter() {
println!("{:?}", key);
if let Some(Value(x)) = &value.value {
object.remove_property(&x.to_string());
println!("{:?}", x);
if key != "length" {
object_to_return.set_property(
x.to_string(),
object.get_property(&x.to_string()).unwrap()
);
}
}
}
return Ok(Value::from(object_to_return.to_json().to_string()));
} else {
println!("this is not an array");
panic!("replacer only supports arrays at this time");
}
}
_ => {
panic!("arg.data() second half match")
panic!("replacer only supports arrays at this time");
}
}
}
let json = object.to_json().to_string();
println!("returned value: {:?}", &json);
Ok(Value::from(json))
}

Expand Down

0 comments on commit 8126de4

Please sign in to comment.