Skip to content

Instantly share code, notes, and snippets.

Ahson Khan ahsonkhan

Block or report user

Report or block ahsonkhan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ahsonkhan
ahsonkhan / JsonEscapingCheck.md
Last active Oct 17, 2019
Optimize the check to see whether a JSON string contains characters that need to be escaped.
View JsonEscapingCheck.md

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 / Json-Poco-BlogPost.cs
Created Jul 25, 2019
Generating a .NET POCO for a json file found on the web (some blog post)
View Json-Poco-BlogPost.cs
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; }
}
View Scenario1_expected.json
// 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 / Class_Utf8JsonWriter.md
Created Apr 17, 2019
Utf8JsonWriter Re-design based on usability and reliability feedback
View Class_Utf8JsonWriter.md

Motivation - Usability and Reliability:

  • The current writer is a ref struct, which requires passing by ref.
  • The use of array pool could result in reliability concerns if misused.
  • Async writes and state passing could be problematic.
  • No IBufferWriter implementation built in.

Goals:

  • Continue to support IBufferWriter (i.e. PipeWriter) directly
  • Keep ability for user to control buffering with ability to avoid data copies.
@ahsonkhan
ahsonkhan / CallStack-StackOverflow.txt
Created Feb 27, 2019
ApiCompat write diff stack overflow for uap
View CallStack-StackOverflow.txt
[External Code]
> Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 67 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteMetadataConstant(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 258 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 123 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteMetadataConstant(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 258 C#
Microsoft.Cci.Extensions.dll!Microsoft.Cci.Writers.CSharp.CSDeclarationWriter.WriteEnumValue(Microsoft.Cci.IMetadataConstant constant, Microsoft.Cci.ITypeReference constantType) Line 123 C#
Microsoft.Cci.Extensions.dll!M
@ahsonkhan
ahsonkhan / ref-api-diff.md
Last active Feb 22, 2019
Difference in reference assemblies between .NET Core 3.0 master and .NET Core 3.0 UpdateRefs branch
View ref-api-diff.md
@ahsonkhan
ahsonkhan / ArrayBufferWriter.ArrayPool.cs
Created Jan 29, 2019
Sample implementation of ArrayBufferWriter, a class that implements IBufferWriter<byte> backed by ArrayPool<byte>.Shared.
View ArrayBufferWriter.ArrayPool.cs
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
private long _committed;
private const int MinimumBufferSize = 256;
public ArrayBufferWriter(int initialCapacity = MinimumBufferSize)
{
@ahsonkhan
ahsonkhan / ArrayBufferWriter.cs
Created Jan 28, 2019
Sample implementation of ArrayBufferWriter, a class that implements IBufferWriter<byte>
View ArrayBufferWriter.cs
namespace System.Buffers
{
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
private long _committed;
private const int MinimumBufferSize = 256;
@ahsonkhan
ahsonkhan / JsonReaderPerf-report-github.md
Last active May 12, 2018
JsonLab and Json.NET JsonReader Performance Comparison Results for various JSON strings (UTF-8 and UTF-16)
View JsonReaderPerf-report-github.md
BenchmarkDotNet=v0.10.14.534-nightly, OS=Windows 10.0.16299.371 (1709/FallCreatorsUpdate/Redstone3)
Intel Xeon CPU E5-1620 v2 3.70GHz, 1 CPU, 8 logical and 4 physical cores
Frequency=3604598 Hz, Resolution=277.4234 ns, Timer=TSC
.NET Core SDK=2.1.300-rtm-008823
  [Host]     : .NET Core 2.1.0-rtm-26508-02 (CoreCLR 4.6.26508.04, CoreFX 4.6.26508.03), 64bit RyuJIT
  Job-JIQQBR : .NET Core 2.1.0-rtm-26508-02 (CoreCLR 4.6.26508.04, CoreFX 4.6.26508.03), 64bit RyuJIT

InvocationCount=1024  TargetCount=5  WarmupCount=3  
@ahsonkhan
ahsonkhan / IntParserUnrolled.md
Last active Jun 24, 2017
Loop unrolled int 32 parser
View IntParserUnrolled.md
public static bool TryParseInt32(ReadOnlySpan<byte> text, out int value, out int bytesConsumed)
{
    int textLength = text.Length;
    if (textLength < 1) goto FalseExit;

    int sign = 1;
    int index = default;
    int num = text[index];
You can’t perform that action at this time.