Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/builtins/runtime/logical_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub(super) fn encode_typed(
) -> VmResult<String> {
let schema = prepare_json_schema(descriptor, &names)?;
charge_value(value, &mut 0)?;
let json = encode_value(value, &schema, Position::Value, 0, &names)?;
let mut json = encode_value(value, &schema, Position::Value, 0, &names)?;
// Keep lexical key order even if a downstream crate enables preserve_order.
json.sort_all_objects();
serde_json::to_string(&json)
.map_err(|err| VmError::HostError(format!("typed json encode: {err}")))
}
Expand Down
18 changes: 18 additions & 0 deletions tests/map_json_behavior_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,24 @@ fn unit_scalar_array_and_map_round_trip() {
assert_eq!(reload_eval(source), Value::Bool(true));
}

#[test]
fn typed_json_encode_sorts_nested_object_keys() {
let source = r#"
use json;
struct Payload { stats: [map<int>], label: string }
let mut values: map<int> = {};
values["z"] = 1;
values["a"] = 2;
values["m"] = 3;
let value: Payload = {stats: [values, {}], label: "public"};
json::encode(value);
"#;
assert_eq!(
eval(source),
Value::string(r#"{"label":"public","stats":[{"a":2,"m":3,"z":1},{}]}"#),
);
}

#[test]
fn option_unit_field_none_omits_and_some_encodes_null() {
let source = r#"
Expand Down
Loading