Unified.Json 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Unified.Json --version 2.0.1
                    
NuGet\Install-Package Unified.Json -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Unified.Json" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Unified.Json" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Unified.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Unified.Json --version 2.0.1
                    
#r "nuget: Unified.Json, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Unified.Json@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Unified.Json&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Unified.Json&version=2.0.1
                    
Install as a Cake Tool

Unified Id - the identity of your data.

Pull Requests Welcome Build Quality Gate Status NuGet Downloads

Why

What is Unified Id? If GUID is too heavy for your application but you need a random global Id that can be used as a string or long basic type, you are in right place.

What are the main advantages?

Feature Unified GUID
Size 8 byte (13 as string) 16 byte (36 as string)
Partitioning Build-in No, external can be added
Collisions ~2.7% chance at 1B IDs (50% at ~5.06B) 50% at 2.7e18 IDs
Cast explicit ToString(); implicit(ulong, long) explicit(byte[], Parse/ToString)
Generate Build-in(byte[], string, GUID) No, only random NewGuid
Null/Empty-handling Empty is a value; Parse rejects null/blank Exception

UnifiedId is a 64-bit FNV-1a value. At 1 billion IDs the chance of at least one collision is ~2.7%; at 10 billion it is ~93% (50% at ~5.06 billion). FNV-1a is not a cryptographic hash — do not use it for passwords, secrets or unguessable tokens. Use a 128-bit identifier when you need a larger collision budget or adversarial resistance.

Getting started

Install the NuGet package and write the following code:

using Unified;

class Program
{
    static void Main()
    {
        var id = UnifiedId.NewId(); // AFHUTVDSGUGVQ
    }
}

You have created your first Unified Id!

Want to use it as a string? var id = UnifiedId.NewId().ToString(); or long? long id = UnifiedId.NewId();

UnifiedId could be used as DDD ValueObject in your entities.

using System.Text.Json;
using Unified;
using Unified.Json;

class User
{
    public UnifiedId UserId { set; get; }
}

class Program
{
    static void Main()
    {
        var user = new User
        {
            UserId = UnifiedId.NewId()
        };

        var options = new JsonSerializerOptions();
        options.Converters.Add(new UnifiedIdJsonConverter());

        var json = JsonSerializer.Serialize(user, options); // { "UserId": "AFHUTVDSGUGVQ" }
    }
}

JSON

The Unified.Json package contains the System.Text.Json converter (UnifiedIdJsonConverter, recommended) and the legacy Newtonsoft.Json converter (UnifiedIdConverter, obsolete). The old Unified.Text.Json package has been merged into Unified.Json.

using System.Text.Json;
using Unified.Json;

var options = new JsonSerializerOptions();
options.Converters.Add(new UnifiedIdJsonConverter());

var json = JsonSerializer.Serialize(user, options); // { "UserId": "AFHUTVDSGUGVQ" }

The converter also supports UnifiedId as a dictionary key. It writes the canonical 13-character HEX32 string and rejects JSON null and blank values.

For legacy Newtonsoft.Json code, UnifiedIdConverter still exists but is marked [Obsolete] and is not compatible with Native AOT. Migrate to UnifiedIdJsonConverter.

#pragma warning disable CS0618
var settings = new JsonSerializerSettings
{
    Converters = { new UnifiedIdConverter() }
};
#pragma warning restore CS0618

Modern .NET

On modern targets (net10.0) UnifiedId implements IParsable<UnifiedId>, ISpanParsable<UnifiedId>, IFormattable and ISpanFormattable, so it participates in generic parsing, ASP.NET Core model binding and allocation-free (Span-based) formatting — and can serve as the backing value for strongly-typed id libraries. The package multi-targets netstandard2.0;net10.0.

Native AOT

The Unified package is built with IsAotCompatible=true on modern targets and Directory.Build.props promotes trim/AOT analyzer warnings to errors, so UnifiedId stays reflection-free and works with PublishAot.

