Skip to content

Commit 24a196d

Browse files
committed
Add NullValueHandling.Ignore Setting
1 parent cef40db commit 24a196d

13 files changed

+406
-24
lines changed

JsonClassGeneratorLib/CodeWriterConfiguration/CSharpCodeWriterConfig.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ public CSharpCodeWriterConfig()
2525
OutputMembers = OutputMembers.AsProperties;
2626
ReadOnlyCollectionProperties = false;
2727
CollectionType = OutputCollectionType.MutableList;
28+
AlwaysUseNullables = false;
29+
NullValueHandlingIgnore = false;
2830
}
2931

3032
public CSharpCodeWriterConfig(
31-
bool usePascalCase,
32-
bool useNestedClasses,
33-
JsonLibrary attributeLibrary,
34-
JsonPropertyAttributeUsage attributeUsage,
35-
OutputTypes outputType,
36-
OutputMembers members,
33+
bool usePascalCase,
34+
bool useNestedClasses,
35+
JsonLibrary attributeLibrary,
36+
JsonPropertyAttributeUsage attributeUsage,
37+
OutputTypes outputType,
38+
OutputMembers members,
3739
bool readOnlyCollectionProperties,
38-
OutputCollectionType collectionType)
40+
OutputCollectionType collectionType,
41+
bool alwaysUseNullables,
42+
bool nullValueHandlingIgnore)
3943
{
4044
this.UsePascalCase = usePascalCase;
4145
this.UseNestedClasses = useNestedClasses;
@@ -44,17 +48,19 @@ public CSharpCodeWriterConfig(
4448
this.OutputType = outputType;
4549
this.OutputMembers = members;
4650
this.ReadOnlyCollectionProperties = readOnlyCollectionProperties;
47-
CollectionType = collectionType;
51+
this.CollectionType = collectionType;
52+
this.AlwaysUseNullables = alwaysUseNullables;
53+
this.NullValueHandlingIgnore = nullValueHandlingIgnore;
4854
}
4955

5056
public bool UsePascalCase { get; set; }
5157
public bool UseNestedClasses { get; set; }
5258
public JsonLibrary AttributeLibrary { get; set; }
59+
public bool NullValueHandlingIgnore { get; set; }
5360
public JsonPropertyAttributeUsage AttributeUsage { get; set; }
5461
public OutputTypes OutputType { get; set; }
5562
public OutputMembers OutputMembers { get; set; }
5663
public bool ReadOnlyCollectionProperties { get; set; }
64+
public bool AlwaysUseNullables { get; set; }
5765
}
58-
59-
6066
}

JsonClassGeneratorLib/CodeWriters/CSharpCodeWriter.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ public string GetTypeName(JsonType type)
363363
case JsonTypeEnum.Anything: return "object";
364364
case JsonTypeEnum.Array: return GetCollectionTypeName(elementTypeName: this.GetTypeName(type.InternalType), config.CollectionType);
365365
case JsonTypeEnum.Dictionary: return "Dictionary<string, " + this.GetTypeName(type.InternalType) + ">";
366-
case JsonTypeEnum.Boolean: return "bool";
367-
case JsonTypeEnum.Float: return "double";
368-
case JsonTypeEnum.Integer: return "int";
369-
case JsonTypeEnum.Long: return "long";
370-
case JsonTypeEnum.Date: return "DateTime";
366+
case JsonTypeEnum.Boolean: return config.AlwaysUseNullables ? "bool?" : "bool";
367+
case JsonTypeEnum.Float: return config.AlwaysUseNullables ? "double?" : "double";
368+
case JsonTypeEnum.Integer: return config.AlwaysUseNullables ? "int?" : "int";
369+
case JsonTypeEnum.Long: return config.AlwaysUseNullables ? "long?" : "long";
370+
case JsonTypeEnum.Date: return config.AlwaysUseNullables ? "DateTime?" : "DateTime";
371371
case JsonTypeEnum.NonConstrained: return "object";
372372
case JsonTypeEnum.NullableBoolean: return "bool?";
373373
case JsonTypeEnum.NullableFloat: return "double?";
@@ -538,14 +538,25 @@ private string GetCSharpJsonAttributeCode(JsonFieldInfo field)
538538
switch (config.AttributeLibrary)
539539
{
540540
case JsonLibrary.NewtonsoftJson:
541-
return $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\")]";
541+
{
542+
if (config.NullValueHandlingIgnore)
543+
return $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\", NullValueHandling = NullValueHandling.Ignore)]";
544+
else
545+
return $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\")]";
546+
}
542547

543548
case JsonLibrary.SystemTextJson:
544549
return $"[{attributeTarget}JsonPropertyName(\"{field.JsonMemberName}\")]";
545550

546551
case JsonLibrary.NewtonsoftAndSystemTextJson:
547-
return $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\")]\r\n" +
548-
$" [{attributeTarget}JsonPropertyName(\"{field.JsonMemberName}\")]";
552+
{
553+
string newtonsoftAttribute = $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\")]";
554+
if (config.NullValueHandlingIgnore) {
555+
newtonsoftAttribute = $"[{attributeTarget}JsonProperty(\"{field.JsonMemberName}\", NullValueHandling = NullValueHandling.Ignore)]";
556+
}
557+
return newtonsoftAttribute + Environment.NewLine + $" [{attributeTarget}JsonPropertyName(\"{field.JsonMemberName}\")]";
558+
}
559+
549560
default:
550561
throw new InvalidOperationException("Unrecognized " + nameof(config.AttributeLibrary) + " value: " + config.AttributeLibrary);
551562
}

