public static void Run()
        {
            string          jsonString;
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <SnippetSerialize>
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = new UpperCaseNamingPolicy(),
                WriteIndented        = true
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </SnippetSerialize>
            Console.WriteLine($"JSON output:\n{jsonString}\n");

            // <SnippetDeserialize>
            options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = new UpperCaseNamingPolicy()
            };
            weatherForecast = JsonSerializer.Deserialize <WeatherForecast>(jsonString, options);
            // </SnippetDeserialize>
            weatherForecast.DisplayPropertyValues();
        }
Exemple #2
0
        public static void Run()
        {
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <SnippetSerialize>
            byte[] jsonUtf8Bytes;
            var    options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(weatherForecast, options);
            // </SnippetSerialize>
            Console.WriteLine($"JSON output:\n{Encoding.UTF8.GetString(jsonUtf8Bytes)}\n");

            // <SnippetDeserialize1>
            var readOnlySpan = new ReadOnlySpan <byte>(jsonUtf8Bytes);

            weatherForecast = JsonSerializer.Deserialize <WeatherForecast>(readOnlySpan);
            // </SnippetDeserialize1>
            weatherForecast.DisplayPropertyValues();

            // <SnippetDeserialize2>
            var utf8Reader = new Utf8JsonReader(jsonUtf8Bytes);

            weatherForecast = JsonSerializer.Deserialize <WeatherForecast>(ref utf8Reader);
            // </SnippetDeserialize2>
            weatherForecast.DisplayPropertyValues();
        }
        public static void Run()
        {
            string          jsonString;
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <Serialize>
            var serializeOptions = new JsonSerializerOptions
            {
                WriteIndented = true,
                Converters    =
                {
                    new DateTimeOffsetConverter()
                }
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions);
            // </Serialize>
            Console.WriteLine($"JSON output:\n{jsonString}\n");

            // <Deserialize>
            var deserializeOptions = new JsonSerializerOptions();

            deserializeOptions.Converters.Add(new DateTimeOffsetConverter());
            weatherForecast = JsonSerializer.Deserialize <WeatherForecast>(jsonString, deserializeOptions);
            // </Deserialize>
            weatherForecast.DisplayPropertyValues();
        }
        public static void Run()
        {
            string          jsonString;
            WeatherForecast weatherForecast =
                WeatherForecastFactories.CreateWeatherForecastCyrillic();

            weatherForecast.DisplayPropertyValues();

            Console.WriteLine("Default serialization - non-ASCII escaped");
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize language sets unescaped");
            // <LanguageSets>
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic),
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </LanguageSets>
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize selected unescaped characters");
            // <SelectedCharacters>
            var encoderSettings = new TextEncoderSettings();

            encoderSettings.AllowCharacters('\u0436', '\u0430');
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(encoderSettings),
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </SelectedCharacters>
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize using unsafe relaxed encoder");
            // <UnsafeRelaxed>
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </UnsafeRelaxed>
            Console.WriteLine(jsonString);
        }
