https://github.com/treblereel/mapper-json
APT based j2cl/gwt compatible jakarta JSONB like marshallers
https://github.com/treblereel/mapper-json
closure-compiler gwt j2cl java json jsonb
Last synced: about 1 month ago
JSON representation
APT based j2cl/gwt compatible jakarta JSONB like marshallers
- Host: GitHub
- URL: https://github.com/treblereel/mapper-json
- Owner: treblereel
- License: apache-2.0
- Created: 2022-04-24T04:44:01.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-29T06:01:59.000Z (7 months ago)
- Last Synced: 2025-03-31T02:21:58.781Z (3 months ago)
- Topics: closure-compiler, gwt, j2cl, java, json, jsonb
- Language: Java
- Homepage:
- Size: 476 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/treblereel/mapper-json/blob/main/LICENSE)


[](https://github.com/treblereel/mapper-json/actions/workflows/maven.yml)# 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
```xml
org.treblereel.gwt.json.mapper
common
${project.version}
org.treblereel.gwt.json.mapper
processor
${project.version}
provided
```
2. In case you use GWT2, add the `inherits` directive to your `gwt.xml` file:```xml
```
3. Annotate POJOs with the @JSONMapper annotation:
```xml
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`:
```java
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:
```java
Person_JsonMapperImpl mapper = new Person_JsonMapperImpl();Person person = mapper.fromJSON("{\"firstName\":\"John\",\"lastName\":\"Doe\"}")
```## Supported annotations and data types:
Supported `JSON-B` annotations:
* [@JsonbProperty](###@JsonbProperty)
* [@JsonbTypeSerializer](###@JsonbTypeSerializer)
* [@JsonbTypeDeserializer](###@JsonbTypeDeserializer)
* [@JsonbTransient](###@JsonbTransient)
* [@JsonbPropertyOrder](###@JsonbPropertyOrder)### @JsonbProperty
Allows customization of field (or JavaBean property) name.This name is used either in serialization or in deserialization.
* field```java
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
* type```java
@JsonbTypeSerializer(ObjectJsonbTypeSerializer.class)
@JsonbTypeDeserializer(ObjectJsonbTypeDeserializer.class)
private Object holder;
```JsonbSerializer:
```java
public class ObjectJsonbTypeSerializer implements JsonbSerializer {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:
```java
public class ObjectJsonbTypeDeserializer extends JsonbDeserializer {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
```java
@JSONMapper
public class Person {@JsonbTransient
private String firstName;private String lastName;
}
//{"lastName":"Doe"}
```### @JsonbPropertyOrder
JsonbPropertyOrder allows us to specify the order of properties in the JSON representation of the class, which can be useful when working with systems that have strict ordering requirements.* type
```java
@JSONMapper
@JsonbPropertyOrder({"property3", "property2", "property1"})
public class MyClass {
private String property1;
private String property2;
private String property3;
// getters and setters
}
//{"property3":"value3","property2":"value2","property1":"value1"}
```