GithubHelp home page GithubHelp logo

keritervdeveloping / anyserializer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from replaysmike/anyserializer

0.0 0.0 0.0 250 KB

A CSharp library that can binary serialize any object quickly and easily

License: GNU General Public License v3.0

C# 94.95% Jupyter Notebook 5.05%

anyserializer's Introduction

AnySerializer

nuget nuget Build status Codacy Badge Codacy Badge

A CSharp binary serialization library that can serialize any object quickly and easily. No attributes/decoration required!

That's right, no need for [Serializable] or any other custom attributes on your classes!

Description

AnySerializer was built for software applications that make manual serialization difficult, or time consuming to decorate and design correctly. Other libraries require custom attributes to define serialization contracts, or fail entirely at more complicated scenarios. That's where AnySerializer shines! It literally is an anything in, anything out binary serializer.

Installation

Install AnySerializer from the Package Manager Console:

PM> Install-Package AnySerializer

Usage

using AnySerializer;

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);
var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes);

Ignoring Properties/Fields

Ignoring fields/properties is as easy as using any of the following standard ignores: [IgnoreDataMember], [NonSerializable] and [JsonIgnore]. Note that [NonSerializable] only works on fields, for properties (and/or fields) use [IgnoreDataMember].

Providing custom type mappings

If you find you need to map interfaces to concrete types that are contained in different assemblies, you can add custom type mappings:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);

var typeMaps = TypeRegistry.Configure((config) => {
  config.AddMapping<ICustomInterfaceName, ConcreteClassName>();
  config.AddMapping<ICustomer, Customer>();
});

var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes, typeMaps);

or alternatively, a type factory for creating empty objects:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);

var typeMaps = TypeRegistry.Configure((config) => {
  config.AddFactory<ICustomInterfaceName, ConcreteClassName>(() => new ConcreteClassName());
});

var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes, typeMaps);

and an alternate form for adding one-or-more mappings:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);

var typeMap = TypeRegistry.For<ICustomInterfaceName>()
                .Create<ConcreteClassName>();

var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes, typeMap);

or single type one-or-more factories:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);

var typeMap = TypeRegistry.For<ICustomInterfaceName>()
                .CreateUsing<ConcreteClassName>(() => new ConcreteClassName());

var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes, typeMap);

Complicated scenarios - Embedded Type Descriptors to the rescue!

There are some scenarios that cause grief when serializing certain types. Things like abstract interfaces and anonymous types require information about how to serialize them. To solve this, you can choose to embed type information for these scenarios which will increase the size of the serialized data slightly - which is optimized and compressed so it's not that much data.

To embed type descriptors in the serialized data:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject, SerializerOptions.EmbedTypes);
var restoredObject = Serializer.Deserialize<SomeComplexTypeWithDeepStructure>(bytes);

What does it do? Essentially what is going on here is we store a reference to the assembly and type which tells AnySerializer how to restore the data when deserializing. Only types that are interfaces and anonymous types are stored and concrete classes are ignored. When this isn't applied AnySerializer can still try to figure out what to do, but it doesn't guarantee that it will succeed if types are contained in assemblies it isn't aware of, or where there are multiple concrete classes available for an interface.

Validating binary data

A validator is provided for verifying if a serialized object contains valid deserializable data that has not been corrupted:

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = Serializer.Serialize(originalObject);
var isValid = Serializer.Validate(bytes);
Assert.IsTrue(isValid);

Extensions

You can use the extensions to perform serialization/deserialization:

using AnySerializer.Extensions;

var originalObject = new SomeComplexTypeWithDeepStructure();
var bytes = originalObject.Serialize();
var restoredObject = bytes.Deserialize<SomeComplexTypeWithDeepStructure>();

Capabilities

  • All basic types, enums, generics, collections
  • Read-only types
  • Circular references
  • Ignore attributes on unwanted fields/properties
  • Constructorless classes
  • Anonymous types
  • Ignoring of delegates and events, other non-serializable types
  • Resolving abstract interfaces to concrete types
  • Manually specifying custom type mappings through the registry
  • Embedded type descriptors
  • Data validator
  • Custom collections
  • Optional compact mode
  • Compression support
  • Specialized collections (Queue, Stack, ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, ConcurrentBag)
  • High performance testing and optimization

Other applications

To see differences between two serialized objects you can use AnyDiff on your copied object:

using AnyDiff;

var object1 = new MyComplexObject(1, "A string");
var object1bytes = Serializer.Serialize(object1);
var object2 = Serializer.Deserialize<MyComplexObject>(object1bytes);

object2.Id = 100;

// view the changes between them
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 1);

If you need a way to copy an object that doesn't involve serialization, try AnyClone which is a pure reflection based cloning library!

anyserializer's People

Contributors

itnmike avatar keritervdeveloping avatar replaysmike avatar steventcramer 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.