Git Product home page Git Product logo

xserializer's Introduction

XSerializer - serialize anything

XSerializer is a library that provides advanced, high-performance XML and JSON serializers.

XML

XSerializer's XML serialization handles properties and types that the System.Xml.Serialization.XmlSerializer does not handle, such as interfaces and dictionaries. It is meant to be a drop-in replacement for the BCL XmlSerializer - it uses the same attributes: [XmlElement], [XmlAttribute], etc.

// Create an instance of a serializer.
XmlSerializer<Foo> serializer1 = new XmlSerializer<Foo>();

// If the type of object is not known at compile time, use a factory method.
IXSerializer serializer2 = XmlSerializer.Create(someType);

JSON

JSON serialization in XSerializer has better performance than JSON.NET (Newtonsoft). It also has better support for deserialization into a variable or field of type dynamic.

// Create an instance of a serializer.
JsonSerializer<Foo> serializer1 = new JsonSerializer<Foo>();

// If the type of object is not known at compile time, use a factory method.
IXSerializer serializer2 = JsonSerializer.Create(someType);

Encryption

XSerializer's XML and JSON serializers support the concept of field-level encryption. Properties that are decorated with an [Encrypt] attribute have their values encrypted/decrypted automatically. The actual mechanism to encrypt these fields is exposed through the IEncryptionMechanism interface - users of the encrypt feature are expected to implement this interface in their project.

public class Foo
{
    // This property is sensitive and should be encrypted.
    [Encrypt]
    public int Bar { get; set; }
    
    // This property is not sensitive and can be serialized in plain text.
    public DateTime Baz { get; set; }
}

Typical Usage

You can serialize/deserialize like this:

public class Foo
{
    public int Id { get; set; }
    public IBar Bar { get; set; }
}

[XmlInclude(typeof(Bar))] // Method #1 uses attributes to indicate the inheritors of the interface.
public interface IBar
{
    int Id { get; set; }
    string Value { get; set; }
}

public class Bar : IBar
{
    public int Id { get; set; }
    public string Value { get; set; }
}

public void Main()
{
    Foo foo = new Foo { Id = 1, Bar = new Bar { Id = 2, Value = "Baz" } };
    
    XmlSerializer<Foo> serializer = new XmlSerializer<Foo>();
    
    string xml = serializer.Serialize(foo);
    Foo roundTripFoo = serializer.Deserialize(xml);
}

Or this:

public class Foo
{
    public int Id { get; set; }
    public IBar Bar { get; set; }
}

public interface IBar
{
    int Id { get; set; }
    string Value { get; set; }
}

public class Bar : IBar
{
    public int Id { get; set; }
    public string Value { get; set; }
}

public void Main()
{
    Foo foo = new Foo { Id = 1, Bar = new Bar { Id = 2, Value = "Baz" } };
    
    // Method #2 passes in the types of the inheritors to the constructor.
    XmlSerializer<Foo> serializer = new XmlSerializer<Foo>(typeof(Bar));
    
    string xml = serializer.Serialize(foo);
    Foo roundTripFoo = serializer.Deserialize(xml);
}

xserializer's People

Contributors

bfriesen avatar jonosment avatar justinmoss avatar jzeferino avatar

Watchers

James Cloos avatar  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.