Giter Site home page Giter Site logo

afxres / binary Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 2.45 MB

Serialization library for C# and F#.

License: MIT License

C# 74.18% F# 25.80% Shell 0.02%
binary-data serialization csharp fsharp dotnet serialization-library roslyn-generator source-generator

binary's Introduction

Binary

GitHub repo size GitHub Workflow Status Coverage Status

Package Version Downloads Descriptions
Mikodev.Binary version downloads Main package
Mikodev.Binary.FSharp version downloads Additional converters for F#

Getting Started

Sample code

using Mikodev.Binary;
using System;

var generator = Generator.CreateDefault();
var source = new Person("C#", 21);
var buffer = generator.Encode(source);
var result = generator.Decode<Person>(buffer);
Console.WriteLine(result);

record Person(string Name, int Age);

Supported types

Category Details Comment
Primitive (U)Int(16,32,64,128), Boolean, Byte, Char, Decimal, Double, Half, SByte, Single, String Default string encoding is UTF-8
Data & Time DateOnly, DateTime, DateTimeOffset, TimeOnly, TimeSpan
Numeric BigInteger, Complex, Matrix3x2, Matrix4x4, Plane, Quaternion, Vector2, Vector3, Vector4
Memory T[...], Memory<>, ReadOnlyMemory<>, ReadOnlySequence<>
Tuple KeyValuePair<,>, Tuple<...>, ValueTuple<...> Tuple can not be null
Miscellaneous BitArray, BitVector32, Guid, IPAddress, IPEndPoint, Nullable<>, PriorityQueue<,>, Rune, Uri, Version
Collection Implements IEnumerable<> and have a constructor accept IEnumerable<> as parameter Stack types are explicitly not supported

Language Features

Feature JIT AOT Comment
Enumeration Types Yes Yes Handle as integers
Anonymous Types Yes
Tuple Types Yes Yes
Records Yes Yes
Required Members Yes Yes
Inline Arrays Yes Yes
Discriminated Unions (F#) Yes

AOT Support

AOT support (via source generator) is now generally available.
For example, we have a data model like this:

record Person(int Id, string Name);

Then create a partial type with SourceGeneratorContextAttribute and include this data model:

namespace SomeNamespace;

using Mikodev.Binary.Attributes;

[SourceGeneratorContext]
[SourceGeneratorInclude<Person>]
partial class SomeSourceGeneratorContext { }

This will generate a property named ConverterCreators which contains all generated converter creators.
Just add those converter creators to IGenerator and it will work.

var generator = Generator.CreateAotBuilder()
    .AddConverterCreators(SomeSourceGeneratorContext.ConverterCreators.Values)
    .Build();
var converter = generator.GetConverter<Person>();
var person = new Person(Id: 1, Name: "Someone");
var buffer = converter.Encode(person);
var result = converter.Decode(buffer);
Console.WriteLine(result.Id);   // 1
Console.WriteLine(result.Name); // Someone

Binary Layout

Length Prefix

Leading Bit Byte Length Range Example Bytes Example Value
0 1 0 ~ 0x7F 7F 127
1 4 0 ~ 0x7FFF_FFFF 80 00 04 01 1025

Object

Value:

new { id = 1024, name = "C#" }

Equivalent to:

new SortedDictionary<string, object> { ["id"] = 1024, ["name"] = "C#" }

Bytes:

    i  d                1024
02  69 64           04  00 04 00 00
    n  a  m  e          C  #
04  6e 61 6d 65     02  43 23

Tuple

Value:

("Text", 3.14F)

Bytes:

    T  e  x  t
04  54 65 78 74
    3.14
    c3 f5 48 40

binary's People

Contributors

afxres avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.