-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add System.Text.Json support in DTO generation (#1308)
* Add System.Text.Json support in DTO generation * Update * Improve JsonInheritanceConverter * Add inheritance converter + tests * Update * Update liquid
- Loading branch information
Showing
21 changed files
with
609 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonLibrary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="CSharpClassStyle.cs" company="NJsonSchema"> | ||
// Copyright (c) Rico Suter. All rights reserved. | ||
// </copyright> | ||
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license> | ||
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace NJsonSchema.CodeGeneration.CSharp | ||
{ | ||
/// <summary>The CSharp JSON library to use.</summary> | ||
public enum CSharpJsonLibrary | ||
{ | ||
/// <summary>Use Newtonsoft.Json</summary> | ||
NewtonsoftJson, | ||
|
||
/// <summary>Use System.Text.Json</summary> | ||
SystemTextJson | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.FromJson.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
public static {{ ClassName }} FromJson(string data) | ||
{ | ||
{% if UseSystemTextJson -%} | ||
return System.Text.Json.JsonSerializer.Deserialize<{{ ClassName }}>(data{{ JsonSerializerParameterCode }}); | ||
{% else -%} | ||
return Newtonsoft.Json.JsonConvert.DeserializeObject<{{ ClassName }}>(data{{ JsonSerializerParameterCode }}); | ||
{% endif -%} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.ToJson.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
public string ToJson() | ||
{ | ||
{% if UseSystemTextJson -%} | ||
return System.Text.Json.JsonSerializer.Serialize(this{{ JsonSerializerParameterCode }}); | ||
{% else -%} | ||
return Newtonsoft.Json.JsonConvert.SerializeObject(this{{ JsonSerializerParameterCode }}); | ||
{% endif -%} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")] | ||
{% if UseSystemTextJson -%} | ||
internal class DateFormatConverter : JsonConverter<DateTime> | ||
{ | ||
public override System.DateTime Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
return DateTime.Parse(reader.GetString()); | ||
} | ||
|
||
public override void Write(System.Text.Json.Utf8JsonReader writer, System.DateTime value, System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString("yyyy-MM-dd")); | ||
} | ||
} | ||
{% else -%} | ||
internal class DateFormatConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter | ||
{ | ||
public DateFormatConverter() | ||
{ | ||
DateTimeFormat = "yyyy-MM-dd"; | ||
} | ||
} | ||
} | ||
{% endif -%} |
2 changes: 1 addition & 1 deletion
2
src/NJsonSchema.CodeGeneration.CSharp/Templates/JsonInheritanceAttribute.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.