< Summary

Information
Class: Elsa.Http.Parsers.JsonHttpContentParser
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Parsers/JsonHttpContentParser.cs
Line coverage
85%
Covered lines: 17
Uncovered lines: 3
Coverable lines: 20
Total lines: 48
Line coverage: 85%
Branch coverage
50%
Covered branches: 6
Total branches: 12
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Priority()100%11100%
GetSupportsContentType(...)100%11100%
ReadAsync()50%131283.33%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Parsers/JsonHttpContentParser.cs

#LineLine coverage
 1using System.Dynamic;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Elsa.Expressions.Helpers;
 5using Elsa.Http.Contexts;
 6using Elsa.Workflows.Serialization.Converters;
 7
 8namespace Elsa.Http.Parsers;
 9
 10/// <summary>
 11/// Reads application/json and text/json content type streams.
 12/// </summary>
 13public class JsonHttpContentParser : IHttpContentParser
 14{
 15    /// <inheritdoc />
 20616    public int Priority => 0;
 17
 18    /// <inheritdoc />
 20619    public bool GetSupportsContentType(HttpResponseParserContext context) => context.ContentType.Contains("json", String
 20
 21    /// <inheritdoc />
 22    public async Task<object> ReadAsync(HttpResponseParserContext context)
 23    {
 624        var options = new JsonSerializerOptions
 625        {
 626            PropertyNameCaseInsensitive = true
 627        };
 28
 629        options.Converters.Add(new JsonStringEnumConverter());
 30
 631        var content = context.Content;
 632        using var reader = new StreamReader(content, leaveOpen: true);
 633        var json = await reader.ReadToEndAsync();
 634        var returnType = context.ReturnType;
 35
 636        if(returnType == typeof(string))
 037            return json;
 38
 639        if (returnType == null || returnType.IsPrimitive)
 040            return json.ConvertTo(returnType ?? typeof(string))!;
 41
 642        if (returnType != typeof(ExpandoObject) && returnType != typeof(object))
 043            return JsonSerializer.Deserialize(json, returnType, options)!;
 44
 645        options.Converters.Add(new ExpandoObjectConverterFactory());
 646        return JsonSerializer.Deserialize<object>(json, options)!;
 647    }
 48}