All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ddth.commons.serialization.README.md Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
# com.github.ddth.commons.serialization

Helper & Utility classes to serialize/deserialize Java objects

_**Available since v0.5.0.**_

## Maven

```xml

    com.github.ddth
    ddth-commons-serialization
    ${ddth_commons_version}
    pom

```

**Interfaces & Classes**

- `ISerDeser`: Serializer/Deserializer interface.
- `ISerializationSupport`: Marker interface -class implements this interface to provide its own serialization/deserialization methods.
- `JsonSerDeser`: Serialize/deserialize Java object to/from JSON.
- `KryoSerDeser`: Serialize/deserialize Java object to/from `byte[]` using [Kryo library](https://github.com/EsotericSoftware/kryo).
- `FstSerDeser`: Serialize/deserialize Java object to/from `byte[]` using [Fst library](https://github.com/RuedigerMoeller/fast-serialization).

## Examples

```java
package com.github.ddth.commons.qnd.serialization;

import java.io.IOException;

import com.github.ddth.commons.rocksdb.RocksDbException;
import com.github.ddth.commons.serialization.FstSerDeser;
import com.github.ddth.commons.serialization.ISerDeser;
import com.github.ddth.commons.serialization.JsonSerDeser;
import com.github.ddth.commons.serialization.KryoSerDeser;

public class QndSerialization {

    static class ClassA {
        public String fieldString = "a string";
        public int fieldInt = 19;

        public ClassA() {
        }

        public ClassA(String valString, int valInt) {
            fieldString = valString;
            fieldInt = valInt;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("ClassA{");
            sb.append("fieldString:").append(fieldString);
            sb.append(",fieldInt:").append(fieldInt);
            sb.append("}");
            return sb.toString();
        }
    }

    static class ClassB {
        public ClassA fieldA = new ClassA("another string", 81);
        public boolean fieldBool = true;

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("ClassB{");
            sb.append("fieldA:").append(fieldA.toString());
            sb.append(",fieldBool:").append(fieldBool);
            sb.append("}");
            return sb.toString();
        }
    }

    static void qndSerDeser(ISerDeser sd) {
        System.out.println("===== Serialize/Deserialize using " + sd.getClass());
        ClassA A = new ClassA();
        System.out.println("Original A  : " + A.toString());
        byte[] bytesA = sd.toBytes(A);
        System.out.println("Serialized A: " + bytesA.length + " bytes");
        ClassA deserA = sd.fromBytes(bytesA, ClassA.class);
        System.out.println("Desered A   : " + deserA.toString());

        ClassB B = new ClassB();
        System.out.println("Original B  : " + B.toString());
        byte[] bytesB = sd.toBytes(B);
        System.out.println("Serialized B: " + bytesB.length + " bytes");
        ClassB deserB = sd.fromBytes(bytesB, ClassB.class);
        System.out.println("Desered B   : " + deserB.toString());

        System.out.println();
    }

    public static void main(String[] args) throws RocksDbException, IOException {
        qndSerDeser(new JsonSerDeser());
        qndSerDeser(new FstSerDeser());
        qndSerDeser(new KryoSerDeser());
    }
}
```

## History

**v0.9.3 - 2019-04-29**
- Add `FstSerDeser`.

**v0.5.0 - 2016-09-28**
- First release




© 2015 - 2025 Weber Informatics LLC | Privacy Policy