Sha256: 21ea8d118437d5f5f096f7c591d232c333472131bdec5009a664aa8983dd01fd

Contents?: true

Size: 1.96 KB

Versions: 111

Compression:

Stored size: 1.96 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 jTokenReader = (JTokenReader) reader;

            var canonicalDataCase = new CanonicalDataCase();
            serializer.Populate(jTokenReader, canonicalDataCase);
            
            canonicalDataCase.Properties = CanonicalDataCaseJson.ToDictionary(jTokenReader.CurrentToken);
            canonicalDataCase.SetInputParameters(GetInputProperties(canonicalDataCase.Properties));
            canonicalDataCase.DescriptionPath = GetDescriptionPath(jTokenReader.CurrentToken);

            return canonicalDataCase;
        }

        private static string[] GetInputProperties(IDictionary<string, dynamic> properties) => properties.Keys.Except(NonInputProperties).ToArray();

        private static string[] GetDescriptionPath(JToken canonicalDataCaseToken)
        {
            var descriptionPath = new Stack<string>();
            var currentToken = canonicalDataCaseToken;

            while (currentToken != null)
            {
                if (currentToken.Type == JTokenType.Object)
                    descriptionPath.Push(currentToken.SelectToken("description").ToObject<string>());

                currentToken = currentToken.Parent;
            }
            
            return descriptionPath.Where(x => !string.IsNullOrEmpty(x)).ToArray();
        }

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

Version data entries

111 entries across 111 versions & 1 rubygems

Version Path
trackler-2.2.1.50 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.49 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.48 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.47 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.46 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.45 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.44 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.43 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.42 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.41 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs
trackler-2.2.1.40 tracks/csharp/generators/Input/CanonicalDataCaseJsonConverter.cs