< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.ExpandoObjectConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/ExpandoObjectConverter.cs
Line coverage
41%
Covered lines: 15
Uncovered lines: 21
Coverable lines: 36
Total lines: 87
Line coverage: 41.6%
Branch coverage
32%
Covered branches: 9
Total branches: 28
Branch coverage: 32.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Write(...)100%210%
Read(...)32.14%1782842.42%
CreateDictionary()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/ExpandoObjectConverter.cs

#LineLine coverage
 1using System.Dynamic;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4
 5namespace Elsa.Workflows.Serialization.Converters;
 6
 7/// <summary>
 8/// Parses a JON string into a dynamic <see cref="ExpandoObject"/>.
 9/// </summary>
 10public sealed class ExpandoObjectConverter : JsonConverter<object>
 11{
 12    /// <inheritdoc />
 13    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
 14    {
 015        JsonSerializer.Serialize(writer, value, value.GetType(), options);
 016    }
 17
 18    /// <inheritdoc />
 19    public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 20    {
 1221        switch (reader.TokenType)
 22        {
 23            case JsonTokenType.Null:
 024                return null!;
 25            case JsonTokenType.False:
 026                return false;
 27            case JsonTokenType.True:
 228                return true;
 29            case JsonTokenType.String:
 230                return reader.GetString()!;
 31            case JsonTokenType.Number:
 32            {
 233                if (reader.TryGetInt32(out var i))
 234                    return i;
 035                if (reader.TryGetInt64(out var l))
 036                    return l;
 37                // BigInteger could be added here.
 38
 039                if (reader.TryGetDouble(out var d))
 040                    return d;
 041                using var doc = JsonDocument.ParseValue(ref reader);
 042                return doc.RootElement.Clone();
 43            }
 44            case JsonTokenType.StartArray:
 45            {
 046                var list = new List<object>();
 047                while (reader.Read())
 48                {
 049                    switch (reader.TokenType)
 50                    {
 51                        default:
 052                            list.Add(Read(ref reader, typeof(object), options));
 053                            break;
 54                        case JsonTokenType.EndArray:
 055                            return list.ToArray();
 56                    }
 57                }
 58
 059                throw new JsonException();
 60            }
 61            case JsonTokenType.StartObject:
 662                var dict = CreateDictionary();
 1263                while (reader.Read())
 64                {
 1265                    switch (reader.TokenType)
 66                    {
 67                        case JsonTokenType.EndObject:
 668                            return dict;
 69                        case JsonTokenType.PropertyName:
 670                            var key = reader.GetString()!;
 671                            reader.Read();
 672                            var value = Read(ref reader, typeof(object), options)!;
 673                            dict.Add(key, value);
 674                            break;
 75                        default:
 076                            throw new JsonException();
 77                    }
 78                }
 79
 080                throw new JsonException();
 81            default:
 082                throw new JsonException($"Unknown token {reader.TokenType}");
 83        }
 084    }
 85
 686    private IDictionary<string, object> CreateDictionary() => new ExpandoObject()!;
 87}