Sha256: baa115ea33d059467450733c297767930967f9d29817630f56dd89b96ee1a659

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Generators.Input
{
    public class CanonicalDataCaseJsonConverter : JsonConverter
    {
        private static readonly string[] NonInputProperties = {"description", "property", "expected", "comments"};

        public override bool CanConvert(Type objectType) => typeof(CanonicalDataCase) == objectType;

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jToken = JToken.ReadFrom(reader);

            var canonicalDataCase = new CanonicalDataCase();
            serializer.Populate(new JTokenReader(jToken), canonicalDataCase);

            canonicalDataCase.Properties = jToken.ToObject<IDictionary<string, object>>();
            canonicalDataCase.Input = GetInputProperty(jToken);

            return canonicalDataCase;
        }

        private static object GetInputProperty(JToken jToken)
        {
            var allProperties = jToken.ToObject<IDictionary<string, object>>();

            foreach (var nonInputProperty in NonInputProperties)
                allProperties.Remove(nonInputProperty);

            if (allProperties.Keys.Count == 1)
                return allProperties[allProperties.Keys.First()];

            return allProperties;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
    }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.1.0.52 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.1.0.51 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.1.0.50 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.1.0.49 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs