Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Dec 10, 2023
2 parents 8f85e27 + 2bd21cc commit 014cacf
Show file tree
Hide file tree
Showing 23 changed files with 698 additions and 11 deletions.
7 changes: 6 additions & 1 deletion domino-jackson-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.dominokit</groupId>
<artifactId>domino-jackson-parent</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</parent>

<artifactId>domino-jackson-processor</artifactId>
Expand Down Expand Up @@ -96,6 +96,11 @@
<version>0.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import org.dominokit.jackson.annotation.CustomDeserializer;
import org.dominokit.jackson.annotation.CustomSerializer;
import org.dominokit.jackson.annotation.JSONMapper;
import org.dominokit.jackson.annotation.JSONReader;
import org.dominokit.jackson.annotation.JSONWriter;
Expand All @@ -50,6 +52,8 @@ public abstract class AbstractMapperProcessor extends AbstractProcessor {
protected Set<? extends Element> mappers;
protected Set<? extends Element> readers;
protected Set<? extends Element> writers;
protected Set<? extends Element> customSerializers;
protected Set<? extends Element> customDeserializers;

/** {@inheritDoc} */
@Override
Expand All @@ -70,6 +74,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return false;
}

customSerializers = roundEnv.getElementsAnnotatedWith(CustomSerializer.class);
customDeserializers = roundEnv.getElementsAnnotatedWith(CustomDeserializer.class);

