Skip to content
Merged
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
23 changes: 10 additions & 13 deletions src/Argon/Serialization/JsonSerializerInternalWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,29 +896,26 @@ void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryCo

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

static IEnumerable<DictionaryEntry> Items(IDictionary values)
if (contract is {OrderByKey: true, IsSortable: true})
{
var entries = new List<DictionaryEntry>();
foreach (DictionaryEntry entry in values)
{
yield return entry;
entries.Add(entry);
}
}

if (contract is {OrderByKey: true, IsSortable: true})
{
if (contract.DictionaryKeyType == typeof(string))
{
foreach (var entry in Items(values).OrderBy(_ => ((string) _.Key, StringComparer.OrdinalIgnoreCase)))
{
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
}
entries.Sort((a, b) => StringComparer.OrdinalIgnoreCase.Compare((string) a.Key, (string) b.Key));
}
else
{
foreach (var entry in Items(values).OrderBy(_ => _.Key))
{
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
}
entries.Sort((a, b) => Comparer.Default.Compare(a.Key, b.Key));
}

foreach (var entry in entries)
{
SerializeDictionaryItem(writer, contract, member, entry.Key, entry.Value, keyContract, underlying);
}
}
else
Expand Down