Skip to content

Commit 8ed8cb6

Browse files
authored
Optimize JsonSchema.TypeRaw handling (#1795)
* don't reset in ResetTypeRaw setter as it call Type setter which already resets * only reset if type changes
1 parent 1ba28e9 commit 8ed8cb6

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/NJsonSchema/JsonSchema.Serialization.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,9 @@ internal object? TypeRaw
317317
}
318318
set
319319
{
320-
if (value is JArray)
321-
{
322-
Type = ((JArray) value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()));
323-
}
324-
else
325-
{
326-
Type = ConvertStringToJsonObjectType(value as string);
327-
}
328-
329-
ResetTypeRaw();
320+
Type = value is JArray array
321+
? array.Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString()))
322+
: ConvertStringToJsonObjectType(value as string);
330323
}
331324
}
332325

@@ -336,20 +329,16 @@ private void ResetTypeRaw()
336329
_typeRaw = new Lazy<object?>(() =>
337330
{
338331
var flags = JsonObjectTypes
339-
.Where(v => Type.HasFlag(v))
332+
.Where(x => Type.HasFlag(x))
333+
.Select(object (x) => new JValue(x.ToString().ToLowerInvariant()))
340334
.ToArray();
341335

342-
if (flags.Length > 1)
336+
return flags.Length switch
343337
{
344-
return new JArray(flags.Select(f => new JValue(f.ToString().ToLowerInvariant())));
345-
}
346-
347-
if (flags.Length == 1)
348-
{
349-
return new JValue(flags[0].ToString().ToLowerInvariant());
350-
}
351-
352-
return null;
338+
> 1 => new JArray(flags),
339+
1 => flags[0],
340+
_ => null
341+
};
353342
});
354343
}
355344

src/NJsonSchema/JsonSchema.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,14 @@ public IReadOnlyDictionary<string, JsonSchemaProperty> ActualProperties
396396
[JsonIgnore]
397397
public JsonObjectType Type
398398
{
399-
get => _type; set
399+
get => _type;
400+
set
400401
{
401-
_type = value;
402-
ResetTypeRaw();
402+
if (_type != value)
403+
{
404+
_type = value;
405+
ResetTypeRaw();
406+
}
403407
}
404408
}
405409

0 commit comments

Comments
 (0)