TESTS-JSON-to-CSHARP/CreateTest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$TestName = Read-Host -Prompt 'Test Name With Number: THE FORMAT IS [TESTNUMBER]_[TESTNAME], EXAMPLE : 10_TESTNAME.'
22
#$Dir = ($psise.CurrentFile.FullPath -replace "CreateTest.ps1", "")
3-
$Dir = "C:\Users\Hilal\OneDrive\Desktop\Json2CSharpCodeGenerator\TESTS-JSON-to-CSHARP"
3+
$Dir = "C:\Users\Hilal\OneDrive\Desktop\Json2CSharpCodeGenerator\TESTS-JSON-to-CSHARP\"
44
$fullPath = $Dir + "Test_" + $TestName + ".cs"
55
$fullPathInput = $Dir + "Test_" + $TestName + "_INPUT"+ ".txt"
66
$fullPathOutput = $Dir + "Test_" + $TestName + "_OUTPUT"+".txt"

TESTS-JSON-to-CSHARP/TESTS-JSON-to-CSHARP.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
</Reference>
5555
<Reference Include="System" />
5656
<Reference Include="System.Core" />
57+
<Reference Include="System.Numerics" />
5758
<Reference Include="System.Xml" />
5859
</ItemGroup>
5960
<ItemGroup>
@@ -68,6 +69,7 @@
6869
<Compile Include="Test_21_ADD_TWO_ATTRIBUTELIBRAIRY.cs" />
6970
<Compile Include="Test_22_SUPPORT_FOR_NULLABLES.cs" />
7071
<Compile Include="Test_23_ADD_NULLABLE_TYPES_SETTINGS.cs" />
72+
<Compile Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING.cs" />
7173
<Compile Include="Test_5_BASIC_SCENARIO.cs" />
7274
<Compile Include="Test_1_2_SETTINGS_PASCAL.cs" />
7375
<Compile Include="Test_1_3_SETTINGS_FIELDS.cs" />
@@ -85,6 +87,7 @@
8587
<Compile Include="Test_9_HANDLE_NUMBERS.cs" />
8688
</ItemGroup>
8789
<ItemGroup>
90+
<None Include="app.config" />
8891
<None Include="CreateTest.ps1" />
8992
<None Include="packages.config" />
9093
</ItemGroup>
@@ -119,6 +122,11 @@
119122
<Content Include="Test_22_SUPPORT_FOR_NULLABLES_OUTPUT.txt" />
120123
<Content Include="Test_23_ADD_NULLABLE_TYPES_SETTINGS_INPUT.txt" />
121124
<Content Include="Test_23_ADD_NULLABLE_TYPES_SETTINGS_OUTPUT.txt" />
125+
<Content Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_INPUT1.txt" />
126+
<Content Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_INPUT.txt" />
127+
<Content Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT2.txt" />
128+
<Content Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT1.txt" />
129+
<Content Include="Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT.txt" />
122130
<Content Include="Test_5_BASIC_SCENARIO_INPUT.txt" />
123131
<Content Include="Test_5_BASIC_SCENARIO_OUTPUT.txt" />
124132
<Content Include="Test_1_2_SETTINGS_PASCAL_INPUT.txt" />

