jackson-datatype-thrift

Jackson datatype module to support JSON serialization/deserialization of Thrift objects

License

License

MIT
Categories

Categories

Data Jackson JSON
GroupId

GroupId

io.github.artemy-osipov.thrift
ArtifactId

ArtifactId

jackson-datatype-thrift
Last Version

Last Version

0.4.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

jackson-datatype-thrift
Jackson datatype module to support JSON serialization/deserialization of Thrift objects
Project URL

Project URL

https://github.com/artemy-osipov/jackson-datatype-thrift
Source Code Management

Source Code Management

https://github.com/artemy-osipov/jackson-datatype-thrift

Download jackson-datatype-thrift

Dependencies

compile (1)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.9.9

runtime (2)

Group / Artifact Type Version
org.apache.thrift : libthrift jar 0.13.0
org.projectlombok : lombok jar 1.18.12

Project Modules

There are no modules declared in this project.

Jackson datatype module to support JSON serialization/deserialization of Thrift objects

Maven Central Codacy Badge

Maven dependency

To use module on Maven-based projects, use following dependency:

<dependency>
  <groupId>io.github.artemy.osipov.thrift</groupId>
  <artifactId>jackson-datatype-thrift</artifactId>
  <version>0.1.0</version> // or any newer version
</dependency>   

Usage

To use thrift datatype you will need to register the module:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ThriftModule());

For thrift with scheme:

struct SomeStruct {
    1: string stringField
    2: bool boolField
    3: i32 intField
    4: SomeEnum enumField
    5: SomeInnerStruct complexField
}

struct SomeInnerStruct {
    1: string f1
    2: string f2
}

enum SomeEnum {
    ENUM_1
    ENUM_2
}

serialization will be:

SomeStruct thrift = new SomeStruct()
                .setStringField("s1")
                .setBoolField(true)
                .setIntField(1)
                .setEnumField(SomeEnum.ENUM_1)
                .setComplexField(new SomeInnerStruct()
                        .setF1("f1")
                        .setF2("f2")
                );

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(thrift);
assert json.equals("""
                {
                  "stringField": "s1",
                  "boolField": true,
                  "intField": 1,
                  "enumField": "ENUM_1",
                  "complexField": {
                    "f1": "f1",
                    "f2": "f2"
                  }
                }""");

deserialization will be:

String json = """
              {
                "stringField": "s1",
                "boolField": true,
                "intField": 1,
                "enumField": "ENUM_1",
                "complexField": {
                  "f1": "f1",
                  "f2": "f2"
                }
               }""";

String thrift = mapper.readValue(json, SomeStruct.class);
assert thrift.equals(new SomeStruct()
                     .setStringField("s1")
                     .setBoolField(true)
                     .setIntField(1)
                     .setEnumField(SomeEnum.ENUM_1)
                     .setComplexField(
                       new SomeInnerStruct()
                         .setF1("f1")
                         .setF2("f2")
                     ));

Versions

Version
0.4.2