mappers = roundEnv.getElementsAnnotatedWith(JSONMapper.class);
readers = roundEnv.getElementsAnnotatedWith(JSONReader.class);
writers = roundEnv.getElementsAnnotatedWith(JSONWriter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@
package org.dominokit.jackson.processor;

import com.google.auto.service.AutoService;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.WildcardTypeName;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import org.dominokit.jackson.CustomMappersLoader;
import org.dominokit.jackson.JsonDeserializer;
import org.dominokit.jackson.JsonSerializer;
import org.dominokit.jackson.annotation.CustomDeserializer;
import org.dominokit.jackson.annotation.CustomSerializer;
import org.dominokit.jackson.annotation.JSONMapper;
import org.dominokit.jackson.annotation.JSONReader;
import org.dominokit.jackson.annotation.JSONWriter;
Expand All @@ -41,12 +51,61 @@ public class ObjectMapperProcessor extends AbstractMapperProcessor {
/** {@inheritDoc} */
@Override
protected boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

customSerializers.forEach(this::registerSerializer);
customDeserializers.forEach(this::registerDeserializer);

ServiceLoader.load(CustomMappersLoader.class, ObjectMapperProcessor.class.getClassLoader())
.stream()
.map(ServiceLoader.Provider::get)
.forEach(loader -> loader.register(TypeRegistry.INSTANCE));

mappers.forEach(this::generateMappers);
readers.forEach(this::generateMapperForReader);
writers.forEach(this::generateMapperForWriter);
return false;
}

private void registerSerializer(Element element) {
if (!Type.isAssignableFrom(element, JsonSerializer.class)) {
messager.printMessage(
Diagnostic.Kind.ERROR,
"Custom serializer ["
+ element.getSimpleName()
+ "] must extend from "
+ JsonSerializer.class.getCanonicalName(),
element);
return;
}
Optional<TypeMirror> targetType =
Type.getClassValueFromAnnotation(element, CustomSerializer.class, "value");
targetType.ifPresent(
typeMirror -> {
TypeRegistry.registerSerializer(
Type.stringifyTypeWithPackage(typeMirror), TypeName.get(element.asType()));
});
}

private void registerDeserializer(Element element) {
if (!Type.isAssignableFrom(element, JsonDeserializer.class)) {
messager.printMessage(
Diagnostic.Kind.ERROR,
"Custom deserializer ["
+ element.getSimpleName()
+ "] must extend from "
+ JsonDeserializer.class.getCanonicalName(),
element);
return;
}
Optional<TypeMirror> targetType =
Type.getClassValueFromAnnotation(element, CustomDeserializer.class, "value");
targetType.ifPresent(
typeMirror -> {
TypeRegistry.registerDeserializer(
Type.stringifyTypeWithPackage(typeMirror), TypeName.get(element.asType()));
});
}

private void generateMappers(Element element) {
try {
new BeanMapperGenerator().generate(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.sql.Timestamp;
import java.util.*;
import javax.lang.model.type.TypeMirror;
import org.dominokit.jackson.JsonDeserializer;
import org.dominokit.jackson.JsonSerializer;
import org.dominokit.jackson.RegistryWrapper;
import org.dominokit.jackson.deser.*;
import org.dominokit.jackson.deser.BaseDateJsonDeserializer.DateJsonDeserializer;
import org.dominokit.jackson.deser.BaseDateJsonDeserializer.SqlDateJsonDeserializer;
Expand Down Expand Up @@ -54,7 +57,9 @@
* bean type it will also dynamically register new generated serializers/deserializer for any custom
* type.
*/
public final class TypeRegistry {
public final class TypeRegistry implements RegistryWrapper {

public static final TypeRegistry INSTANCE = new TypeRegistry();

private static Map<String, ClassMapper> simpleTypes = new HashMap<>();
private static Map<String, ClassMapper> keysMappers = new HashMap<>();
Expand All @@ -66,7 +71,7 @@ public final class TypeRegistry {

static final ClassMapperFactory MAPPER = new ClassMapperFactory();

// bas types mappers
// base types mappers
static {
MAPPER
.forType(boolean.class)
Expand Down Expand Up @@ -722,6 +727,16 @@ public static void registerSerializer(String type, TypeName serializer) {
}
}

@Override
public void registerSerializer(String type, Class<? extends JsonSerializer<?>> serializer) {
registerSerializer(type, TypeName.get(serializer));
}

@Override
public void registerDeserializer(String type, Class<? extends JsonDeserializer<?>> deserializer) {
registerDeserializer(type, TypeName.get(deserializer));
}

/**
* registerDeserializer.
*
Expand Down Expand Up @@ -918,7 +933,7 @@ public static boolean containsDeserializer(TypeMirror typeMirror) {
return containsDeserializer(Type.stringifyTypeWithPackage(typeMirror));
}

static class ClassMapperFactory {
public static class ClassMapperFactory {
ClassMapper forType(Class<?> clazz) {
return new ClassMapper(clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private String getCustomDeserializer(TypeMirror typeMirror) {
rootGenerated = true;
}
}
return "new $T()";
return "$T.getInstance()";
}

private String getPackageName(TypeMirror typeMirror) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private String getCustomSerializer(TypeMirror typeMirror) {
this.rootGenerated = true;
}
}
return "new $T()";
return "$T.getInstance()";
}

private String getPackageName(TypeMirror typeMirror) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,8 @@ public void testNestedBeanTypeField() throws Exception {
ClassName.bestGuess("org.dominokit.jackson.processor.TestBeanBeanJsonDeserializerImpl");
TypeRegistry.registerSerializer("org.dominokit.jackson.processor.TestBean", deserializer);
addFieldTest(
"testBean", result -> assertEquals(buildTestString("new $T()", deserializer), result));
"testBean",
result -> assertEquals(buildTestString("$T.getInstance()", deserializer), result));

runTests();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,8 @@ public void testNestedBeanTypeField() throws Exception {
ClassName.bestGuess("org.dominokit.jackson.processor.TestBeanBeanJsonSerializerImpl");
TypeRegistry.registerSerializer("org.dominokit.jackson.processor.TestBean", serializer);
addFieldTest(
"testBean", result -> assertEquals(buildTestString("new $T()", serializer), result));
"testBean",
result -> assertEquals(buildTestString("$T.getInstance()", serializer), result));

runTests();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.jackson.processor.custom;

import static java.util.Objects.nonNull;

import org.dominokit.jackson.JsonDeserializationContext;
import org.dominokit.jackson.JsonDeserializer;
import org.dominokit.jackson.JsonDeserializerParameters;
import org.dominokit.jackson.annotation.CustomDeserializer;
import org.dominokit.jackson.stream.JsonReader;

@CustomDeserializer(Employee.class)
public class CustomBeanDeserializer extends JsonDeserializer<Employee> {

private static final CustomBeanDeserializer INSTANCE = new CustomBeanDeserializer();

public static CustomBeanDeserializer getInstance() {
return INSTANCE;
}

@Override
protected Employee doDeserialize(
JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params) {

String value = reader.nextString();
if (nonNull(value)) {
String[] split = value.split(",");
Employee bean = new Employee();
bean.setId(Long.parseLong(split[0]));
bean.setName(split[1]);
bean.setTitle(split[2]);
return bean;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.jackson.processor.custom;

import static java.util.Objects.nonNull;

import org.dominokit.jackson.JsonSerializationContext;
import org.dominokit.jackson.JsonSerializer;
import org.dominokit.jackson.JsonSerializerParameters;
import org.dominokit.jackson.annotation.CustomSerializer;
import org.dominokit.jackson.stream.JsonWriter;

@CustomSerializer(Employee.class)
public class CustomBeanSerializer extends JsonSerializer<Employee> {

private static final CustomBeanSerializer INSTANCE = new CustomBeanSerializer();

public static CustomBeanSerializer getInstance() {
return INSTANCE;
}

@Override
protected void doSerialize(
JsonWriter writer,
Employee value,
JsonSerializationContext ctx,
JsonSerializerParameters params) {
if (nonNull(value)) {
writer.value(value.getId() + "," + value.getName() + "," + value.getTitle());
} else {
writer.value("");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.jackson.processor.custom;

import org.junit.Assert;
import org.junit.Test;

public class CustomMappingTest {

@Test
public void testCustomMapping() {
Employee customBean = new Employee();
customBean.setId(10);
customBean.setName("custom");
customBean.setTitle("custom title");

Person person = new Person();
person.setId(5);
person.setName("Jackson");
person.setTitle("Bean");

TestBean testBean = new TestBean();
testBean.setId(1);
testBean.setName("test bean");
testBean.setBean(customBean);
testBean.setPerson(person);

String result =
"{\"id\":1,\"name\":\"test bean\",\"bean\":\"10,custom,custom title\",\"person\":\"5,Jackson,Bean\"}";
Assert.assertEquals(result, TestBean_MapperImpl.INSTANCE.write(testBean));
Assert.assertEquals(testBean, TestBean_MapperImpl.INSTANCE.read(result));
}
}
Loading

0 comments on commit 014cacf

Please sign in to comment.