Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

XSON: A High-Performance Java Serialization Framework

DZone's Guide to

XSON: A High-Performance Java Serialization Framework

If you aren't familiar with XSON, a new version was recently released. Let's check out how it can help maintain performant serialization and deserialization.

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

Image title

XSON is a Java object serialization and deserialization framework. It supports the serialization of Java objects into byte arrays and deserialization from byte arrays to Java objects.

New Version Features

  1. The new buffer package, the relevant class in this package, provides the allocation of the byte [] in the process of distribution, use, recycling management; further providing serialization speed and reducing the Full GC.
  2. Provides API support with offset content.
  3. Provides XCO object serialization and deserialization support.
  4. Provide support for extended configuration files.

Tutorials

Basic Use

a. Add the dependency

<dependency>
    <groupId>org.xson</groupId>
    <artifactId>xson</artifactId>
    <version>1.0.2</version>
</dependency>


b. Serialization

User user = new User();
// set...
byte[] data = XSON.encode(user);


c. Deserialization

// byte[] data
User user = XSON.decode(data);


d. Serialization and deserialization with offset content

int x = 6;

User user = new User();
// set...
byte[] data = XSON.encode(x, user);

// byte[] data
User user = XSON.decode(x, data);


Configuration File

Let's look at this xson.properties file configuration example, then dive into what the components mean below:

# Support for XCO
xco=true

# ByteArrayManager configuration
byteArray.number=100
byteArray.capacity=512

# User classname mapping configuration
java.util.ArrayList=0
java.util.EnumSet=1
java.util.HashSet=2
java.util.LinkedHashSet=3
java.util.LinkedList=4
java.util.Stack=5
java.util.TreeSet=6
java.util.Vector=7
java.util.EnumMap=8
java.util.HashMap=9
java.util.Hashtable=a
java.util.IdentityHashMap=b
java.util.LinkedHashMap=c
java.util.Properties=d
java.util.TreeMap=e
java.util.concurrent.ConcurrentHashMap=f


Configuration Instructions

Xco = true to open support for XCO objects, the default does not open;

ByteArray.numberByteArrayManager Managed byte [] number, default 100;

ByteArray.capacityByteArrayManager manages the capacity of each byte [], the default 512;

Java.util.ArrayList = 0 user class name custom mapping; For example: When XSON serialized User object, the serialization of the results of the class information description area will record the User class class name, such as org.xson. User, if we configure org.xson.User = user through this, theuser will only be logged in the class information description area. Here, the volume of the serialized data can be reduced.

Note: The configuration file uses the fixed name xson.properties. When used, please put it under the classpath root path.

Customize the Serializer

1. Here's a user-defined serializer and deserializer:

public class CustomerSerializer implements XsonWriter {

    @Override
    public void write(Object target, ByteModel model) {
        // Implementation code
    }
}

public class CustomerDeserializer implements XsonReader {
    @Override
    public Object read(ReaderModel model) {
        // Implementation code
        return null;
    }
}


2. Add the user-defined serializer and deserializer

XsonSupport.addCustomSerializer(User.class, new CustomerSerializer(), new CustomerDeserializer());

Note: The serializer and deserializer must be set in pairs.

Design

Class Diagram Design

Image title

  1. XSON: user entry class, providing serialization and deserialization methods
  2. WriterModel: serialized write model class
  3. XsonWriter: serialized interface
  4. ArraySerializer: object array serialization class
  5. CollectionSerializer: Collection object serialization class
  6. EnumSerializer: enumeration object serialization class
  7. MapSerializer: Map object serialization class
  8. Other Serializer: other types of object serialization class, see the source
  9. ReaderModel: deserialized read model class
  10. XsonReader: deserialization interface
  11. CurrencyDeserializer: Currency object inversion class
  12. DateUtilDeserializer: time object inversion class
  13. LocaleDeserializer: region object inversion class
  14. LongDeserializer: Long wrapper object inversion class
  15. Other Deserializer: other types of object inversion class, see the source code
  16. XsonConst: constant class, holding all the serialization and deserialization class instance

byte[] Management


Image title


  1. Light green: byte [], managed by the ByteArrayManager class, holding multiple byte []s in its contents, the same is true;ByteArrayManager in the serialization process provides byte [] application and recycling management.
  2. Dark green: byte [], temporarily created during serialization, is not managed by ByteArrayManager, processed by GC.
  3. Light blue: ByteArrayItem class; based on byte [] package class, the contents of the record byte []the capacity, limit and so on the use of attributes;
  4. The yellow: XSONByteArray class; holds one or more ByteArrayItem classes internally, through which the primitive type, string, byte array is serialized to the byte [] of the ByteArrayItem class in;

ASM Technology

For Java Bean object handling, the XSON framework uses ASM technology to dynamically generate the corresponding Serializer and Deserializer for each bean object for serialization and deserialization.

Data Structure

Refer to: http://www.xson.org/project/xson/1.0.2/structure.html

Performance Evaluation

Refer to: http://www.xson.org/project/xson/1.0.2/testing.html

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
xco ,serialization and deserialization ,java ,xson ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}