Skip to content

Commit e8c313b

Browse files
Merge branch 'main' into seo/104001-csharp-how-to-create-a-temp-file-in-temp-folder
2 parents 683583c + c0d6f15 commit e8c313b

7 files changed

Lines changed: 168 additions & 15 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace RetrieveJSONProperty.DTOs;
4+
5+
public class Customer
6+
{
7+
[JsonPropertyName("FirstName")]
8+
public string? Name { get; set; }
9+
10+
[JsonIgnore]
11+
public string? Password { get; set; }
12+
13+
public int Age { get; set; }
14+
}

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Helper/JsonHelper.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IEnumerable<string> RetrievalUsingSerialization(object obj)
2222
{
2323
string json = JsonConvert.SerializeObject(obj);
2424

25-
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
25+
var values = JsonConvert.DeserializeObject<Dictionary<string, object?>>(json);
2626

2727
return values != null
2828
? values.Keys

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Helper/JsonTextImplementation.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Reflection;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization.Metadata;
24
using System.Text.Json.Serialization;
35

46
namespace RetrieveJSONProperty.Helper;
@@ -16,4 +18,17 @@ public static string[] GetJsonPropertyNames(object obj)
1618

1719
return propertyNames;
1820
}
19-
}
21+
22+
public static string[] GetSerializedPropertyNames<T>(JsonSerializerOptions? options = null)
23+
{
24+
options ??= JsonSerializerOptions.Default;
25+
options.MakeReadOnly(populateMissingResolver: true);
26+
27+
JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(T));
28+
29+
return typeInfo.Properties
30+
.Where(property => property.Get is not null || property.Set is not null)
31+
.Select(property => property.Name)
32+
.ToArray();
33+
}
34+
}

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Program.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Console.WriteLine("2. ListPropertywithValue");
1212
Console.WriteLine("3. ResolverImplementation");
1313
Console.WriteLine("4. AlternateImplementation();");
14+
Console.WriteLine("5. SerializerMetadataImplementation");
1415

1516
string? option = Console.ReadLine();
1617

@@ -32,6 +33,10 @@
3233
AlternateImplementation();
3334
break;
3435

36+
case "5":
37+
SerializerMetadataImplementation();
38+
break;
39+
3540
case "exit":
3641
continueExecution = false;
3742
break;
@@ -92,6 +97,17 @@ static void AlternateImplementation()
9297
}
9398
}
9499

100+
static void SerializerMetadataImplementation()
101+
{
102+
var propertyNames = JsonTextImplementation.GetSerializedPropertyNames<Sales>();
103+
104+
Console.WriteLine("JSON property names:");
105+
foreach (var propertyName in propertyNames)
106+
{
107+
Console.WriteLine(propertyName);
108+
}
109+
}
110+
95111
static Sales CreateSalesObject()
96112
{
97113
Console.WriteLine("Enter YearlySales:");

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/RetrieveJSONProperty.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
1212
</ItemGroup>
1313

1414
</Project>

‎json-csharp/RetrieveJSONProperty/Tests/RetrieveJSONPropertyUnitTest.cs‎

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json;
2+
13
namespace Tests;
24

35
[TestFixture]
@@ -17,7 +19,7 @@ public void GivenObjectWithJsonPropertyAttribute_WhenRetrievalUsingReflection_Th
1719
var result = JsonHelper.RetrievalUsingReflection(sales);
1820

1921
// Assert
20-
CollectionAssert.AreEquivalent(new[] { "Slx_PER_Year", "Slx_PER_DAY" }, result);
22+
Assert.That(result, Is.EquivalentTo(new[] { "Slx_PER_Year", "Slx_PER_DAY" }));
2123
}
2224

2325
[Test]
@@ -34,7 +36,20 @@ public void GivenSalesObject_WhenRetrievingKeyValuesUsingSerialization_ThenRetur
3436
var result = JsonHelper.RetrievalUsingSerialization(sales);
3537

3638
// Assert
37-
CollectionAssert.AreEqual(new[] { "Slx_PER_Year", "Slx_PER_DAY" }, result);
39+
Assert.That(result, Is.EqualTo(new[] { "Slx_PER_Year", "Slx_PER_DAY" }));
40+
}
41+
42+
[Test]
43+
public void GivenObjectWithNonStringProperty_WhenRetrievingUsingSerialization_ThenReturnsAllNames()
44+
{
45+
// Arrange
46+
var testObject = new NewtonsoftObjectWithNumber { Name = "Widget", Quantity = 3 };
47+
48+
// Act
49+
var result = JsonHelper.RetrievalUsingSerialization(testObject);
50+
51+
// Assert
52+
Assert.That(result, Is.EqualTo(new[] { "product_name", "Quantity" }));
3853
}
3954

4055
[Test]
@@ -51,6 +66,83 @@ public void Given_ObjectWithJsonPropertyAttributes_WhenGetJsonPropertyNames_Then
5166
Assert.That(result, Is.EqualTo(expected));
5267
}
5368

