diff --git a/changelog/sortJson.dd b/changelog/sortJson.dd new file mode 100644 index 0000000..fd3dc54 --- /dev/null +++ b/changelog/sortJson.dd @@ -0,0 +1,3 @@ +Sort JSON + +JSON files are now sorted before being written to dub.json. This is to prevent the order of the JSON properties from changing when dub.json is updated. diff --git a/source/dub/internal/vibecompat/data/json.d b/source/dub/internal/vibecompat/data/json.d index 9d2a7b6..3671ddf 100644 --- a/source/dub/internal/vibecompat/data/json.d +++ b/source/dub/internal/vibecompat/data/json.d @@ -1779,24 +1779,38 @@ case Json.Type.object: dst.put('{'); bool first = true; - foreach( string k, ref const Json e; json ){ - if( e.type == Json.Type.undefined ) continue; - if( !first ) dst.put(','); - first = false; - static if (pretty) { + + static if (pretty) { + import std.algorithm.sorting : sort; + string[] keyOrder; + foreach (string key, ref const Json e; json) keyOrder ~= key; + keyOrder.sort(); + + foreach( key; keyOrder ){ + if( json[key].type == Json.Type.undefined ) continue; + if( !first ) dst.put(','); + first = false; dst.put('\n'); foreach (tab; 0 .. level+1) dst.put('\t'); + dst.put('\"'); + jsonEscape(dst, key); + dst.put(pretty ? `": ` : `":`); + writeJsonString!(R, pretty)(dst, json[key], level+1); } - dst.put('\"'); - jsonEscape(dst, k); - dst.put(pretty ? `": ` : `":`); - writeJsonString!(R, pretty)(dst, e, level+1); - } - static if (pretty) { if (json.length > 0) { dst.put('\n'); foreach (tab; 0 .. level) dst.put('\t'); } + } else { + foreach( string k, ref const Json e; json ){ + if( e.type == Json.Type.undefined ) continue; + if( !first ) dst.put(','); + first = false; + dst.put('\"'); + jsonEscape(dst, k); + dst.put(pretty ? `": ` : `":`); + writeJsonString!(R, pretty)(dst, e, level+1); + } } dst.put('}'); break; @@ -1818,13 +1832,6 @@ 1, {} ] -}` || a.toPrettyString() == -`{ - "b": [ - 1, - {} - ], - "a": [] }`); }