Skip to content

Commit b083807

Browse files
Fix comments
1 parent 4eb259a commit b083807

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/libraries/Core/Microsoft.Agents.Core/Serialization/Converters/ConnectorConverter.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace Microsoft.Agents.Core.Serialization.Converters
1515
public abstract class ConnectorConverter<T> : JsonConverter<T> where T : new()
1616
{
1717
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new();
18-
private static readonly ConcurrentDictionary<(Type, bool, JsonNamingPolicy), Dictionary<string, PropertyInfo>> PropertyNameCache = new();
18+
private static readonly ConcurrentDictionary<(Type, bool, Type), Dictionary<string, (PropertyInfo, bool)>> JsonPropertyMetadataCache = new();
19+
1920
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2021
{
2122
if (reader.TokenType != JsonTokenType.StartObject)
@@ -25,7 +26,7 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
2526

2627
var value = new T();
2728

28-
var properties = GetPropertyMap(typeof(T), options.PropertyNameCaseInsensitive, options.PropertyNamingPolicy);
29+
var propertyMetadataMap = GetJsonPropertyMetadata(typeof(T), options.PropertyNameCaseInsensitive, options.PropertyNamingPolicy);
2930

3031
while (reader.Read())
3132
{
@@ -38,9 +39,9 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
3839
{
3940
var propertyName = reader.GetString();
4041

41-
if (properties.ContainsKey(propertyName))
42+
if (propertyMetadataMap.TryGetValue(propertyName, out var entry))
4243
{
43-
ReadProperty(ref reader, value, propertyName, options, properties);
44+
ReadProperty(ref reader, value, propertyName, options, entry.Property);
4445
}
4546
else
4647
{
@@ -58,11 +59,12 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
5859

5960
var type = value.GetType();
6061
var properties = GetCachedProperties(type);
61-
var resolvedNames = GetPropertyMap(type, false, options.PropertyNamingPolicy); // case-insensitivity doesn’t matter here
62+
var propertyMetadataMap = GetJsonPropertyMetadata(type, false, options.PropertyNamingPolicy); // case-insensitivity doesn’t matter here
63+
var reverseMap = propertyMetadataMap.ToDictionary(kv => kv.Value.Property, kv => (kv.Key, kv.Value.IsIgnored));
6264

6365
foreach (var property in properties)
6466
{
65-
if (property.GetCustomAttribute<JsonIgnoreAttribute>() != null)
67+
if (!reverseMap.TryGetValue(property, out var propertyMetadata) || propertyMetadata.IsIgnored)
6668
{
6769
continue;
6870
}
@@ -83,7 +85,7 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
8385
if (propertyValue != null || !(options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull))
8486

8587
{
86-
var propertyName = resolvedNames.FirstOrDefault(kv => kv.Value == property).Key ?? property.Name;
88+
var propertyName = propertyMetadata.Key ?? property.Name;
8789

8890
writer.WritePropertyName(propertyName);
8991

@@ -258,10 +260,8 @@ protected void SetGenericProperty(ref Utf8JsonReader reader, Action<object> sett
258260
setter(deserialized);
259261
}
260262

261-
private void ReadProperty(ref Utf8JsonReader reader, T value, string propertyName, JsonSerializerOptions options, Dictionary<string, PropertyInfo> properties)
263+
private void ReadProperty(ref Utf8JsonReader reader, T value, string propertyName, JsonSerializerOptions options, PropertyInfo property)
262264
{
263-
var property = properties[propertyName];
264-
265265
if (TryReadExtensionData(ref reader, value, property.Name, options))
266266
{
267267
return;
@@ -302,31 +302,32 @@ private static PropertyInfo[] GetCachedProperties(Type type)
302302
return PropertyCache.GetOrAdd(type, static t => t.GetProperties());
303303
}
304304

305-
private static Dictionary<string, PropertyInfo> GetPropertyMap(Type type, bool caseInsensitive, JsonNamingPolicy? namingPolicy)
305+
private static Dictionary<string, (PropertyInfo Property, bool IsIgnored)> GetJsonPropertyMetadata(Type type, bool caseInsensitive, JsonNamingPolicy? namingPolicy)
306306
{
307-
return PropertyNameCache.GetOrAdd((type, caseInsensitive, namingPolicy), static key =>
307+
var cacheKey = (type, caseInsensitive, namingPolicy?.GetType());
308+
return JsonPropertyMetadataCache.GetOrAdd(cacheKey, key =>
308309
{
309-
var (t, insensitive, policy) = key;
310+
var (t, insensitive, _) = key;
310311
var comparer = insensitive ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
311-
var dict = new Dictionary<string, PropertyInfo>(comparer);
312+
var metadata = new Dictionary<string, (PropertyInfo, bool)>(comparer);
312313

313314
foreach (var prop in GetCachedProperties(t))
314315
{
315316
var resolvedName = prop.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name
316-
?? policy?.ConvertName(prop.Name)
317+
?? namingPolicy?.ConvertName(prop.Name)
317318
?? prop.Name;
318319

319-
if (dict.ContainsKey(resolvedName))
320+
if (metadata.ContainsKey(resolvedName))
320321
{
321322
throw new InvalidOperationException(
322323
$"Duplicate JSON property name detected: '{resolvedName}' maps to multiple properties in type '{t.FullName}'."
323324
);
324325
}
325326

326-
dict[resolvedName] = prop;
327+
metadata [resolvedName] = (prop, prop.GetCustomAttribute<JsonIgnoreAttribute>()?.Condition == JsonIgnoreCondition.Always);
327328
}
328329

329-
return dict;
330+
return metadata;
330331
});
331332
}
332333
}

0 commit comments

Comments
 (0)