From ad1f26c298dc0c80519a94eccd0437dbe733d2f4 Mon Sep 17 00:00:00 2001
From: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
Date: Tue, 26 Mar 2024 10:30:52 -0700
Subject: [PATCH] use raw string literals (#40206)
---
.../csharp/ConvertDictionaryTkeyEnumTValue.cs | 5 +-
.../csharp/DeserializeCaseInsensitive.cs | 14 +++--
.../csharp/DeserializeCommasComments.cs | 21 ++++---
.../how-to/csharp/DeserializeExtra.cs | 51 +++++++--------
.../how-to/csharp/DeserializeIgnoreNull.cs | 14 +++--
.../DictionaryTKeyEnumTValueConverter.cs | 6 +-
.../csharp/BothModesNoOptions.cs | 14 ++---
.../csharp/JsonSerializerOptionsExample.cs | 14 ++---
.../csharp/MetadataOnlyNoOptions.cs | 14 ++---
.../JsonDocumentWithJsonSerializerOptions.cs | 9 +--
.../csharp/JsonNodeAverageGradeExample.cs | 63 +++++++++----------
.../csharp/JsonNodeFromStringExample.cs | 42 ++++++-------
.../csharp/JsonNodePOCOExample.cs | 42 ++++++-------
.../JsonNodeWithJsonSerializerOptions.cs | 5 +-
.../csharp/SystemTextJsonSamples.csproj | 2 +-
15 files changed, 162 insertions(+), 154 deletions(-)
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/ConvertDictionaryTkeyEnumTValue.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/ConvertDictionaryTkeyEnumTValue.cs
index d02dd8c1fe6f9..a140031ad08ad 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/ConvertDictionaryTkeyEnumTValue.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/ConvertDictionaryTkeyEnumTValue.cs
@@ -26,7 +26,10 @@ public static void Run()
//
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new DictionaryTKeyEnumTValueConverter());
- weatherForecast = JsonSerializer.Deserialize(jsonString, deserializeOptions);
+ weatherForecast = JsonSerializer.Deserialize(
+ jsonString,
+ deserializeOptions
+ );
//
weatherForecast!.DisplayPropertyValues();
}
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCaseInsensitive.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCaseInsensitive.cs
index 391f3abff6af1..b045242b5a0f5 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCaseInsensitive.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCaseInsensitive.cs
@@ -7,11 +7,13 @@ 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");
//
@@ -19,7 +21,7 @@ public static void Run()
{
PropertyNameCaseInsensitive = true
};
- var weatherForecast = JsonSerializer.Deserialize(jsonString, options);
+ WeatherForecast? weatherForecast = JsonSerializer.Deserialize(jsonString, options);
//
weatherForecast!.DisplayPropertyValues();
}
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCommasComments.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCommasComments.cs
index 71b7428cdcdf7..d0f590868c7b4 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCommasComments.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeCommasComments.cs
@@ -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");
//
@@ -22,7 +24,10 @@ public static void Run()
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
- var weatherForecast = JsonSerializer.Deserialize(jsonString, options)!;
+ WeatherForecast weatherForecast = JsonSerializer.Deserialize(
+ jsonString,
+ options
+ )!;
//
weatherForecast.DisplayPropertyValues();
}
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs
index 83597c80e89e1..68f3a8fa6b6ae 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeExtra.cs
@@ -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(jsonString);
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeIgnoreNull.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeIgnoreNull.cs
index d795eda5ffbf4..fd74cb40224b8 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeIgnoreNull.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DeserializeIgnoreNull.cs
@@ -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(jsonString)!;
+ WeatherForecastWithDefault weatherForecast = JsonSerializer.Deserialize(jsonString)!;
weatherForecast.DisplayPropertyValues();
//
diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DictionaryTKeyEnumTValueConverter.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DictionaryTKeyEnumTValueConverter.cs
index 6aeda44554463..999e0e3599fb7 100644
--- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DictionaryTKeyEnumTValueConverter.cs
+++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/DictionaryTKeyEnumTValueConverter.cs
@@ -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;
@@ -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);
diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs
index a60d70c09518c..0cd904f646864 100644
--- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs
+++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/BothModesNoOptions.cs
@@ -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;
//
diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs
index 213d5b1b38c6f..ea935213b6a17 100644
--- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs
+++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/JsonSerializerOptionsExample.cs
@@ -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;
//
diff --git a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs
index f98f298c2d4c3..a6b98bc4b1094 100644
--- a/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs
+++ b/docs/standard/serialization/system-text-json/snippets/source-generation/csharp/MetadataOnlyNoOptions.cs
@@ -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.
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonDocumentWithJsonSerializerOptions.cs b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonDocumentWithJsonSerializerOptions.cs
index 7b9b7da2a97e6..5806c4ac8dd64 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonDocumentWithJsonSerializerOptions.cs
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonDocumentWithJsonSerializerOptions.cs
@@ -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;
@@ -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}
@@ -28,7 +26,7 @@ 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(personJsonWithNull);
+ JsonDocument? personJsonDocument = JsonSerializer.Deserialize(personJsonWithNull);
personJsonWithNull = JsonSerializer.Serialize(personJsonDocument, options);
Console.WriteLine(personJsonWithNull);
}
@@ -36,7 +34,6 @@ public static void Main()
public class Person
{
public string? Name { get; set; }
-
public string? Address { get; set; }
}
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeAverageGradeExample.cs b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeAverageGradeExample.cs
index 87ec54f94dd7e..a7a6d3e19e473 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeAverageGradeExample.cs
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeAverageGradeExample.cs
@@ -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)
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeFromStringExample.cs b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeFromStringExample.cs
index 8fa0dc66d6b05..84035afbaabfd 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeFromStringExample.cs
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeFromStringExample.cs
@@ -7,27 +7,27 @@ public class Program
{
public static void Main()
{
- string jsonString =
-@"{
- ""Date"": ""2019-08-01T00:00:00"",
- ""Temperature"": 25,
- ""Summary"": ""Hot"",
- ""DatesAvailable"": [
- ""2019-08-01T00:00:00"",
- ""2019-08-02T00:00:00""
- ],
- ""TemperatureRanges"": {
- ""Cold"": {
- ""High"": 20,
- ""Low"": -10
- },
- ""Hot"": {
- ""High"": 60,
- ""Low"": 20
- }
- }
-}
-";
+ string jsonString = """
+ {
+ "Date": "2019-08-01T00:00:00",
+ "Temperature": 25,
+ "Summary": "Hot",
+ "DatesAvailable": [
+ "2019-08-01T00:00:00",
+ "2019-08-02T00:00:00"
+ ],
+ "TemperatureRanges": {
+ "Cold": {
+ "High": 20,
+ "Low": -10
+ },
+ "Hot": {
+ "High": 60,
+ "Low": 20
+ }
+ }
+ }
+ """;
// Create a JsonNode DOM from a JSON string.
JsonNode forecastNode = JsonNode.Parse(jsonString)!;
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodePOCOExample.cs b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodePOCOExample.cs
index f24efb189f64d..90c3caae51b1d 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodePOCOExample.cs
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodePOCOExample.cs
@@ -19,27 +19,27 @@ public class Program
public static void Main()
{
- string jsonString =
-@"{
- ""Date"": ""2019-08-01T00:00:00"",
- ""Temperature"": 25,
- ""Summary"": ""Hot"",
- ""DatesAvailable"": [
- ""2019-08-01T00:00:00"",
- ""2019-08-02T00:00:00""
- ],
- ""TemperatureRanges"": {
- ""Cold"": {
- ""High"": 20,
- ""Low"": -10
- },
- ""Hot"": {
- ""High"": 60,
- ""Low"": 20
- }
- }
-}
-";
+ string jsonString = """
+ {
+ "Date": "2019-08-01T00:00:00",
+ "Temperature": 25,
+ "Summary": "Hot",
+ "DatesAvailable": [
+ "2019-08-01T00:00:00",
+ "2019-08-02T00:00:00"
+ ],
+ "TemperatureRanges": {
+ "Cold": {
+ "High": 20,
+ "Low": -10
+ },
+ "Hot": {
+ "High": 60,
+ "Low": 20
+ }
+ }
+ }
+ """;
// Parse all of the JSON.
JsonNode forecastNode = JsonNode.Parse(jsonString)!;
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeWithJsonSerializerOptions.cs b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeWithJsonSerializerOptions.cs
index abac15b49063f..24ab82d2c49ad 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeWithJsonSerializerOptions.cs
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/JsonNodeWithJsonSerializerOptions.cs
@@ -9,7 +9,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}
@@ -28,7 +28,7 @@ public static void Main()
// Ignore null properties doesn't work when serializing JsonNode instance
// by using JsonSerializer.
// Output: {"Name":"Nancy","Address":null}
- var personJsonNode = JsonSerializer.Deserialize(personJsonWithNull);
+ JsonNode? personJsonNode = JsonSerializer.Deserialize(personJsonWithNull);
personJsonWithNull = JsonSerializer.Serialize(personJsonNode, options);
Console.WriteLine(personJsonWithNull);
@@ -49,6 +49,7 @@ public static void Main()
Console.WriteLine(personJsonWithNull);
}
}
+
public class Person
{
public string? Name { get; set; }
diff --git a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/SystemTextJsonSamples.csproj b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/SystemTextJsonSamples.csproj
index f3af57da1cab7..7e25878e6c073 100644
--- a/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/SystemTextJsonSamples.csproj
+++ b/docs/standard/serialization/system-text-json/snippets/use-dom-utf8jsonreader-utf8jsonwriter/csharp/SystemTextJsonSamples.csproj
@@ -2,7 +2,7 @@
Exe
- net6.0
+ net8.0
SystemTextJsonSamples.Program
enable
enable