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;
public static partial class JsonExtensions
public static string ToString(this JSchema schema, Formatting formatting, JSchemaWriterSettings settings = default)
using var sw = new StringWriter(CultureInfo.InvariantCulture);
using (var jsonWriter = new JsonTextWriter(sw) { Formatting = formatting })
schema.WriteTo(jsonWriter);
schema.WriteTo(jsonWriter, settings);
public static void Test()
'description': 'A person',
'name': {'type':['string','null']},
'items': {'type':'string'}
var schema = JSchema.Parse(schemaJson);
var json1 = JsonConvert.SerializeObject(schema, Formatting.None);
Console.WriteLine(json1);
var unversionedSchema = schema.ToString(Formatting.None);
Assert.AreEqual(json1, unversionedSchema);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json1), JToken.Parse(schemaJson)));
Assert.IsTrue(!json1.Contains("\n"));
var versionedSchema = schema.ToString(Formatting.None, new JSchemaWriterSettings { Version = SchemaVersion.Draft7 } );
Console.WriteLine(versionedSchema);
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];