Skip to content

Commit

Permalink
use raw string literals (#40206)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren authored Mar 26, 2024
1 parent 714925f commit ad1f26c
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public static void Run()
// <Deserialize>
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new DictionaryTKeyEnumTValueConverter());
weatherForecast = JsonSerializer.Deserialize<WeatherForecastWithEnumDictionary>(jsonString, deserializeOptions);
weatherForecast = JsonSerializer.Deserialize<WeatherForecastWithEnumDictionary>(
jsonString,
deserializeOptions
);
// </Deserialize>
weatherForecast!.DisplayPropertyValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ public class DeserializeCaseInsensitive
public static void Run()
{
string jsonString =
@"{
""date"": ""2019-08-01T00:00:00-07:00"",
""temperatureCelsius"": 25,
""summary"": ""Hot""
}";
"""
{
"date": "2019-08-01T00:00:00-07:00",
"temperatureCelsius": 25,
"summary": "Hot"
}
""";
Console.WriteLine($"JSON input:\n{jsonString}\n");

// <Deserialize>
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options);
WeatherForecast? weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options);
// </Deserialize>
weatherForecast!.DisplayPropertyValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ public class DeserializeCommasComments
public static void Run()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00-07:00"",
""TemperatureC"": 25, // Fahrenheit 77
""Summary"": ""Hot"", /* Zharko */
// Comments on
/* separate lines */
}";
"""
{
"Date": "2019-08-01T00:00:00-07:00",
"TemperatureC": 25, // Fahrenheit 77
"Summary": "Hot", /* Zharko */
// Comments on
/* separate lines */
}
""";
Console.WriteLine($"JSON input:\n{jsonString}\n");

// <Deserialize>
Expand All @@ -22,7 +24,10 @@ public static void Run()
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options)!;
WeatherForecast weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(
jsonString,
options
)!;
// </Deserialize>
weatherForecast.DisplayPropertyValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,32 @@ public class Program
public static void Main()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00-07:00"",
""TemperatureCelsius"": 25,
""Summary"": ""Hot"",
""DatesAvailable"": [
""2019-08-01T00:00:00-07:00"",
""2019-08-02T00:00:00-07:00""
],
""TemperatureRanges"": {
""Cold"": {
""High"": 20,
""Low"": -10
},
""Hot"": {
""High"": 60,
""Low"": 20
}
},
""SummaryWords"": [
""Cool"",
""Windy"",
""Humid""
]
}
";
"""
{
"Date": "2019-08-01T00:00:00-07:00",
"TemperatureCelsius": 25,
"Summary": "Hot",
"DatesAvailable": [
"2019-08-01T00:00:00-07:00",
"2019-08-02T00:00:00-07:00"
],
"TemperatureRanges": {
"Cold": {
"High": 20,
"Low": -10
},
"Hot": {
"High": 60,
"Low": 20
}
},
"SummaryWords": [
"Cool",
"Windy",
"Humid"
]
}
""";

WeatherForecast? weatherForecast =
JsonSerializer.Deserialize<WeatherForecast>(jsonString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ public class DeserializeIgnoreNull
public static void Run()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00-07:00"",
""TemperatureCelsius"": 25,
""Summary"": null
}";
"""
{
"Date": "2019-08-01T00:00:00-07:00",
"TemperatureCelsius": 25,
"Summary": null
}
""";
Console.WriteLine($"JSON input:\n{jsonString}\n");

// Deserialize default behavior
var weatherForecast = JsonSerializer.Deserialize<WeatherForecastWithDefault>(jsonString)!;
WeatherForecastWithDefault weatherForecast = JsonSerializer.Deserialize<WeatherForecastWithDefault>(jsonString)!;
weatherForecast.DisplayPropertyValues();

// <Deserialize>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public override JsonConverter CreateConverter(

JsonConverter converter = (JsonConverter)Activator.CreateInstance(
typeof(DictionaryEnumConverterInner<,>).MakeGenericType(
new Type[] { keyType, valueType }),
[keyType, valueType]),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: new object[] { options },
args: [options],
culture: null)!;

