Git Product home page Git Product logo

mapper-json's Introduction

GitHub license Sonatype Nexus (Releases) Gitter Java CI with Maven

mapper-json

mapper-json is an annotation-processor-based JSON-B (JSON Binding) like mapper that works both on the client side - GWT and J2CL - and on the JVM side with "Code-first" approach.

Get started

  1. Add relevant dependencies to your pom.xml
    <dependency>
        <groupId>org.treblereel.gwt.json.mapper</groupId>
        <artifactId>common</artifactId>
        <version>${project.version}</version>
    </dependency>

    <dependency>
        <groupId>org.treblereel.gwt.json.mapper</groupId>
        <artifactId>processor</artifactId>
        <version>${project.version}</version>
        <scope>provided</scope>
    </dependency>
  1. In case you use GWT2, add the inherits directive to your gwt.xml file:
<inherits name='org.treblereel.gwt.json.Mapper' />
  1. Annotate POJOs with the @JSONMapper annotation:
import org.treblereel.gwt.json.mapper.annotation.JSONMapper;
    
@JSONMapper
public class Person {
    
   private String firstName;
   private String lastName;
       
   public String getFirstName() {
      return firstName;
   }
       
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
       
   public String getLastName() {
      return lastName;
   }
       
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
}

Setup is complete.

Using JSON mapper

The annotation processor will generate the JSON mapper for the Person class.

Example of serializing Person to JSON:

Person_JsonMapperImpl mapper = new Person_JsonMapperImpl();

Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");

String json = mapper.toJSON(person);
// {"firstName":"John","lastName":"Doe"}

Example of deserializing to POJO:

Person_JsonMapperImpl mapper = new Person_JsonMapperImpl();

Person person = mapper.fromJSON("{\"firstName\":\"John\",\"lastName\":\"Doe\"}")

Supported annotations and data types:

Supported JSON-B annotations:

@JsonbProperty

Allows customization of field (or JavaBean property) name.This name is used either in serialization or in deserialization.

  • field
public class Person {
    @JsonbProperty("_firstName")
    private String firstName;

    @JsonbProperty("_lastName")
    private String lastName;
}

//{"_firstName":"John","_lastName":"Doe"}

@JsonbTypeSerializer

@JsonbTypeDeserializer

Annotation provides way how to set custom JsonbSerializer/JsonbDeserializer to field or JavaBean property.

  • field
  @JsonbTypeSerializer(ObjectJsonbTypeSerializer.class)
  @JsonbTypeDeserializer(ObjectJsonbTypeDeserializer.class)
  private Object holder;

JsonbSerializer:

public class ObjectJsonbTypeSerializer implements JsonbSerializer<Object> {

    Translation_JsonSerializerImpl translation_JsonSerializerImpl =
            new Translation_JsonSerializerImpl();

    @Override
    public void serialize(Object obj, JsonGenerator generator, SerializationContext ctx) {
        if (obj instanceof Boolean) {
            generator.write("holder", ((Boolean) obj));
        } else if (obj instanceof Translation) {
            translation_JsonSerializerImpl.serialize((Translation) obj, "holder", generator, ctx);
        }
    }
}

JsonbDeserializer:

public class ObjectJsonbTypeDeserializer extends JsonbDeserializer<Object> {

    Translation_JsonDeserializerImpl translation_JsonDeserializerImpl =
            new Translation_JsonDeserializerImpl();

    @Override
    public Object deserialize(JsonValue value, DeserializationContext ctx) {

        if (value.getValueType() != JsonValue.ValueType.NULL) {
            if (value.getValueType() == JsonValue.ValueType.TRUE
                    || value.getValueType() == JsonValue.ValueType.FALSE) {
                if (value.getValueType() == JsonValue.ValueType.TRUE) {
                    return true;
                } else {
                    return false;
                }
            } else if (value.getValueType() == JsonValue.ValueType.OBJECT) {
                return translation_JsonDeserializerImpl.deserialize(value, ctx);
            }
        }
        return null;
    }
}

@JsonbTransient

Prevents mapping of a Java Bean property, field or type to JSON representation.

  • field
@JSONMapper
public class Person {

    @JsonbTransient
    private String firstName;

    private String lastName;
}
//{"lastName":"Doe"}

mapper-json's People

Contributors

treblereel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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