using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
public int[,] MyProperty { get; set; }
public string[,,,] MyProperty4D { get; set; }
public class MultidimensionalArraySchemaProvider : JSchemaGenerationProvider
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
if (CanGenerateSchema(context))
var type = context.ObjectType.GetElementType().MakeArrayType();
for (int i = context.ObjectType.GetArrayRank(); i > 1; i--)
type = typeof(ArrayRow<>).MakeGenericType(type);
return context.Generator.Generate(type);
throw new NotImplementedException();
public override bool CanGenerateSchema(JSchemaTypeGenerationContext context) =>
context.ObjectType.IsArray && context.ObjectType.GetArrayRank() > 1;
[JsonArray(AllowNullItems = false)]
class ArrayRow<T> : List<T> { }
public static void Test()
var generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new MultidimensionalArraySchemaProvider());
var schema = generator.Generate(typeof(MyClass));
Console.WriteLine(schema);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Json.NET Schema version: " + typeof(Newtonsoft.Json.Schema.JSchema).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];