Skip to content

Commit

Permalink
Merge pull request #970 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v10.0.0-preview.01
  • Loading branch information
RicoSuter authored May 20, 2019
2 parents c110c30 + a878082 commit 913ee00
Show file tree
Hide file tree
Showing 206 changed files with 3,405 additions and 4,773 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ CI NuGet Feed: https://www.myget.org/gallery/njsonschema-ci

**Features:**

- [Read existing JSON Schemas](https://github.com/RSuter/NJsonSchema/wiki/JsonSchema4) and [validate JSON data](https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaValidator) (`JsonSchema4.FromJsonAsync()`)
- [Generate JSON Schema from .NET type via reflection](https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaGenerator) (with support for many attributes/annotations) (`JsonSchema4.FromTypeAsync<MyType>()`)
- [Generate JSON Schema from sample JSON data](https://github.com/RSuter/NJsonSchema/wiki/SampleJsonSchemaGenerator) (`JsonSchema4.FromSampleJson()`)
- [Read existing JSON Schemas](https://github.com/RSuter/NJsonSchema/wiki/JsonSchema) and [validate JSON data](https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaValidator) (`JsonSchema.FromJsonAsync()`)
- [Generate JSON Schema from .NET type via reflection](https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaGenerator) (with support for many attributes/annotations) (`JsonSchema.FromTypeAsync<MyType>()`)
- [Generate JSON Schema from sample JSON data](https://github.com/RSuter/NJsonSchema/wiki/SampleJsonSchemaGenerator) (`JsonSchema.FromSampleJson()`)
- Support for schema references ($ref) (relative, URL and file)
- Generate C# and TypeScript code from JSON Schema
- Support for .NET Core (via PCL 259 / .NET Standard 1.0, also see [XML Documentation](https://github.com/NJsonSchema/NJsonSchema/wiki/XML-Documentation))
Expand All @@ -41,17 +41,17 @@ The project is developed and maintained by [Rico Suter](http://rsuter.com) and o

## NJsonSchema usage

The [JsonSchema4](https://github.com/NJsonSchema/NJsonSchema/wiki/JsonSchema4) class can be used as follows:
The [JsonSchema](https://github.com/NJsonSchema/NJsonSchema/wiki/JsonSchema) class can be used as follows:

```csharp
var schema = await JsonSchema4.FromTypeAsync<Person>();
var schema = await JsonSchema.FromTypeAsync<Person>();
var schemaData = schema.ToJson();
var errors = schema.Validate("{...}");

foreach (var error in errors)
Console.WriteLine(error.Path + ": " + error.Kind);

schema = await JsonSchema4.FromJsonAsync(schemaData);
schema = await JsonSchema.FromJsonAsync(schemaData);
```

The `Person` class:
Expand Down
25 changes: 0 additions & 25 deletions src/NJsonSchema.Benchmark/GeneratorPerformance.cs

This file was deleted.

65 changes: 0 additions & 65 deletions src/NJsonSchema.Benchmark/GeneratorPerformanceTests.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/NJsonSchema.Benchmark/NJsonSchema.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
Expand Down
92 changes: 92 additions & 0 deletions src/NJsonSchema.Benchmark/SchemaGenerationPerformanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using NBench;
using Pro.NBench.xUnit.XunitExtensions;
using Xunit.Abstractions;

namespace NJsonSchema.Benchmark
{
public class SchemaGenerationPerformanceTests
{
private Counter _counter;

public SchemaGenerationPerformanceTests(ITestOutputHelper output)
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new XunitTraceListener(output));
}

[PerfSetup]
#pragma warning disable xUnit1013 // Public method should be marked as test
public void Setup(BenchmarkContext context)
#pragma warning restore xUnit1013 // Public method should be marked as test
{
_counter = context.GetCounter("Iterations");
}

/// <summary>
/// Ensure that we can serialise at least 200 times per second (5ms).
/// </summary>
[NBenchFact]
[PerfBenchmark(
Description = "Ensure schema generation doesn't take too long",
NumberOfIterations = 3,
RunTimeMilliseconds = 1000,
RunMode = RunMode.Throughput,
TestMode = TestMode.Test)]
[CounterThroughputAssertion("Iterations", MustBe.GreaterThan, 100)]
public void GenerateSchema()
{
var schema = JsonSchema.FromTypeAsync<Container>().GetAwaiter().GetResult();
_counter.Increment();
}

public class SpecialTeacher : Teacher
{
public string Foo { get; set; }
}

[KnownType(typeof(SpecialTeacher))]
public class Teacher
{
public string Bar { get; set; }
}

[KnownType(typeof(Teacher))]
public class Person
{
public string Baz { get; set; }
}

public class Pen : WritingInstrument
{
public string Foo { get; set; }
}

public class Pencil : WritingInstrument
{
public string Bar { get; set; }
}

[KnownType("GetKnownTypes")]
public class WritingInstrument
{
public static Type[] GetKnownTypes()
{
return new[] { typeof(Pen), typeof(Pencil) };
}

public string Baz { get; set; }
}

public class Container
{
public Person Person { get; set; }

public Teacher Teacher { get; set; }

public WritingInstrument WritingInstrument { get; set; }
}
}
}
8 changes: 4 additions & 4 deletions src/NJsonSchema.Benchmark/SerializationPerformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NJsonSchema.Benchmark
public class SerializationPerformance
{
private readonly string _json;
private readonly JsonSchema4 _schema;
private readonly JsonSchema _schema;

public SerializationPerformance()
{
Expand All @@ -22,7 +22,7 @@ public SerializationPerformance()
_json = reader.ReadToEnd();
}

_schema = JsonSchema4.FromJsonAsync(_json).Result;
_schema = JsonSchema.FromJsonAsync(_json).Result;
}

[Benchmark]
Expand All @@ -32,9 +32,9 @@ public string ToJson()
}

[Benchmark]
public JsonSchema4 FromJson()
public JsonSchema FromJson()
{
return JsonSchema4.FromJsonAsync(_json).Result;
return JsonSchema.FromJsonAsync(_json).Result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class AbstractClass
public async Task When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated()
{
/// Arrange
var schema = await JsonSchema4.FromTypeAsync<AbstractClass>();
var schema = await JsonSchema.FromTypeAsync<AbstractClass>();

/// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task When_additionalProperties_schema_is_set_for_object_then_specia
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
Expand Down
10 changes: 5 additions & 5 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task When_allOf_has_two_schemas_then_referenced_schema_is_inherited
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task When_allOf_has_one_schema_then_it_is_inherited()
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
Expand Down Expand Up @@ -150,7 +150,7 @@ public async Task When_all_of_has_multiple_refs_then_the_properties_should_expan
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns" };
var generator = new CSharpGenerator(schema, settings);
var output = generator.GenerateFile("Foo");
Expand Down Expand Up @@ -188,7 +188,7 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

Expand Down Expand Up @@ -231,7 +231,7 @@ public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_class
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var code = generator.GenerateFile("Foo");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MyRequiredTest
public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_setter()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<MyRequiredTest>();
var schema = await JsonSchema.FromTypeAsync<MyRequiredTest>();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Expand All @@ -36,8 +36,8 @@ public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_se
var code = generator.GenerateFile();

//// Assert
Assert.Contains("public System.Collections.ObjectModel.ObservableCollection<string> Collection { get; } = new System.Collections.ObjectModel.ObservableCollection<string>();", code);
Assert.Contains("public System.Collections.Generic.Dictionary<string, object> Dictionary { get; } = new System.Collections.Generic.Dictionary<string, object>();", code);
Assert.Contains("public System.Collections.Generic.ICollection<string> Collection { get; } = new System.Collections.ObjectModel.Collection<string>();", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> Dictionary { get; } = new System.Collections.Generic.Dictionary<string, object>();", code);
}
}
}
6 changes: 3 additions & 3 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ArrayTest
public async Task When_array_property_is_required_then_array_instance_can_be_changed()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<ArrayTest>();
var schema = await JsonSchema.FromTypeAsync<ArrayTest>();
var data = schema.ToJson();

//// Act
Expand All @@ -45,7 +45,7 @@ public class ClassWithNullableArrayItems
public async Task When_array_item_is_nullable_then_generated_CSharp_is_correct()
{
// Arrange
var schema = await JsonSchema4.FromTypeAsync<ClassWithNullableArrayItems>();
var schema = await JsonSchema.FromTypeAsync<ClassWithNullableArrayItems>();
var json = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());

Expand All @@ -54,7 +54,7 @@ public async Task When_array_item_is_nullable_then_generated_CSharp_is_correct()

// Assert
Assert.True(schema.Properties["Items"].Item.IsNullable(SchemaType.JsonSchema));
Assert.Contains("System.Collections.ObjectModel.ObservableCollection<int?> Items", output);
Assert.Contains("System.Collections.Generic.ICollection<int?> Items", output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task When_property_has_interger_default_it_is_reflected_in_the_poco
}
}}";

var schema = await JsonSchema4.FromJsonAsync(data);
var schema = await JsonSchema.FromJsonAsync(data);
var settings = new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Expand All @@ -39,7 +39,7 @@ public async Task When_property_has_boolean_default_it_is_reflected_in_the_poco(
}
}}";

var schema = await JsonSchema4.FromJsonAsync(data);
var schema = await JsonSchema.FromJsonAsync(data);
var settings = new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Expand All @@ -62,7 +62,7 @@ public async Task When_property_has_boolean_default_and_default_value_generation
}
}}";

var schema = await JsonSchema4.FromJsonAsync(data);
var schema = await JsonSchema.FromJsonAsync(data);
var settings = new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Expand Down
Loading

0 comments on commit 913ee00

Please sign in to comment.