Skip to content

Commit d342a6e

Browse files
committed
initial commit
1 parent 29150c3 commit d342a6e

File tree

19 files changed

+330
-10
lines changed

19 files changed

+330
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ hs_err_pid*
3131
bin/
3232

3333
src/main/java/info/unterrainer/commons/serialization/Information.java
34+
/.apt_generated/
35+
/.apt_generated_tests/

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@
2020
<nature>org.eclipse.jdt.core.javanature</nature>
2121
<nature>org.eclipse.m2e.core.maven2Nature</nature>
2222
</natures>
23+
<filteredResources>
24+
<filter>
25+
<id>1658217977308</id>
26+
<name></name>
27+
<type>30</type>
28+
<matcher>
29+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
30+
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
31+
</matcher>
32+
</filter>
33+
</filteredResources>
2334
</projectDescription>

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<artifactId>jackson-datatype-jsr310</artifactId>
4343
<version>2.13.3</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.windpanda.jmapper-framework</groupId>
47+
<artifactId>jmapper-core</artifactId>
48+
<version>1.6.3</version>
49+
</dependency>
4550
</dependencies>
46-
4751
</project>

src/main/java/info/unterrainer/commons/serialization/JsonMapper.java renamed to src/main/java/info/unterrainer/commons/serialization/jsonmapper/JsonMapper.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization;
1+
package info.unterrainer.commons.serialization.jsonmapper;
22

33
import java.text.DateFormat;
44
import java.text.SimpleDateFormat;
@@ -68,35 +68,41 @@ public <T> String toStringFrom(final T sourceObject) {
6868
try {
6969
return objectMapper.writeValueAsString(sourceObject);
7070
} catch (JsonProcessingException e) {
71-
throw new info.unterrainer.commons.serialization.exceptions.JsonProcessingException(e.getMessage(), e);
71+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonProcessingException(
72+
e.getMessage(), e);
7273
}
7374
}
7475

7576
public <T> T fromStringTo(final Class<T> targetClass, final String sourceJson) {
7677
try {
7778
return objectMapper.readValue(sourceJson, targetClass);
7879
} catch (JsonMappingException e) {
79-
throw new info.unterrainer.commons.serialization.exceptions.JsonMappingException(e.getMessage(), e);
80+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonMappingException(e.getMessage(),
81+
e);
8082
} catch (JsonProcessingException e) {
81-
throw new info.unterrainer.commons.serialization.exceptions.JsonProcessingException(e.getMessage(), e);
83+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonProcessingException(
84+
e.getMessage(), e);
8285
}
8386
}
8487

8588
public <T> T fromStringTo(final JavaType targetClass, final String sourceJson) {
8689
try {
8790
return objectMapper.readValue(sourceJson, targetClass);
8891
} catch (JsonMappingException e) {
89-
throw new info.unterrainer.commons.serialization.exceptions.JsonMappingException(e.getMessage(), e);
92+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonMappingException(e.getMessage(),
93+
e);
9094
} catch (JsonProcessingException e) {
91-
throw new info.unterrainer.commons.serialization.exceptions.JsonProcessingException(e.getMessage(), e);
95+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonProcessingException(
96+
e.getMessage(), e);
9297
}
9398
}
9499

95100
public JsonNode toTreeFrom(final String sourceJson) {
96101
try {
97102
return objectMapper.readTree(sourceJson);
98103
} catch (JsonProcessingException e) {
99-
throw new info.unterrainer.commons.serialization.exceptions.JsonProcessingException(e.getMessage(), e);
104+
throw new info.unterrainer.commons.serialization.jsonmapper.exceptions.JsonProcessingException(
105+
e.getMessage(), e);
100106
}
101107
}
102108

src/main/java/info/unterrainer/commons/serialization/exceptions/JsonMappingException.java renamed to src/main/java/info/unterrainer/commons/serialization/jsonmapper/exceptions/JsonMappingException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization.exceptions;
1+
package info.unterrainer.commons.serialization.jsonmapper.exceptions;
22

33
public class JsonMappingException extends RuntimeException {
44

src/main/java/info/unterrainer/commons/serialization/exceptions/JsonProcessingException.java renamed to src/main/java/info/unterrainer/commons/serialization/jsonmapper/exceptions/JsonProcessingException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package info.unterrainer.commons.serialization.exceptions;
1+
package info.unterrainer.commons.serialization.jsonmapper.exceptions;
22

33
public class JsonProcessingException extends RuntimeException {
44

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package info.unterrainer.commons.serialization.objectmapper;
2+
3+
import java.lang.reflect.Array;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.function.BiConsumer;
9+
10+
import com.googlecode.jmapper.JMapper;
11+
import com.googlecode.jmapper.api.JMapperAPI;
12+
import com.googlecode.jmapper.exceptions.JMapperException;
13+
14+
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException;
15+
import info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperProcessingException;
16+
import info.unterrainer.commons.serialization.objectmapper.key.MappingKey;
17+
18+
import static com.googlecode.jmapper.api.JMapperAPI.mappedClass;
19+
import static com.googlecode.jmapper.api.JMapperAPI.global;
20+
21+
public class ObjectMapper {
22+
23+
private Map<MappingKey<?, ?>, BiConsumer<?, ?>> mappings;
24+
private Map<MappingKey<?, ?>, JMapper<?, ?>> mappers;
25+
26+
public ObjectMapper() {
27+
mappings = new HashMap<>();
28+
mappers = new HashMap<>();
29+
}
30+
31+
public <S, T> T[] map(Class<S> sourceType, Class<T> targetType, S[] source) {
32+
return map(sourceType, targetType, source, false);
33+
}
34+
35+
public <S, T> T[] map(Class<S> sourceType, Class<T> targetType, S[] source, boolean usejMapper) {
36+
37+
@SuppressWarnings("unchecked")
38+
T[] targetList = (T[]) Array.newInstance(targetType, source.length);
39+
for (int i = 0; i < source.length; i++) {
40+
targetList[i] = map(sourceType, targetType, source[i], usejMapper);
41+
}
42+
43+
return targetList;
44+
}
45+
46+
public <S, T> T map(Class<S> sourceType, Class<T> targetType, S source) {
47+
return this.map(sourceType, targetType, source, false);
48+
}
49+
50+
public <S, T> T map(Class<S> sourceType, Class<T> targetType, S source, boolean usejMapper) {
51+
T result = null;
52+
53+
@SuppressWarnings("unchecked")
54+
BiConsumer<S, T> biConsumer = (BiConsumer<S, T>) mappings.get(new MappingKey<S, T>(sourceType, targetType));
55+
// Map fields by name...
56+
if (biConsumer != null && usejMapper == false) {
57+
try {
58+
result = targetType.getConstructor().newInstance();
59+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
60+
| InvocationTargetException | NoSuchMethodException | SecurityException
61+
| ObjectMapperProcessingException e) {
62+
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperProcessingException(
63+
e.getMessage(), e);
64+
}
65+
try {
66+
biConsumer.accept(source, result);
67+
} catch (ObjectMapperMappingException e) {
68+
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException(
69+
e.getMessage(), e);
70+
}
71+
} else {
72+
@SuppressWarnings("unchecked")
73+
JMapper<T, S> jMapper = (JMapper<T, S>) mappers.get(new MappingKey<S, T>(sourceType, targetType));
74+
75+
if (jMapper == null) {
76+
JMapperAPI jmapperApi = new JMapperAPI().add(mappedClass(targetType).add(global()));
77+
try {
78+
jMapper = new JMapper<>(targetType, sourceType, jmapperApi);
79+
T tmp = jMapper.getDestination(source);
80+
result = tmp;
81+
} catch (JMapperException | ObjectMapperMappingException e) {
82+
throw new info.unterrainer.commons.serialization.objectmapper.exceptions.ObjectMapperMappingException(
83+
e.getMessage(), e);
84+
}
85+
}
86+
if (result != null) {
87+
mappers.put(new MappingKey<S, T>(sourceType, targetType), jMapper);
88+
}
89+
}
90+
91+
return result;
92+
}
93+
94+
// mapping function
95+
// Object to Object mapper
96+
public <A, B> void registerMapping(Class<A> sourceType, Class<B> targetType, BiConsumer<A, B> forth,
97+
BiConsumer<B, A> back) {
98+
mappings.put(new MappingKey<A, B>(sourceType, targetType), forth);
99+
mappings.put(new MappingKey<B, A>(targetType, sourceType), back);
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package info.unterrainer.commons.serialization.objectmapper.exceptions;
2+
3+
public class ObjectMapperMappingException extends RuntimeException {
4+
5+
public ObjectMapperMappingException(final String message, final Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package info.unterrainer.commons.serialization.objectmapper.exceptions;
2+
3+
public class ObjectMapperProcessingException extends RuntimeException {
4+
public ObjectMapperProcessingException(final String message, final Throwable cause) {
5+
super(message, cause);
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package info.unterrainer.commons.serialization.objectmapper.key;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class MappingKey<S, T> {
7+
public MappingKey(Class<S> s, Class<T> t) {
8+
sourceType = s;
9+
targetType = t;
10+
}
11+
12+
private Class<S> sourceType;
13+
private Class<T> targetType;
14+
}

0 commit comments

Comments
 (0)