Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ src/packages/Newtonsoft.Json**
/src/TestResults
/src/.cr/*
/src/NJsonSchema.Benchmark/BenchmarkDotNet.Artifacts*
_ReSharper.Caches
14 changes: 7 additions & 7 deletions src/NJsonSchema.Benchmark/NJsonSchema.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
<NoWarn>$(NoWarn),xUnit1013</NoWarn>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="*.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NBench" Version="2.0.1" />
<PackageReference Include="Pro.NBench.xUnit" Version="2.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration.CSharp\NJsonSchema.CodeGeneration.CSharp.csproj" />
Expand Down
15 changes: 14 additions & 1 deletion src/NJsonSchema.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Runtime.CompilerServices;

namespace NJsonSchema.Benchmark
{
public static class Program
{
public static void Main(string[] args)
{
// RunCsharpBenchmark();
//RunCsharpBenchmark();
BenchmarkDotNet.Running.BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAllJoined();
}

Expand All @@ -13,6 +16,16 @@ private static void RunCsharpBenchmark()
var benchmark = new CsharpGeneratorBenchmark();
benchmark.Setup().GetAwaiter().GetResult();
benchmark.GenerateFile();
ExecuteBenchmarkMultiple(benchmark.GenerateFile);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ExecuteBenchmarkMultiple(Action a)
{
for (int i = 0; i < 100; ++i)
{
a();
}
}
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

//// Assert
Assert.Contains(@" public partial class Foo : Anonymous
Assert.Contains(@" public partial class Foo : Anonymous
{
[Newtonsoft.Json.JsonProperty(""prop1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop1 { get; set; }

[Newtonsoft.Json.JsonProperty(""prop2"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop2 { get; set; }
".Replace("\r", string.Empty), code);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp5.0</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1998,1591</NoWarn>
<NoWarn>$(NoWarn),1587,1998,1591,618</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Remove="References\A.json" />
<None Remove="References\Animal.json" />
<None Remove="References\B.json" />
<None Remove="References\C.json" />
<None Remove="References\D.json" />
<None Remove="References\E.json" />
<Content Include="References\*.json" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<Content Include="References\A.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\Animal.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\B.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\C.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\D.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\E.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" Condition="'$(TargetFramework)' == 'netcoreapp5.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.2" Condition="'$(TargetFramework)' == 'net452'" />

<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp5.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<Reference Condition="'$(TargetFramework)' == 'net452'" Include="System.ComponentModel.DataAnnotations">
</Reference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" Condition="'$(TargetFramework)' == 'net5.0'" />
<PackageReference Condition="'$(TargetFramework)' == 'net5.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration.CSharp\NJsonSchema.CodeGeneration.CSharp.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public async Task When_property_is_optional_and_GenerateNullableOptionalProperti
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public object? Property { get; set; }= default!;", code);
Assert.Contains("public object Property2 { get; set; }= default!;", code);
Assert.Contains("public object? Property { get; set; } = default!;", code);
Assert.Contains("public object Property2 { get; set; } = default!;", code);
}

[Fact]
Expand Down Expand Up @@ -145,7 +145,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public object? Property { get; set; }= default!;", code);
Assert.Contains("public object? Property { get; set; } = default!;", code);
Assert.Contains("public object Property2 { get; set; } = new object();", code);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.5.2</Version>
<PackageTags>json schema validation generator .net</PackageTags>
Expand All @@ -17,10 +17,6 @@
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.3|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<None Remove="Templates\Class.Body.liquid" />
<None Remove="Templates\Class.Constructor.Record.liquid" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{% assign skipComma = true -%}
{%- assign skipComma = true %}
{% if HasInheritance %}
{% assign parentProperties = BaseClass.AllProperties -%}
{%- assign parentProperties = BaseClass.AllProperties %}
{% else %}
{% assign parentProperties = "" | empty -%}
{%- assign parentProperties = "" | empty %}
{% endif %}

{% assign sortedProperties = AllProperties | sort: "Name" -%}
{% assign sortedParentProperties = parentProperties | sort: "Name" -%}
{%- assign sortedProperties = AllProperties | sort: "Name" %}
{%- assign sortedParentProperties = parentProperties | sort: "Name" %}

{% if UseSystemTextJson -%}
{%- if UseSystemTextJson %}
[System.Text.Json.Serialization.JsonConstructor]
{% else -%}
{%- else %}
[Newtonsoft.Json.JsonConstructor]
{% endif -%}
{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties -%}{% if skipComma -%}{% assign skipComma = false %}{% else %}, {% endif -%} {{ property.Type }} @{{ property.Name | lowercamelcase }}{% endfor -%})
{% assign skipComma = true -%}
{% if HasInheritance -%}
: base({% for property in sortedParentProperties -%}{% if skipComma -%}{% assign skipComma = false %}{% else %}, {% endif -%}{{ property.Name | lowercamelcase }}{% endfor -%})
{% endif -%}
{%- endif %}
{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Type }} @{{ property.Name | lowercamelcase }}{% endfor %})
{%- assign skipComma = true %}
{%- if HasInheritance %}
: base({%- for property in sortedParentProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Name | lowercamelcase }}{%- endfor %})
{%- endif %}
{
{% for property in Properties -%}
{%- for property in Properties %}
this.{{property.PropertyName}} = @{{property.Name | lowercamelcase}};
{% endfor -%}
{%- endfor %}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
public static {{ ClassName }} FromJson(string data)
{
{% if UseSystemTextJson -%}
{%- if UseSystemTextJson %}
var options = {{ JsonSerializerParameterCode }};
{% if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" -%}
{%- if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" %}
var converters = {{ JsonConvertersArrayCode }};
foreach(var converter in converters)
options.Converters.Add(converter);
{% endif -%}
{%- endif %}
return System.Text.Json.JsonSerializer.Deserialize<{{ ClassName }}>(data, options);
{% else -%}
{%- else %}
return Newtonsoft.Json.JsonConvert.DeserializeObject<{{ ClassName }}>(data, {{ JsonSerializerParameterCode }});
{% endif -%}
{%- endif %}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% if RenderInpc -%}
{% if HasInheritance %}: {{ BaseClassName }}{% else %}: System.ComponentModel.INotifyPropertyChanged{% endif %}
{% elsif RenderPrism -%}
{% if HasInheritance %}: {{ BaseClassName }}{% else %}: Prism.Mvvm.BindableBase{% endif %}
{% else -%}
{% if HasInheritance %}: {{ BaseClassName }}{% endif %}
{% endif -%}
{%- if RenderInpc %}
{% if HasInheritance %} : {{ BaseClassName }}{% else %} : System.ComponentModel.INotifyPropertyChanged{% endif %}
{%- elsif RenderPrism %}
{% if HasInheritance %} : {{ BaseClassName }}{% else %} : Prism.Mvvm.BindableBase{% endif %}
{%- else %}
{% if HasInheritance %} : {{ BaseClassName }}{% endif %}
{%- endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public event System.ComponentModel.PropertyChangedEventHandler{% if GenerateNull
protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string{% if GenerateNullableReferenceTypes %}?{% endif %} propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
if (handler != null)
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
{% endif -%}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
public string ToJson()
{
{% if UseSystemTextJson -%}
{%- if UseSystemTextJson %}
var options = {{ JsonSerializerParameterCode }};
{% if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" -%}
{%- if JsonConvertersArrayCode contains "System.Text.Json.Serialization.JsonConverter[]" %}
var converters = {{ JsonConvertersArrayCode }};
foreach(var converter in converters)
options.Converters.Add(converter);
{% endif -%}
{%- endif %}
return System.Text.Json.JsonSerializer.Serialize(this, options);
{% else -%}
{%- else %}
return Newtonsoft.Json.JsonConvert.SerializeObject(this, {{ JsonSerializerParameterCode }});
{% endif -%}
{%- endif %}
}
Loading