return converter;
Expand Down Expand Up @@ -113,7 +113,7 @@ public override void Write(

foreach ((TKey key, TValue value) in dictionary)
{
var propertyName = key.ToString();
string propertyName = key.ToString();
writer.WritePropertyName
(options.PropertyNamingPolicy?.ConvertName(propertyName) ?? propertyName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public class Program
{
public static void Main()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00"",
""TemperatureCelsius"": 25,
""Summary"": ""Hot""
}
";
string jsonString = """
{
"Date": "2019-08-01T00:00:00",
"TemperatureCelsius": 25,
"Summary": "Hot"
}
""";
WeatherForecast? weatherForecast;

// <DeserializeWithTypeInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public class Program
{
public static void Main()
{
string jsonString =
@"{
""date"": ""2019-08-01T00:00:00"",
""temperatureCelsius"": 25,
""summary"": ""Hot""
}
";
string jsonString = """
{
"date": "2019-08-01T00:00:00",
"temperatureCelsius": 25,
"summary": "Hot"
}
""";
WeatherForecast? weatherForecast;

// <Deserialize>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class Program
{
public static void Main()
{
string jsonString =
@"{
""Date"": ""2019-08-01T00:00:00"",
""TemperatureCelsius"": 25,
""Summary"": ""Hot""
}
";
string jsonString = """
{
"Date": "2019-08-01T00:00:00",
"TemperatureCelsius": 25,
"Summary": "Hot"
}
""";
WeatherForecast? weatherForecast;

// Deserialize with context that selects metadata mode only for WeatherForecast only.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JsonDocumentWithJsonSerializerOptions;
Expand All @@ -9,7 +7,7 @@ public class Program
{
public static void Main()
{
Person person = new Person { Name = "Nancy" };
Person person = new() { Name = "Nancy" };

// Default serialization - Address property included with null token.
// Output: {"Name":"Nancy","Address":null}
Expand All @@ -28,15 +26,14 @@ public static void Main()
// Ignore null properties doesn't work when serializing JsonDocument instance
// by using JsonSerializer.
// Output: {"Name":"Nancy","Address":null}
var personJsonDocument = JsonSerializer.Deserialize<JsonDocument>(personJsonWithNull);
JsonDocument? personJsonDocument = JsonSerializer.Deserialize<JsonDocument>(personJsonWithNull);
personJsonWithNull = JsonSerializer.Serialize(personJsonDocument, options);
Console.WriteLine(personJsonWithNull);
}
}
public class Person
{
public string? Name { get; set; }

public string? Address { get; set; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,42 @@ public class Program
{
public static void Main()
{
string jsonString =
@"{
""Class Name"": ""Science"",
""Teacher\u0027s Name"": ""Jane"",
""Semester"": ""2019-01-01"",
""Students"": [
{
""Name"": ""John"",
""Grade"": 94.3
},
{
""Name"": ""James"",
""Grade"": 81.0
},
{
""Name"": ""Julia"",
""Grade"": 91.9
},
{
""Name"": ""Jessica"",
""Grade"": 72.4
},
{
""Name"": ""Johnathan""
}
],
""Final"": true
}
";
string jsonString = """
{
"Class Name": "Science",
"Teacher\u0027s Name": "Jane",
"Semester": "2019-01-01",
"Students": [
{
"Name": "John",
"Grade": 94.3
},
{
"Name": "James",
"Grade": 81.0
},
{
"Name": "Julia",
"Grade": 91.9
},
{
"Name": "Jessica",
"Grade": 72.4
},
{
"Name": "Johnathan"
}
],
"Final": true
}
""";
double sum = 0;
int count = 0;

JsonNode document = JsonNode.Parse(jsonString)!;

JsonNode root = document.Root;
JsonArray studentsArray = root["Students"]!.AsArray();

count = studentsArray.Count;

int count = studentsArray.Count;
foreach (JsonNode? student in studentsArray)
{
if (student?["Grade"] is JsonNode gradeNode)
Expand Down
Loading

0 comments on commit ad1f26c

Please sign in to comment.