When using System.Text.Json in Native AOT, register UnifiedIdJsonConverter and supply source-generated metadata (JsonSerializerContext) for your containing types; reflection-based serialization is disabled by default in AOT. The converter itself is allocation-free on net10.0.

The Newtonsoft.Json converter is obsolete and not AOT-compatible; use the System.Text.Json converter instead.

Benchmarks

The benchmarks/Unified.Benchmarks project uses BenchmarkDotNet with MemoryDiagnoser:

dotnet run -c Release --project benchmarks/Unified.Benchmarks -- --filter "*" -j short

The core hot paths (Parse, TryParse, TryFormat, Equals, the factories, PartitionNumber, and the System.Text.Json converter) allocate 0 B. APIs that return a new string (ToString, PartitionKey, PartitionNumberAsString) allocate only the resulting string. test/Unified.Tests/AllocationTests.cs enforces the 0-allocation paths in the test suite.

How it works

using Unified;

class Program
{
    static void Main()
    {
        var guid = Guid.NewGuid();
        var id = UnifiedId.FromGuid(guid);
        var fnv = id.ToUInt64();
        Console.WriteLine($"{guid} => {fnv} => {id}");
        // 8dd02ad1-62cc-4015-9502-49658ba240ae => 15834445116674749764 => DNFPVU1LD2DA4
    }
}

Unified Id generates 64bit FNV-1a Id's based on GUID and converts it to HEX32 to use as string. FNV-1a is fast and non-cryptographic; do not use it for passwords, secrets or unguessable tokens.

Algorithm

HEX32 is reversible, so you can convert it back from string to UInt64. ulong number = UnifiedId.Parse("DNFPVU1LD2DA4");

Why FNV-1a 64bit? because it has the best space randomization in the case of GUID conversion, below is space representation.

FNV-1a

Default method of generation is GUID based using method var id = UnifiedId.NewId();.

This value could be used as string converted in HEX32 consisting of two parts.

[KEY][UNIFIED_ID] KEY - Partition/Shard Key and UNIFIED_ID as Row Unified Key together used as the global identity.

You can also generate this Id as a 64-bit FNV-1a fingerprint (not cryptographic) using the following sources:

  • UnifiedId FromGuid(Guid id)
  • UnifiedId FromBytes(byte[] bytes)
  • UnifiedId FromString(string text)
  • UnifiedId FromInt64(long number)
  • UnifiedId FromUInt64(ulong number)

Do you need sequential Id? var id = new UnifiedId(DateTime.UtcNow.Ticks)

Want to save partitioned data? It's easy...

using Unified;
using System;

class Program
{
    static void Main()
    {
        // Let's emulate the partitioned database.
        var db = new Dictionary<string, List<UnifiedId>>();

        // We will use 10M records, just to execute it fast.
        var all = 10000000; 
        for (var i = 0; i <= all; i++)
        {
            // Generate random Id.
            var id = UnifiedId.NewId();

            // Get it's partition key. Number of partitions could be customized, default 16K.
            var partition = id.PartitionKey();

            // Initialize partitions in your DB.
            if (!db.ContainsKey(partition))
            {
                db.Add(partition, new List<UnifiedId>());
            }

            // Add values to partitions.
            db[partition].Add(id);
        }
    }
}

Result:

DB Count             : 16384
Each item contain    : 610 elements +/-5%

64-bit FNV-1a has a birthday bound: ~2.7% chance of at least one collision at 1 billion IDs and ~93% at 10 billion. Treat 64-bit IDs as unsuitable when you need guaranteed uniqueness at that scale; handle collisions or use a 128-bit identifier.

© 2021 Serhii Seletskyi. All rights reserved.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 0 11/28/2026
2.0.1 0 11/28/2026
2.0.0 0 11/28/2026
1.0.7 207 3/10/2026
1.0.6 1,969 11/26/2021
1.0.5 1,747 11/26/2021
1.0.4 2,187 11/26/2021
1.0.3 643 9/24/2020
1.0.2 609 9/24/2020
1.0.1 699 9/21/2020

2.0.1: Sonar quality fixes; no wire-format changes.