Skip to content

Commit 86cbb6c

Browse files
committed
Revert "Use List.Sort for ordered dictionary output (#345)"
This reverts commit 852a492.
1 parent 17a854e commit 86cbb6c

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/Argon/Serialization/JsonSerializerInternalWriter.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -896,26 +896,29 @@ void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryCo
896896

897897
var keyContract = contract.KeyContract ??= Serializer.ContractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object));
898898

899-
if (contract is {OrderByKey: true, IsSortable: true})
899+
static IEnumerable<DictionaryEntry> Items(IDictionary values)
900900
{
901-
var entries = new List<DictionaryEntry>();
902901
foreach (DictionaryEntry entry in values)
903902
{
904-
entries.Add(entry);
903+
yield return entry;
905904
}
905+
}
906906

907+
if (contract is {OrderByKey: true, IsSortable: true})
908+
{
907909
if (contract.DictionaryKeyType == typeof(string))
908910
{
909-
entries.Sort((a, b) => StringComparer.OrdinalIgnoreCase.Compare((string) a.Key, (string) b.Key));
911+
foreach (var entry in Items(values).OrderBy(_ => ((string) _.Key, StringComparer.OrdinalIgnoreCase)))
912+
{
913+
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
914+
}
910915
}
911916
else
912917
{
913-
entries.Sort((a, b) => Comparer.Default.Compare(a.Key, b.Key));
914-
}
915-
916-
foreach (var entry in entries)
917-
{
918-
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
918+
foreach (var entry in Items(values).OrderBy(_ => _.Key))
919+
{
920+
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
921+
}
919922
}
920923
}
921924
else

0 commit comments

Comments
 (0)