Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daadf8e

Browse files
Improve perf of JsonValue.CreateFromElement by calling ValueKind once (#104108)
* Improve perf of JsonValue.CreateFromElement by calling JsonElement.ValueKind only once * Use switch statement instead of multiple ifs * Update src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs * Update src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs --------- Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent f8bcb89 commit daadf8e

File tree

1 file changed

+10
-10
lines changed
  • src/libraries/System.Text.Json/src/System/Text/Json/Nodes

1 file changed

+10
-10
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValue.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Collections.Generic;
54
using System.Diagnostics;
65
using System.Diagnostics.CodeAnalysis;
76
using System.Text.Json.Serialization;
@@ -181,18 +180,19 @@ internal static JsonValue CreateFromTypeInfo<T>(T value, JsonTypeInfo<T> jsonTyp
181180

182181
internal static JsonValue? CreateFromElement(ref readonly JsonElement element, JsonNodeOptions? options = null)
183182
{
184-
if (element.ValueKind is JsonValueKind.Null)
183+
switch (element.ValueKind)
185184
{
186-
return null;
187-
}
185+
case JsonValueKind.Null:
186+
return null;
188187

189-
// Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue.
190-
if (element.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
191-
{
192-
ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray();
193-
}
188+
case JsonValueKind.Object or JsonValueKind.Array:
189+
// Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue.
190+
ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray();
191+
return null;
194192

195-
return new JsonValueOfElement(element, options);
193+
default:
194+
return new JsonValueOfElement(element, options);
195+
}
196196
}
197197
}
198198
}

0 commit comments

Comments
 (0)