Skip to content

Instantly share code, notes, and snippets.

View ahsonkhan's full-sized avatar
💭
Working to empower every developer on the planet to achieve more.

Ahson Khan ahsonkhan

💭
Working to empower every developer on the planet to achieve more.
  • Microsoft
View GitHub Profile
@ahsonkhan
ahsonkhan / Scenario1_expected.json
Last active June 19, 2019 17:52
The expected output
// 1b) Expected output:
{"Email":"james@example.com","Active":true,"CreatedDate":"2013-01-20T00:00:00.0000000Z","Roles":["User","Admin"]}
// 2c) Expected output:
{
"Email": "james@example.com",
"Active": true,
"CreatedDate": "2013-01-20T00:00:00Z",
"Roles": [
@ahsonkhan
ahsonkhan / Json-Poco-BlogPost.cs
Created July 25, 2019 23:13
Generating a .NET POCO for a json file found on the web (some blog post)
public class Rootobject
{
public Value value { get; set; }
public Mentioneduser[] mentionedUsers { get; set; }
public Collaborator[] collaborators { get; set; }
public bool hideMeter { get; set; }
public Collectionuserrelation[] collectionUserRelations { get; set; }
public object mode { get; set; }
public References references { get; set; }
}
@ahsonkhan
ahsonkhan / JsonEscapingCheck.md
Last active November 30, 2020 07:26
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

@ahsonkhan
ahsonkhan / CaseSensitive.cs
Created January 28, 2020 00:47
Benchmark for System.Text.Json Deserialize with various casing approaches
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Collections.Generic;
namespace System.Text.Json.Serialization.Tests
{