[Fork] questions/45045546/json-net-serializes-empty-json

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Runtime.Serialization;
using System.Data;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;


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


    // https://stackoverflow.com/questions/45045546/json-net-serializes-empty-json
    //Plant.cs
    public interface ICell
    {
        int Id { get; }
    }
    public interface IEukaryote
    {
        System.Collections.Generic.IEnumerable<ICell> Cells { get; }
        string GenericName { get; }
    }
    public sealed partial class PlantCell
        : ICell
    {
        public int Id { get { return 12324; } }
    }

    public sealed partial class Plant : IEukaryote
    {
        public System.Collections.Generic.IEnumerable<ICell> Cells { get { return (_valuesDict["Cells"] as System.Collections.IEnumerable).Cast<ICell>(); } }
        public string GenericName { get { return _valuesDict["GenericName"] as string; } }
        public string FieldIWantSerialized;
        public int SomethingIDoNotWantSerialized { get { return 99999; } }

        private readonly System.Collections.Generic.IDictionary<string, object> _valuesDict;
        public Plant()
        {
            _valuesDict = new System.Collections.Generic.Dictionary<string, object>();
            var cells = new System.Collections.Generic.List<PlantCell>();
            cells.Add(new PlantCell());
            _valuesDict["Cells"] = cells;
            _valuesDict["GenericName"] = "HousePlant";
            this.FieldIWantSerialized = "FieldIWantSerialized Value";
        }

        public override string ToString()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this,
                new Newtonsoft.Json.JsonSerializerSettings()
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                }
            );
        }
    }

    //Metadata.cs
    [System.ComponentModel.DataAnnotations.MetadataType(typeof(PlantMetadata))]
    public sealed partial class Plant
    {
        [JsonObject(MemberSerialization.OptIn)]
        internal sealed class PlantMetadata
        {
            [JsonProperty]
            public object Cells { get; set; }

            [JsonProperty]
            public object GenericName { get; set; }

            [JsonProperty]
            public object FieldIWantSerialized;
        }
    }

    class TestClass
    {
        public static void Test()
        {
            var plant = new Plant();

			var s = plant.ToString();
			
            Console.WriteLine(s);
			Assert.IsTrue(s.Contains("fieldIWantSerialized"));
			Assert.IsTrue(s.IndexOf("SomethingIDoNotWantSerialized", StringComparison.OrdinalIgnoreCase) == -1);
			Assert.IsTrue(s.Contains("cells"));
			Assert.IsTrue(s.Contains("genericName"));
        }
    }

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

			TestClass.Test();
		}
	}

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

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

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            if (value == false)
                throw new AssertionFailedException("failed");
        }
    }
;