Skip to content

Instantly share code, notes, and snippets.

View layomia's full-sized avatar

Layomi Akinrinade layomia

View GitHub Profile
@layomia
layomia / Program.cs
Last active July 3, 2023 16:46
ExtendedProgram.cs
using System.Text.Json.Serialization;
using AotWebApi;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
// Specifies options to honor when generating binding logic.
// Presence indicates that the source generator should be invoked for the current assembly.
public sealed class BindingSourceGenerationOptionsAttribute : Attribute
{
// Specifies the generation mode.
public BindingSourceGenerationMode GenerationMode { get; set; }
}

ConfigurationBinder source generator

Phase 1: Implement core binding functionality

Similar to fast-path JSON deserialization.

Current status Feature branch:

  • core infra & generation implemented
  • based on David's prototype which generates to global namespace, making compiler choose source-gen'd methods over framework methods
  • testing mechanism same as JSON (sharing tests with reflection-based implementation)
@layomia
layomia / README.md
Last active November 10, 2022 03:22
@layomia
layomia / JsonEscapingCheck.md
Created November 30, 2020 07:26 — forked from ahsonkhan/JsonEscapingCheck.md
Optimize the check to see whether a JSON string contains characters that need to be escaped.

The default behavior of the System.Text.Json JsonSerializer and Utf8JsonWriter follows the behavior of JavascriptEncoder.Default, which means the following character sets are escaped:

  • Any non-ascii character
  • Characters required escaping based on the JSON RFC
  • HTML-specific ascii characters

Current Approach

The current approach involves going through the list of characters, one at a time, and check whether that character needs escaping. We special case the "null" encoder (which is the default when the user doesn't specify their own) and use a 256 byte bit-mask to indicate which character needs to be escaped. https://github.com/dotnet/corefx/blob/52d63157c78c31816b81be1599a5dacf96b5e5ca/src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.Escaping.cs#L82-L109

New Approach Chosen

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace RefHandlingDemo
{
class Program
{
static void Main(string[] args)
@layomia
layomia / JsonIgnore_API_change.md
Last active March 23, 2020 21:04
JsonIgnore_API_change

Approach A

/// <summary>
/// Prevents a property from being serialized or deserialized.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class JsonIgnoreAttribute : JsonAttribute
{
    /// <summary>
sudo ./build.sh -subsetCategory coreclr -configuration Release
__DistroRid: linux-x64
__RuntimeId: linux-x64
Downloading 'https://dot.net/v1/dotnet-install.sh'
Trying to run 'curl https://dot.net/v1/dotnet-install.sh -sSL --retry 10 --create-dirs -o /home/layomia/repos/dotnet_runtime/.dotnet/dotnet-install.sh' for maximum of 5 attempts.
Ran 'curl https://dot.net/v1/dotnet-install.sh -sSL --retry 10 --create-dirs -o /home/layomia/repos/dotnet_runtime/.dotnet/dotnet-install.sh' successfully.
dotnet_install: Warning: Unable to locate zlib. Probable prerequisite missing; install zlib.
dotnet-install: Downloading link: https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.100-preview.3.20168.11/dotnet-sdk-5.0.100-preview.3.20168.11-linux-x64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.100-preview.3.20168.11/dotnet-sdk-5.0.100-preview.3.20168.11-linux-x64.tar.gz
dotnet-install: Adding to current process PATH: `/home/layomia/repos/dotnet_runtime/.dotnet`. Note: This change will

Deserializing objects using parameterless constructors with JsonSerializer

Motivation

JsonSerializer deserializes instances of objects (classes and structs) using public parameterless constructors. If none is present, and deserialization is attempted, the serializer throws a NotSupportedException with a message stating that objects without public parameterless constructors, including interfaces and abstract types, are not supported for deserialization. There is no way to deserialize an instance of an object using a parameterized constructor.

Deserialize LoginViewModel

Method Mean Error StdDev Median Min Max Gen 0 Gen 1 Gen 2 Allocated
Jil 492.3 ns 11.13 ns 11.43 ns 488.6 ns 479.8 ns 522.1 ns 0.0629 - - 264 B
JSON.NET 1,288.4 ns 26.17 ns 67.55 ns 1,263.9 ns 1,209.4 ns 1,493.6 ns 0.6618 - - 2776 B
Utf8Json 360.5 ns 3.07 ns 2.56 ns 360.4 ns 355.4 ns 365.5 ns 0.0668 - - 280 B
Default 517.7 ns 3.63 ns 3.03 ns 517.3 ns 512.5 ns 522.3 ns 0.0391 - - 168 B
AOT_LoadConverters 471.8 ns 9.14 ns 11.22 ns 469.3 ns 452.2 ns 494.5 ns 0.0401 - - 168 B
AOT_Raw 402.2 ns 10.17 ns 9.51 ns