Skip to content

Commit

Permalink
Improve perf of JsonValue.CreateFromElement by calling ValueKind once (
Browse files Browse the repository at this point in the history
…dotnet#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 <eirik.tsarpalis@gmail.com>
  • Loading branch information
andrewjsaid and eiriktsarpalis authored Jul 1, 2024
1 parent f8bcb89 commit daadf8e
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -181,18 +180,19 @@ internal static JsonValue CreateFromTypeInfo<T>(T value, JsonTypeInfo<T> jsonTyp

internal static JsonValue? CreateFromElement(ref readonly JsonElement element, JsonNodeOptions? options = null)
{
if (element.ValueKind is JsonValueKind.Null)
switch (element.ValueKind)
{
return null;
}
case JsonValueKind.Null:
return null;

// Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue.
if (element.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
{
ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray();
}
case JsonValueKind.Object or JsonValueKind.Array:
// Force usage of JsonArray and JsonObject instead of supporting those in an JsonValue.
ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray();
return null;

return new JsonValueOfElement(element, options);
default:
return new JsonValueOfElement(element, options);
}
}
}
}

0 comments on commit daadf8e

Please sign in to comment.