Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jozkee
jozkee / NewStream.cs
Created December 6, 2022 20:35
Implementation of NewStream with Template Method Pattern
namespace System.IO;
public abstract class NewStream : Stream
{
private bool _disposed;
public sealed override int Read(byte[] buffer, int offset, int count)
{
ValidateBufferArguments(buffer, offset, count);
return Read(buffer.AsSpan(offset, count));
@jozkee
jozkee / Stream2022.cs
Last active November 16, 2022 15:13
Make Stream easier to implement
public abstract class Stream2022 : Stream
{
public sealed override int Read(byte[] buffer, int offset, int count)
=> Read(buffer.AsSpan(offset, count));
public abstract override int Read(Span<byte> buffer);
public sealed override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
@jozkee
jozkee / net7_rc2_report.md
Created October 18, 2022 05:23
.NET 7 RC2 Report

Legend

  • Statistical Test threshold: 10%, the noise filter: 2 ns
  • Result is conclusion: Slower|Faster|Same|Noise|Unknown. Noise means that the difference was larger than 10% but not 2 ns.
  • Ratio = Base/Diff (the higher the better).
  • Alloc Delta = Allocated bytes diff - Allocated bytes base (the lower the better)

Statistics

Total: 86862

@jozkee
jozkee / TempWorkingDirectory.cs
Created March 17, 2022 17:32
TempWorkingDirectory
public class TempWorkingDirectory
{
string _directory;
public TempWorkingDirectory(string directory)
{
_directory = directory;
}
public void CreateFile(string filepath)
{
@jozkee
jozkee / how-to-use-cose.md
Last active February 9, 2022 00:18
System.Security.Cryptography.Cose how to use
@jozkee
jozkee / preserve_multiple_serializations.cs
Last active January 5, 2021 02:49
preserving references across multiple (de)serialization calls
// Assuming that you have a list of `Employee`s and you have to serialize each one individually
// and you also want to take advantage of the references saved in the `ReferenceHandler`'s resolver.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ConsoleApp4
{
class Program
@jozkee
jozkee / preserve_with_custom_converter.cs
Created January 5, 2021 02:25
preserving references + custom converters
// Assuming that you wrote a custom converter for `Company` and you don't want to manually serialize the `Supervisor`, which is an `Employee`.
// you want to delegate that to the Serializer and you also want to preserve the references that you have already saved.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ConsoleApp4
{
@jozkee
jozkee / ConsoleApp.csproj
Created April 17, 2020 05:10
The '$id' and '$ref' metadata properties must be JSON strings. Current token type is 'PropertyName'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.0-preview.2.20160.6" />
</ItemGroup>
@jozkee
jozkee / ref_handling_on_deserialize.md
Last active November 4, 2019 21:06
Reference Handilg on JsonSerializer.Deserialize

Handle output created from Serializing an object using Preserve References Handling

This specification describes the mechanism to handle payloads that contain metadata properties ($id, $ref & $values) written on a serialization with Preserved References Handling turned on.

Circular references occur when a property of the object refers to the object itself, directly (a -> a) or indirectly (a -> b -> a).

Multiple ocurrences of the same reference does not imply circularity.

The goal of this specification is to define the boundaries of the Reference Handling feature when opting-in for Preserve References.

Other libraries