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

JsValue::to_json fix integer property keys #4011

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions core/engine/src/value/conversions/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ impl JsValue {
Ok(Value::Array(arr))
} else {
let mut map = Map::new();
let mut value_by_prop_key = |property_key| match obj
.borrow()
.properties()
.get(&property_key)
.and_then(|x| x.value().cloned())
{
Some(val) => val.to_json(context),
None => Ok(Value::Null),
};
Copy link
Contributor

@CrazyboyQCD CrazyboyQCD Sep 29, 2024

Choose a reason for hiding this comment

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

Maybe a bit off, but we could avoid the clone here with extra map:

     .and_then(|x| x.value().map(|v| v.to_json(context)))
{
    Some(val) => val?,
    None => Value::Null,
};

And similar thing can be done here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the worst case it's still just cloning of a smart pointer so I don't think that it has much overhead costs, but ok, done


for index in obj.borrow().properties().index_property_keys() {
let key = index.to_string();

let property_key = PropertyKey::from(index);
let value = value_by_prop_key(property_key)?;

map.insert(key, value);
}

for property_key in obj.borrow().properties().shape.keys() {
let key = match &property_key {
PropertyKey::String(string) => string.to_std_string_escaped(),
Expand All @@ -151,15 +170,7 @@ impl JsValue {
}
};

let value = match obj
.borrow()
.properties()
.get(&property_key)
.and_then(|x| x.value().cloned())
{
Some(val) => val.to_json(context)?,
None => Value::Null,
};
let value = value_by_prop_key(property_key)?;

map.insert(key, value);
}
Expand All @@ -181,7 +192,7 @@ mod tests {
use serde_json::json;

use crate::object::JsArray;
use crate::JsValue;
use crate::{js_string, JsValue};
use crate::{run_test_actions, TestAction};

#[test]
Expand All @@ -200,7 +211,10 @@ mod tests {
-45,
{},
true
]
],
"7.3": "random text",
"100": 1000,
"24": 42
}
"#};

Expand All @@ -217,6 +231,14 @@ mod tests {
assert_eq!(obj.get(js_str!("age"), ctx).unwrap(), 43_i32.into());
assert_eq!(obj.get(js_str!("minor"), ctx).unwrap(), false.into());
assert_eq!(obj.get(js_str!("adult"), ctx).unwrap(), true.into());

assert_eq!(
obj.get(js_str!("7.3"), ctx).unwrap(),
js_string!("random text").into()
);
assert_eq!(obj.get(js_str!("100"), ctx).unwrap(), 1000.into());
assert_eq!(obj.get(js_str!("24"), ctx).unwrap(), 42.into());

{
let extra = obj.get(js_str!("extra"), ctx).unwrap();
let extra = extra.as_object().unwrap();
Expand Down
Loading