TESTS-JSON-to-CSHARP/Test_23_ADD_NULLABLE_TYPES_SETTINGS.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Text;
66
using Xamasoft.JsonClassGenerator;
7+
using Xamasoft.JsonClassGenerator.CodeWriterConfiguration;
78
using Xamasoft.JsonClassGenerator.CodeWriters;
89

910
namespace TESTS_JSON_TO_CSHARP
@@ -14,13 +15,17 @@ public class Test_23_ADD_NULLABLE_TYPES_SETTINGS
1415
[TestMethod]
1516
public void Run()
1617
{
17-
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_23_ADD_NULLABLE_TYPES_SETTINGS_INPUT.txt";
18+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_23_ADD_NULLABLE_TYPES_SETTINGS_INPUT.txt";
1819
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_23_ADD_NULLABLE_TYPES_SETTINGS_OUTPUT.txt";
1920
string input = File.ReadAllText(path);
2021
string errorMessage = string.Empty;
21-
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter();
22-
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
23-
jsonClassGenerator.CodeWriter = csharpCodeWriter;
22+
23+
CSharpCodeWriterConfig config = new CSharpCodeWriterConfig();
24+
config.AlwaysUseNullables = true;
25+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(config);
26+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
27+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
28+
2429
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
2530
string resultsCompare = File.ReadAllText(resultPath);
2631
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
dfasdfadfasdf
1+
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
2+
public class Root
3+
{
4+
public bool? testbool { get; set; }
5+
public int? testint { get; set; }
6+
public long? testlong { get; set; }
7+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text;
6+
using Xamasoft.JsonClassGenerator;
7+
using Xamasoft.JsonClassGenerator.CodeWriterConfiguration;
8+
using Xamasoft.JsonClassGenerator.CodeWriters;
9+
10+
namespace TESTS_JSON_TO_CSHARP
11+
{
12+
[TestClass]
13+
public class Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING
14+
{
15+
16+
[TestMethod]
17+
public void ShouldNotAddAttribute()
18+
{
19+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_INPUT.txt";
20+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT2.txt";
21+
string input = File.ReadAllText(path);
22+
string errorMessage = string.Empty;
23+
24+
CSharpCodeWriterConfig config = new CSharpCodeWriterConfig();
25+
config.NullValueHandlingIgnore = true;
26+
27+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(config);
28+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
29+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
30+
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
31+
string resultsCompare = File.ReadAllText(resultPath);
32+
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
33+
}
34+
35+
[TestMethod]
36+
public void NewtonsoftJson()
37+
{
38+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_INPUT.txt";
39+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT.txt";
40+
string input = File.ReadAllText(path);
41+
string errorMessage = string.Empty;
42+
43+
CSharpCodeWriterConfig config = new CSharpCodeWriterConfig();
44+
config.NullValueHandlingIgnore = true;
45+
config.AttributeLibrary = Xamasoft.JsonClassGenerator.Models.JsonLibrary.NewtonsoftJson;
46+
config.AttributeUsage = Xamasoft.JsonClassGenerator.Models.JsonPropertyAttributeUsage.Always;
47+
48+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(config);
49+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
50+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
51+
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
52+
string resultsCompare = File.ReadAllText(resultPath);
53+
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
54+
}
55+
56+
[TestMethod]
57+
public void NewtonsoftAndSystemTextJson()
58+
{
59+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_INPUT1.txt";
60+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_24_ADD_NULLVALUEHANDLINGIGNORE_SETTING_OUTPUT1.txt";
61+
string input = File.ReadAllText(path);
62+
string errorMessage = string.Empty;
63+
64+
CSharpCodeWriterConfig config = new CSharpCodeWriterConfig();
65+
config.NullValueHandlingIgnore = true;
66+
config.AttributeLibrary = Xamasoft.JsonClassGenerator.Models.JsonLibrary.NewtonsoftAndSystemTextJson;
67+
config.AttributeUsage = Xamasoft.JsonClassGenerator.Models.JsonPropertyAttributeUsage.Always;
68+
69+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(config);
70+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
71+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
72+
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
73+
string resultsCompare = File.ReadAllText(resultPath);
74+
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
75+
}
76+
}
77+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"Class1":{
3+
"id":4,
4+
"user_id":"user_id_value",
5+
"awesomeobject":{
6+
"SomeProps1":1,
7+
"SomeProps2":"test"
8+
},
9+
"created_at":"2015-06-02 23:33:90",
10+
"updated_at":"2015-06-02 23:33:90",
11+
"users":[
12+
{
13+
"id":"6",
14+
"name":"Test Child 1",
15+
"created_at":"2015-06-02 23:33:90",
16+
"updated_at":"2015-06-02 23:33:90",
17+
"email":"test@gmail.com"
18+
},
19+
{
20+
"id":"6",
21+
"name":"Test Child 1",
22+
"created_at":"2015-06-02 23:33:90",
23+
"updated_at":"2015-06-02 23:33:90",
24+
"email":"test@gmail.com",
25+
"testanadditionalfield":"tet"
26+
} ]
27+
},
28+
"Class2":{
29+
"SomePropertyOfClass2":"SomeValueOfClass2"
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"Class1":{
3+
"id":4,
4+
"user_id":"user_id_value",
5+
"awesomeobject":{
6+
"SomeProps1":1,
7+
"SomeProps2":"test"
8+
},
9+
"created_at":"2015-06-02 23:33:90",
10+
"updated_at":"2015-06-02 23:33:90",
11+
"users":[
12+
{
13+
"id":"6",
14+
"name":"Test Child 1",
15+
"created_at":"2015-06-02 23:33:90",
16+
"updated_at":"2015-06-02 23:33:90",
17+
"email":"test@gmail.com"
18+
},
19+
{
20+
"id":"6",
21+
"name":"Test Child 1",
22+
"created_at":"2015-06-02 23:33:90",
23+
"updated_at":"2015-06-02 23:33:90",
24+
"email":"test@gmail.com",
25+
"testanadditionalfield":"tet"
26+
} ]
27+
},
28+
"Class2":{
29+
"SomePropertyOfClass2":"SomeValueOfClass2"
30+
}
31+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
2+
public class Awesomeobject
3+
{
4+
[JsonProperty("SomeProps1", NullValueHandling = NullValueHandling.Ignore)]
5+
public int SomeProps1 { get; set; }
6+
7+
[JsonProperty("SomeProps2", NullValueHandling = NullValueHandling.Ignore)]
8+
public string SomeProps2 { get; set; }
9+
}
10+
11+
public class Class1
12+
{
13+
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
14+
public int id { get; set; }
15+
16+
[JsonProperty("user_id", NullValueHandling = NullValueHandling.Ignore)]
17+
public string user_id { get; set; }
18+
19+
[JsonProperty("awesomeobject", NullValueHandling = NullValueHandling.Ignore)]
20+
public Awesomeobject awesomeobject { get; set; }
21+
22+
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
23+
public string created_at { get; set; }
24+
25+
[JsonProperty("updated_at", NullValueHandling = NullValueHandling.Ignore)]
26+
public string updated_at { get; set; }
27+
28+
[JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]
29+
public List<User> users { get; set; }
30+
}
31+
32+
public class Class2
33+
{
34+
[JsonProperty("SomePropertyOfClass2", NullValueHandling = NullValueHandling.Ignore)]
35+
public string SomePropertyOfClass2 { get; set; }
36+
}
37+
38+
public class Root
39+
{
40+
[JsonProperty("Class1", NullValueHandling = NullValueHandling.Ignore)]
41+
public Class1 Class1 { get; set; }
42+
43+
[JsonProperty("Class2", NullValueHandling = NullValueHandling.Ignore)]
44+
public Class2 Class2 { get; set; }
45+
}
46+
47+
public class User
48+
{
49+
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
50+
public string id { get; set; }
51+
52+
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
53+
public string name { get; set; }
54+
55+
[JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
56+
public string created_at { get; set; }
57+
58+
[JsonProperty("updated_at", NullValueHandling = NullValueHandling.Ignore)]
59+
public string updated_at { get; set; }
60+
61+
[JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]
62+
public string email { get; set; }
63+
64+
[JsonProperty("testanadditionalfield", NullValueHandling = NullValueHandling.Ignore)]
65+
public string testanadditionalfield { get; set; }
66+
}
67+

0 commit comments

Comments
 (0)