From 64b152e34bab79b7cd3c96ee7b35b43d1f72900f Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 23 Aug 2023 15:11:12 -0500 Subject: [PATCH] Enable sorting of keys during serde serialization --- CHANGELOG.md | 2 ++ src/lune/builtins/serde/encode_decode.rs | 1 + tests/serde/json/source.luau | 10 +++++----- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eef6b96..99e4710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Serving requests using `net.serve` now processes requests on background threads, also freeing up resources. In the future, this will also allow us to offload heavy tasks such as compression/decompression to background threads. - Groundwork for custom / user-defined require aliases has been implemented, as well as absolute / cwd-relative requires. These will both be exposed as options and be made available to use some time in the future. +- When using the `serde` built-in library, keys are now sorted during serialization. This means that the output of `encode` is now completely deterministic, and wont cause issues when committing generated files to git etc. + ### Fixed - Fixed not being able to pass arguments to the thread using `coroutine.resume`. ([#86]) diff --git a/src/lune/builtins/serde/encode_decode.rs b/src/lune/builtins/serde/encode_decode.rs index edbcc1b..1f83ce4 100644 --- a/src/lune/builtins/serde/encode_decode.rs +++ b/src/lune/builtins/serde/encode_decode.rs @@ -10,6 +10,7 @@ const LUA_SERIALIZE_OPTIONS: LuaSerializeOptions = LuaSerializeOptions::new() .serialize_unit_to_null(false); const LUA_DESERIALIZE_OPTIONS: LuaDeserializeOptions = LuaDeserializeOptions::new() + .sort_keys(true) .deny_recursive_tables(false) .deny_unsupported_types(true); diff --git a/tests/serde/json/source.luau b/tests/serde/json/source.luau index 0919cd0..53dcca1 100644 --- a/tests/serde/json/source.luau +++ b/tests/serde/json/source.luau @@ -1,15 +1,15 @@ -local JSON_STRING = [[{"Hello":"World","Inner":{"Array":[1,2,3]},"Foo":"Bar"}]] +local JSON_STRING = [[{"Foo":"Bar","Hello":"World","Inner":{"Array":[1,3,2]}}]] local JSON_STRING_PRETTY = [[{ + "Foo": "Bar", "Hello": "World", "Inner": { "Array": [ 1, - 2, - 3 + 3, + 2 ] - }, - "Foo": "Bar" + } }]] return {