Exemple #5
0
        public static void Run()
        {
            string          jsonString;
            string          fileName        = "WeatherForecast.json";
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <SnippetSerialize>
            jsonString = JsonSerializer.Serialize(weatherForecast);
            File.WriteAllText(fileName, jsonString);
            // </SnippetSerialize>
            Console.WriteLine($"The result is in {fileName}\n");

            // <SnippetDeserialize>
            jsonString      = File.ReadAllText(fileName);
            weatherForecast = JsonSerializer.Deserialize <WeatherForecast>(jsonString);
            // </SnippetDeserialize>
            weatherForecast.DisplayPropertyValues();
        }
        public static async Task RunAsync()
        {
            string          fileName        = "WeatherForecastAsync.json";
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <Serialize>
            using FileStream createStream = File.Create(fileName);
            await JsonSerializer.SerializeAsync(createStream, weatherForecast);

            // </Serialize>
            Console.WriteLine($"The result is in {fileName}\n");

            // <Deserialize>
            using FileStream openStream = File.OpenRead(fileName);
            weatherForecast             = await JsonSerializer.DeserializeAsync <WeatherForecast>(openStream);

            // </Deserialize>
            weatherForecast.DisplayPropertyValues();
        }
        public static async Task RunAsync()
        {
            string          fileName        = "WeatherForecastAsync.json";
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.DisplayPropertyValues();

            // <SnippetSerialize>
            using (FileStream fs = File.Create(fileName))
            {
                await JsonSerializer.SerializeAsync(fs, weatherForecast);
            }
            // </SnippetSerialize>
            Console.WriteLine($"The result is in {fileName}\n");

            // <SnippetDeserialize>
            using (FileStream fs = File.OpenRead(fileName))
            {
                weatherForecast = await JsonSerializer.DeserializeAsync <WeatherForecast>(fs);
            }
            // </SnippetDeserialize>
            weatherForecast.DisplayPropertyValues();
        }
        public static void Run()
        {
            string          jsonString;
            WeatherForecast weatherForecast = WeatherForecastFactories.CreateWeatherForecast();

            weatherForecast.Summary = null;
            weatherForecast.DisplayPropertyValues();

            // <Serialize>
            var options = new JsonSerializerOptions
            {
                IgnoreNullValues = true,
                WriteIndented    = true
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </Serialize>
            Console.WriteLine(jsonString);
            Console.WriteLine();
        }
Exemple #9
0
        public static void Run()
        {
            string          jsonString;
            var             weatherForecastDerived = WeatherForecastFactories.CreateWeatherForecastDerived();
            WeatherForecast weatherForecast        = weatherForecastDerived;

            weatherForecast.DisplayPropertyValues();

            Console.WriteLine("Base class generic type - derived class properties omitted");
            // <SnippetSerializeDefault>
            var serializeOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonString = JsonSerializer.Serialize <WeatherForecast>(weatherForecast, serializeOptions);
            // </SnippetSerializeDefault>

            Console.WriteLine($"JSON output:\n{jsonString}\n");

            Console.WriteLine("Object generic type parameter - derived class properties included");
            // <SnippetSerializeObject>
            serializeOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize <object>(weatherForecast, serializeOptions);
            // </SnippetSerializeObject>
            Console.WriteLine($"JSON output:\n{jsonString}\n");


            Console.WriteLine("GetType() type parameter - derived class properties included");
            // <SnippetSerializeGetType>
            serializeOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, weatherForecast.GetType(), serializeOptions);
            // </SnippetSerializeGetType>
            Console.WriteLine($"JSON output:\n{jsonString}\n");
        }
Exemple #10
0
        public static void Run()
        {
            string          jsonString;
            var             weatherForecastDerived = WeatherForecastFactories.CreateWeatherForecastDerived();
            WeatherForecast weatherForecast        = weatherForecastDerived;

            weatherForecast.DisplayPropertyValues();

            Console.WriteLine("Base class generic type - derived class properties omitted");
            // <SerializeDefault>
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonString = JsonSerializer.Serialize <WeatherForecast>(weatherForecast, options);
            // </SerializeDefault>

            Console.WriteLine($"JSON output:\n{jsonString}\n");

            Console.WriteLine("Object generic type parameter - derived class properties included");
            // <SerializeObject>
            options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize <object>(weatherForecast, options);
            // </SerializeObject>
            Console.WriteLine($"JSON output:\n{jsonString}\n");

            Console.WriteLine("GetType() type parameter - derived class properties included");
            // <SerializeGetType>
            options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, weatherForecast.GetType(), options);
            // </SerializeGetType>
            Console.WriteLine($"JSON output:\n{jsonString}\n");

            Console.WriteLine("Extra properties on interface implementations included only for object properties");
            // <SerializeInterface>
            var forecasts = new Forecasts
            {
                Monday = new Forecast
                {
                    Date = DateTime.Parse("2020-01-06"),
                    TemperatureCelsius = 10,
                    Summary            = "Cool",
                    WindSpeed          = 8
                },
                Tuesday = new Forecast
                {
                    Date = DateTime.Parse("2020-01-07"),
                    TemperatureCelsius = 11,
                    Summary            = "Rainy",
                    WindSpeed          = 10
                }
            };

            options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(forecasts, options);
            // </SerializeInterface>
            Console.WriteLine($"{jsonString}\n");

            WeatherForecastWithPrevious weatherForecastWithPrevious =
                WeatherForecastFactories.CreateWeatherForecastWithPrevious();
            WeatherForecastWithPreviousAsObject weatherForecastWithPreviousAsObject =
                WeatherForecastFactories.CreateWeatherForecastWithPreviousAsObject();

            Console.WriteLine("Second level derived class properties included only for object properties");
            // <SerializeSecondLevel>
            options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecastWithPreviousAsObject, options);
            // </SerializeSecondLevel>
            Console.WriteLine($"JSON output with WindSpeed:\n{jsonString}\n");
            jsonString = JsonSerializer.Serialize(
                weatherForecastWithPrevious,
                options);
            Console.WriteLine($"JSON output without WindSpeed:\n{jsonString}\n");
        }