Created
September 27, 2019 03:34
-
-
Save jkotas/b0671e154791e287c38a627ca81d7197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MANUAL_SERIALIALIZATION | |
using System.Text; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Text.Json; | |
public class WeatherForecast | |
{ | |
public DateTime Date { get; set; } | |
public int TemperatureC { get; set; } | |
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |
public string Summary { get; set; } | |
} | |
class Program | |
{ | |
private static readonly string[] Summaries = new[] | |
{ | |
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
}; | |
public static IEnumerable<WeatherForecast> Get() | |
{ | |
var rng = new Random(); | |
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | |
{ | |
Date = DateTime.Now.AddDays(index), | |
TemperatureC = rng.Next(-20, 55), | |
Summary = Summaries[rng.Next(Summaries.Length)] | |
}) | |
.ToArray(); | |
} | |
static void Main(string[] args) | |
{ | |
var result = Get(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
#if MANUAL_SERIALIALIZATION | |
var ms = new MemoryStream(); | |
using (var writer = new Utf8JsonWriter(ms)) | |
{ | |
writer.WriteStartArray(); | |
foreach (var w in result) | |
{ | |
writer.WriteStartObject(); | |
writer.WriteString("Date", w.Date); | |
writer.WriteNumber("TemperatureC", w.TemperatureC); | |
writer.WriteNumber("TemperatureF", w.TemperatureF); | |
writer.WriteString("Summary", w.Summary); | |
writer.WriteEndObject(); | |
} | |
writer.WriteEndArray(); | |
} | |
#else | |
JsonSerializer.SerializeToUtf8Bytes(result); | |
#endif | |
sw.Stop(); | |
Console.WriteLine(sw.ElapsedMilliseconds); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment