< Summary

Information
Class: Elsa.Expressions.JavaScript.ObjectConverters.JsonElementConverter
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/ObjectConverters/JsonElementConverter.cs
Line coverage
70%
Covered lines: 12
Uncovered lines: 5
Coverable lines: 17
Total lines: 37
Line coverage: 70.5%
Branch coverage
46%
Covered branches: 6
Total branches: 13
Branch coverage: 46.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryConvert(...)100%22100%
ConvertJsonElementToJsValue(...)36.36%201158.33%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/ObjectConverters/JsonElementConverter.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Json;
 3using System.Text.Json.Nodes;
 4using Jint;
 5using Jint.Native;
 6using Jint.Runtime.Interop;
 7
 8namespace Elsa.Expressions.JavaScript.ObjectConverters;
 9
 10internal class JsonElementConverter : IObjectConverter
 11{
 12    public bool TryConvert(Engine engine, object value, [NotNullWhen(true)] out JsValue? result)
 13    {
 656914        if (value is JsonElement jsonElement)
 15        {
 916            result = ConvertJsonElementToJsValue(engine, jsonElement);
 917            return true;
 18        }
 19
 656020        result = JsValue.Null;
 656021        return false;
 22    }
 23
 24    private static JsValue ConvertJsonElementToJsValue(Engine engine, JsonElement element) =>
 925        element.ValueKind switch
 926        {
 627            JsonValueKind.Object => JsValue.FromObject(engine, JsonObject.Create(element)),
 128            JsonValueKind.Array => JsValue.FromObject(engine, JsonArray.Create(element)),
 129            JsonValueKind.String => JsValue.FromObject(engine, element.GetString()),
 030            JsonValueKind.Number => element.TryGetInt32(out var intValue) ? JsNumber.Create(intValue) : JsNumber.Create(
 031            JsonValueKind.True => JsBoolean.True,
 132            JsonValueKind.False => JsBoolean.False,
 033            JsonValueKind.Undefined => JsValue.Undefined,
 034            JsonValueKind.Null => JsValue.Null,
 035            _ => throw new InvalidOperationException($"Unsupported JsonValueKind: {element.ValueKind}")
 936        };
 37}