69+
[Test]
70+
public void GivenSalesType_WhenGetSerializedPropertyNames_ThenReturnJsonPropertyNames()
71+
{
72+
// Act
73+
string[] result = JsonTextImplementation.GetSerializedPropertyNames<Sales>();
74+
75+
// Assert
76+
Assert.That(result, Is.EqualTo(new[] { "Slx_PER_Year", "Slx_PER_DAY" }));
77+
}
78+
79+
[Test]
80+
public void GivenWebDefaults_WhenGetSerializedPropertyNames_ThenNamingPolicyAppliesOnlyWithoutAttribute()
81+
{
82+
// Arrange
83+
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
84+
85+
// Act
86+
string[] result = JsonTextImplementation.GetSerializedPropertyNames<TestObjectWithAttributes>(options);
87+
88+
// Assert
89+
Assert.That(result, Is.EqualTo(new[] { "FirstName", "LastName", "age" }));
90+
}
91+
92+
[Test]
93+
public void GivenWebDefaults_WhenSerializing_ThenOutputUsesSameNamesAsGetSerializedPropertyNames()
94+
{
95+
// Arrange
96+
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
97+
var testObject = new TestObjectWithAttributes { Name = "Ana", Surname = "Lee", Age = 30 };
98+
99+
// Act
100+
string json = System.Text.Json.JsonSerializer.Serialize(testObject, options);
101+
string[] namesInJson = JsonDocument.Parse(json).RootElement
102+
.EnumerateObject()
103+
.Select(property => property.Name)
104+
.ToArray();
105+
106+
// Assert
107+
Assert.That(namesInJson,
108+
Is.EqualTo(JsonTextImplementation.GetSerializedPropertyNames<TestObjectWithAttributes>(options)));
109+
}
110+
111+
[Test]
112+
public void GivenIgnoredProperty_WhenGetSerializedPropertyNames_ThenSkipsIt()
113+
{
114+
// Act
115+
string[] result = JsonTextImplementation.GetSerializedPropertyNames<ObjectWithIgnoredProperty>();
116+
117+
// Assert
118+
Assert.That(result, Is.EqualTo(new[] { "id" }));
119+
}
120+
121+
[Test]
122+
public void GivenIgnoredProperty_WhenGetJsonPropertyNames_ThenReflectionStillReturnsIt()
123+
{
124+
// Act
125+
string[] result = JsonTextImplementation.GetJsonPropertyNames(new ObjectWithIgnoredProperty());
126+
127+
// Assert
128+
Assert.That(result, Is.EqualTo(new[] { "id", "Password" }));
129+
}
130+
131+
[Test]
132+
public void GivenCustomer_WhenComparingReflectionWithWebDefaultsMetadata_ThenOnlyMetadataAppliesPolicyAndIgnore()
133+
{
134+
// Arrange
135+
var webOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
136+
137+
// Act
138+
string[] fromReflection = JsonTextImplementation.GetJsonPropertyNames(new Customer());
139+
string[] fromSerializer = JsonTextImplementation.GetSerializedPropertyNames<Customer>(webOptions);
140+
141+
// Assert
142+
Assert.That(fromReflection, Is.EqualTo(new[] { "FirstName", "Password", "Age" }));
143+
Assert.That(fromSerializer, Is.EqualTo(new[] { "FirstName", "age" }));
144+
}
145+
54146
[Test]
55147
public void GivenValidProductObject_WhenGettingJsonPropertyForProductName_ThenReturnCorrectProperty()
56148
{
@@ -65,10 +157,9 @@ public void GivenValidProductObject_WhenGettingJsonPropertyForProductName_ThenRe
65157
var property = ResolverHelper.GetJsonProperty(sales, "Slx_per_day");
66158

67159
// Assert
68-
Assert.IsNotNull(property);
160+
Assert.That(property, Is.Not.Null);
69161
Assert.That(property.PropertyName, Is.EqualTo("Slx_PER_DAY"));
70162
Assert.That(property.PropertyType, Is.EqualTo(typeof(string)));
71-
72163
}
73164

74165
private class TestObjectWithAttributes
@@ -81,4 +172,21 @@ private class TestObjectWithAttributes
81172

82173
public int Age { get; set; }
83174
}
175+
176+
private class NewtonsoftObjectWithNumber
177+
{
178+
[JsonProperty("product_name")]
179+
public string? Name { get; set; }
180+
181+
public int Quantity { get; set; }
182+
}
183+
184+
private class ObjectWithIgnoredProperty
185+
{
186+
[JsonPropertyName("id")]
187+
public int Id { get; set; }
188+
189+
[System.Text.Json.Serialization.JsonIgnore]
190+
public string? Password { get; set; }
191+
}
84192
}

‎json-csharp/RetrieveJSONProperty/Tests/Tests.csproj‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -10,12 +10,12 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
15-
<PackageReference Include="NUnit" Version="3.12.0" />
16-
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
17-
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
18-
<PackageReference Include="coverlet.collector" Version="3.2.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
14+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
15+
<PackageReference Include="NUnit" Version="5.0.0" />
16+
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
17+
<PackageReference Include="NUnit.Analyzers" Version="4.15.0" />
18+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

0 commit comments

Comments
 (0)