Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sample code for new Migrate from Newtonsoft article #1897

Merged
merged 7 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions snippets/core/system-text-json/csharp/IForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace SystemTextJsonSamples
{
public interface IForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
}

public class Forecast : IForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public int WindSpeed { get; set; }
}

public class Forecasts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

{
public IForecast Monday { get; set; }
public object Tuesday { get; set; }
}
}
57 changes: 51 additions & 6 deletions snippets/core/system-text-json/csharp/SerializePolymorphic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,80 @@ public static void Run()

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

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

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


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

Console.WriteLine("Extra properties on interface implementations included only for object properties");
// <SnippetSerializeInterface>
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);
// </SnippetSerializeInterface>
Console.WriteLine($"{jsonString}\n");

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

Console.WriteLine("Second level derived class properties included only for object properties");
// <SnippetSerializeSecondLevel>
options = new JsonSerializerOptions
{
WriteIndented = true
};
jsonString = JsonSerializer.Serialize(weatherForecastWithPreviousAsObject, options);
// </SnippetSerializeSecondLevel>
Console.WriteLine($"JSON output with WindSpeed:\n{jsonString}\n");
jsonString = JsonSerializer.Serialize(
weatherForecastWithPrevious,
options);
Console.WriteLine($"JSON output without WindSpeed:\n{jsonString}\n");
}
}
}
14 changes: 7 additions & 7 deletions snippets/core/system-text-json/csharp/Utf8ReaderFromFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Utf8ReaderFromFile
private static readonly byte[] s_nameUtf8 = Encoding.UTF8.GetBytes("name");
public static void Run()
{
// Read as UTF-16 and transcode to UTF-8 to convert to a Span<byte>
// Read as UTF-16 and transcode to UTF-8 to convert to a ReadOnlySpan<byte>
tdykstra marked this conversation as resolved.
Show resolved Hide resolved
string fileName = "Universities.json";
string jsonString = File.ReadAllText(fileName);
ReadOnlySpan<byte> jsonReadOnlySpan = Encoding.UTF8.GetBytes(jsonString);
Expand All @@ -22,23 +22,23 @@ public static void Run()
int count = 0;
int total = 0;

var json = new Utf8JsonReader(jsonReadOnlySpan, isFinalBlock: true, state: default);
var reader = new Utf8JsonReader(jsonReadOnlySpan);

while (json.Read())
while (reader.Read())
{
JsonTokenType tokenType = json.TokenType;
JsonTokenType tokenType = reader.TokenType;

switch (tokenType)
{
case JsonTokenType.StartObject:
total++;
break;
case JsonTokenType.PropertyName:
if (json.ValueTextEquals(s_nameUtf8))
if (reader.ValueTextEquals(s_nameUtf8))
{
// Assume valid JSON, known schema
json.Read();
if (json.GetString().EndsWith("University"))
reader.Read();
if (reader.GetString().EndsWith("University"))
{
count++;
}
Expand Down
44 changes: 44 additions & 0 deletions snippets/core/system-text-json/csharp/WeatherForecast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ public class WeatherForecast
}
// </SnippetWF>

// <SnippetWFWithPrevious>
public class WeatherForecastWithPrevious
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public WeatherForecast PreviousForecast { get; set; }
}
// </SnippetWFWithPrevious>

// <SnippetWFWithPreviousAsObject>
public class WeatherForecastWithPreviousAsObject
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public object PreviousForecast { get; set; }
}
// </SnippetWFWithPreviousAsObject>

// <SnippetWFWithLong>
public class WeatherForecastWithLong
{
Expand Down Expand Up @@ -306,6 +326,30 @@ public static WeatherForecast CreateWeatherForecast()
return weatherForecast;
}

public static WeatherForecastWithPrevious CreateWeatherForecastWithPrevious()
{
var weatherForecast = new WeatherForecastWithPrevious
{
Date = DateTime.Parse("2019-08-01"),
TemperatureCelsius = 25,
Summary = "Hot",
PreviousForecast = CreateWeatherForecastDerived()
};
return weatherForecast;
}

public static WeatherForecastWithPreviousAsObject CreateWeatherForecastWithPreviousAsObject()
{
var weatherForecast = new WeatherForecastWithPreviousAsObject
{
Date = DateTime.Parse("2019-08-01"),
TemperatureCelsius = 25,
Summary = "Hot",
PreviousForecast = CreateWeatherForecastDerived()
};
return weatherForecast;
}

public static WeatherForecastWithLong CreateWeatherForecastWithLong()
{
var weatherForecast = new WeatherForecastWithLong
Expand Down