CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class Type
{
public int Mushrooms { get; set; }
public int Olives { get; set; }
public int Zucchini { get; set; }
public int Pepperoni { get; set; }
// Test to verify that set-only properties do not cause problems.
int shouldNotBeHere1;
public int ShouldNotBeHere1 { set { shouldNotBeHere1 = value; } }
// Test to verify that private-get properties do not cause problems.
int shouldNotBeHere2;
public int ShouldNotBeHere2 { set { shouldNotBeHere2 = value; } private get { return shouldNotBeHere2; } }
// Test to verify that indexed properties do not cause problems.
public int this[int index]
{
get { return 0; }
set { }
}
// Test to verify that static properties are not included.
public static int ShouldNotBeHere3 { get; set; }
}
public class Rootobject
{
public List<Names> cols { get; set; }
public List<Row> rows { get; set; }
}
public class Names
{
public string label { get; set; }
public string type { get; set; }
}
public class Row
{
public List<C> c { get; set; }
}
public class C
{
public object v { get; set; }
}
class TestClass
{
static string GetDesiredJson()
{
var json = @"{
""cols"": [
{
label: ""Types"",
type: ""string""
},
{
label: ""values"",
type: ""number""
}
],
""rows"": [
{
c: [
{
v: ""Mushrooms""
},
{
v: 3
},
]
},
{
c: [
{
v: ""Olives""
},
{
v: 31
}
]
},
{
c: [
{
v: ""Zucchini""
},
{
v: 1
},
]
},
{
c: [
{
v: ""Pepperoni""
},
{
v: 2
},
]
}
]
}";
return json;
}
public static void Test()
{
var types = new Type { Mushrooms = 3, Olives = 31, Zucchini = 1, Pepperoni = 2 };
var query = types.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetIndexParameters().Length == 0 && p.GetGetMethod() != null && p.CanRead)
.Select(p => new Row { c = new List<C> { new C { v = p.Name }, new C { v = p.GetValue(types, new object[0]) } } });
var root = new Rootobject
{
cols = new List<Names> { new Names { label = "Types", type = "string" }, new Names { label = "values", type = "number" } },
rows = query.ToList(),
};
var json = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine("Initial object: ");
Console.WriteLine(JsonConvert.SerializeObject(types, Formatting.Indented));
Console.WriteLine("Serialized JSON: ");
Console.WriteLine(json);
var desiredJson = GetDesiredJson();
if (!JToken.DeepEquals(JToken.Parse(desiredJson), JToken.Parse(json)))
{
throw new InvalidOperationException("!JToken.DeepEquals(JToken.Parse(desiredJson), JToken.Parse(json))");
}
else
{
Console.WriteLine("Desired and actual JSON are equivalent.");
}
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("");
TestClass.Test();
}
}