Unified.Json
1.0.6
dotnet add package Unified.Json --version 1.0.6
NuGet\Install-Package Unified.Json -Version 1.0.6
<PackageReference Include="Unified.Json" Version="1.0.6" />
paket add Unified.Json --version 1.0.6
#r "nuget: Unified.Json, 1.0.6"
// Install Unified.Json as a Cake Addin #addin nuget:?package=Unified.Json&version=1.0.6 // Install Unified.Json as a Cake Tool #tool nuget:?package=Unified.Json&version=1.0.6
Unified Id - the identity of your data.
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 | 0.00000001% in 10B IDs | 50% in 2.7e18 IDs |
Cast | implicit(string, ulong, long) | explicit(byte[], Parse/ToString) |
Generate | Build-in(byte[], string, GUID) | No, only random NewGuid |
Null/Empty-handling | Friendly as Empty | Exception |
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? string id = UnifiedId.NewId();
or long? long id = UnifiedId.NewId();
UnifiedId could be used as DDD ValueObject in your entities.
using Unified;
class User
{
public UnifiedId UserId { set; get; }
}
class Program
{
static void Main()
{
var user = new User
{
UserId = UnifiedId.NewId();
};
var settings = new JsonSerializerSettings // Could be added to global settings.
{
Converters = new List<JsonConverter>
{
new UnifiedIdConverter()
}
};
var json = JsonConvert.SerializeObject(user, settings); // { "UserId": "AFHUTVDSGUGVQ" }
}
}
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.
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.
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 one-way hash 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%
We recommend using Unified Id for data sets size up to 10 billion Ids. More will increase the chance of collision.
© 2021 Serhii Seletskyi. All rights reserved.
Product | Versions 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. |
.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. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
- Unified (>= 1.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Performance improvements