questions/50702505/json-net-deserialization-child-properties-that-come-as-an-array-when-empty

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	


    // https://stackoverflow.com/questions/50702505/json-net-deserialization-child-properties-that-come-as-an-array-when-empty

    public static class JsonExtensions
    {
        public static TJToken RemoveEmptyArrayProperties<TJToken>(this TJToken root) where TJToken : JToken
        {
            var container = root as JContainer;
            if (container == null)
                return root;
            var query = container.DescendantsAndSelf()
                .OfType<JProperty>()
                .Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
            foreach (var property in query.ToList())
            {
                property.Remove();
            }

            return root;
        }
    }

    public class Foo
    {
        public string bar { get; set; }
    }

    public class RootObject
    {
        public Foo foo { get; set; }
    }
    class TestClass
    {
        public static void Test()
        {
            foreach (var jsonString in GetJson())
            {
                var root = JObject.Parse(jsonString)
                    .RemoveEmptyArrayProperties()
                    .ToObject<RootObject>();
				var newJson = JsonConvert.SerializeObject(root);
				Console.WriteLine(newJson);
            }
        }

        static string[] GetJson()
        {
            var json = new[]
            {
                @"{
    ""foo"": {
        ""bar"": ""hello world""
    }
    }",
                @"{
        ""foo"": []
    }",

            };
            return json;
        }
    }

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}

    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
;