Skip to content

Instantly share code, notes, and snippets.

@layomia

layomia/Size.cs Secret

Created July 1, 2021 22:55
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 layomia/7714dffe94d1d971332c57dc2d768462 to your computer and use it in GitHub Desktop.
Save layomia/7714dffe94d1d971332c57dc2d768462 to your computer and use it in GitHub Desktop.
Source generation: size
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Size
{
class Program
{
static void Main(string[] args)
{
Message obj = new() { Data = "Hello", "World" };
// Size-optimal implementation.
byte[] json = JsonSerializer.SerializeToUtf8Bytes(obj, JsonContext.Default.MyClass);
obj = JsonSerializer.Deserialize(json, JsonContext.Default.MyClass);
// Alternate implementation.
// byte[] json = JsonSerializer.SerializeToUtf8Bytes(obj);
// obj = JsonSerializer.Deserialize(json);
}
}
public struct Message
{
public string Data { get; set; }
}
[JsonSerializable(typeof(Message))]
internal class MyJsonContext
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment