Skip to content

Commit 09938b2

Browse files
authored
Merge pull request #2205 from microsoftgraph/bugfixes/OrderedHashtableSerialization
Fixes serialization of ordered hashtables and dictionaries
2 parents 1a04ab6 + 37c2308 commit 09938b2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/readme.graph.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,18 @@ directive:
361361
{
362362
return $;
363363
} else {
364+
// Add using namespaces to class.
365+
let namespaceRegex = /(namespace.*.Models\n\{)/gm
366+
$ = $.replace(namespaceRegex,'$1\n\tusing System.Linq;');
367+
364368
// Change XmlDateTimeSerializationMode from Unspecified to Utc.
365369
let strToDateTimeRegex = /(XmlConvert\.ToDateTime\(.*,.*XmlDateTimeSerializationMode\.)Unspecified/gm
366370
$ = $.replace(strToDateTimeRegex, '$1Utc');
367371
372+
// Use case-insensitive dictionary when deserializing from dictionaries/OrderedHashtables.
373+
let deserializeFromDictionaryRegex = /(.*DeserializeFromDictionary\(.*IDictionary.*\n.*\{.*\n.*new.*\()content(\);)/gm
374+
$ = $.replace(deserializeFromDictionaryRegex, '$1(content.Cast<global::System.Collections.DictionaryEntry>().ToDictionary(kvp => kvp.Key as string, kvp => kvp.Value, global::System.StringComparer.OrdinalIgnoreCase))$2');
375+
368376
return $;
369377
}
370378
# Modify generated .cs model classes.
@@ -538,10 +546,19 @@ directive:
538546
{
539547
return $;
540548
} else {
549+
// Add using namespaces to class.
550+
let namespaceRegex = /(namespace.*.Runtime\n\{)/gm
551+
$ = $.replace(namespaceRegex,'$1\n\tusing System.Linq;');
552+
541553
// Changes excludes hashset to a case-insensitive hashset.
542554
let fromJsonRegex = /(\s*FromJson<\w*>\s*\(JsonObject\s*json\s*,\s*System\.Collections\.Generic\.IDictionary.*)(\s*)({)/gm
543555
$ = $.replace(fromJsonRegex, '$1$2$3\n$2 if (excludes != null){ excludes = new System.Collections.Generic.HashSet<string>(excludes, global::System.StringComparer.OrdinalIgnoreCase);}');
544556
557+
// Serialize OrderedDictionary
558+
let enumerableRegex = /(if.*\(value.*IEnumerable.*\))/gm
559+
let orderedSerializerImpl = 'if (value is System.Collections.Specialized.OrderedDictionary ovalue) { return JsonSerializable.ToJson((ovalue?.Cast<global::System.Collections.DictionaryEntry>().ToDictionary(kvp => kvp.Key as string, kvp => kvp.Value, global::System.StringComparer.OrdinalIgnoreCase)), null);}';
560+
$ = $.replace(enumerableRegex, `${orderedSerializerImpl}\n\n$1`)
561+
545562
return $;
546563
}
547564

0 commit comments

Comments
 (0)