Skip to content

Instantly share code, notes, and snippets.

@stuartd
Last active May 18, 2021 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartd/1d156063c086f4bf214373cc109b4725 to your computer and use it in GitHub Desktop.
Save stuartd/1d156063c086f4bf214373cc109b4725 to your computer and use it in GitHub Desktop.
[MemoryDiagnoser]
public class JsonSerializeBenchmark {
private const string json = "{\"Name\":\"insert name here\",\"Addresses\":[{\"Line1\":\"Address line 1\",\"Line2\":\"Address line 2\"},{\"Line1\":\"Address line 1\",\"Line2\":\"Address line 2\"}]}";
[Benchmark]
public string Deserialize() => DeserializeJson();
[Benchmark]
public string Parse() => ParseJson();
private string DeserializeJson() {
var data = JsonConvert.DeserializeObject < Data > (json);
return JsonConvert.SerializeObject(data, Formatting.Indented);
}
private string ParseJson() {
return JToken.Parse(json).ToString(Formatting.Indented);
}
}
public class Address {
public string Line1 { get; set; }
public string Line2 { get; set; }
}
public class Data {
public string Name { get; set; }
public List <Address> Addresses { get; set; }
}
public class Program {
public static void Main(string[] args) {
BenchmarkRunner.Run<JsonSerializeBenchmark>();
// See the output in /bin/Release/../BenchmarkDotNet.Artifacts/results
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment