├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.jdt.core.prefs
└── org.eclipse.m2e.core.prefs
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── dzone
│ └── albanoj2
│ └── examples
│ └── annotation
│ └── jsonserializer
│ ├── JsonField.java
│ ├── JsonSerializeException.java
│ └── JsonSerializer.java
└── test
└── java
└── com
└── dzone
└── albanoj2
└── examples
└── annotation
└── jsonserializer
├── JsonSerializerTest.java
└── data
└── Car.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | pom.xml.tag
3 | pom.xml.releaseBackup
4 | pom.xml.versionsBackup
5 | pom.xml.next
6 | release.properties
7 | dependency-reduced-pom.xml
8 | buildNumber.properties
9 | .mvn/timing.properties
10 |
11 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
12 | !/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | json-serializer
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.8
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
13 | org.eclipse.jdt.core.compiler.source=1.8
14 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.dzone.albanoj2.examples.annotation
5 | json-serializer
6 | 0.0.1-SNAPSHOT
7 | JsonSerializer
8 |
9 |
10 |
11 | junit
12 | junit
13 | 4.12
14 | test
15 |
16 |
17 | org.hamcrest
18 | hamcrest-all
19 | 1.3
20 | test
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/main/java/com/dzone/albanoj2/examples/annotation/jsonserializer/JsonField.java:
--------------------------------------------------------------------------------
1 | package com.dzone.albanoj2.examples.annotation.jsonserializer;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.RUNTIME)
9 | @Target(ElementType.FIELD)
10 | public @interface JsonField {
11 | public String value() default "";
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/dzone/albanoj2/examples/annotation/jsonserializer/JsonSerializeException.java:
--------------------------------------------------------------------------------
1 | package com.dzone.albanoj2.examples.annotation.jsonserializer;
2 |
3 | public class JsonSerializeException extends Exception {
4 |
5 | private static final long serialVersionUID = -8845242379503538623L;
6 |
7 | public JsonSerializeException(String message) {
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/dzone/albanoj2/examples/annotation/jsonserializer/JsonSerializer.java:
--------------------------------------------------------------------------------
1 | package com.dzone.albanoj2.examples.annotation.jsonserializer;
2 |
3 | import static java.util.Objects.requireNonNull;
4 | import static java.util.stream.Collectors.joining;
5 |
6 | import java.lang.reflect.Field;
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | public class JsonSerializer {
11 |
12 | public String serialize(Object object) throws JsonSerializeException {
13 |
14 | try {
15 | Class> objectClass = requireNonNull(object).getClass();
16 | Map jsonElements = new HashMap<>();
17 |
18 | for (Field field: objectClass.getDeclaredFields()) {
19 | field.setAccessible(true);
20 | if (field.isAnnotationPresent(JsonField.class)) {
21 | jsonElements.put(getSerializedKey(field), (String) field.get(object));
22 | }
23 | }
24 | System.out.println(toJsonString(jsonElements));
25 | return toJsonString(jsonElements);
26 | }
27 | catch (IllegalAccessException e) {
28 | throw new JsonSerializeException(e.getMessage());
29 | }
30 | }
31 |
32 | private String toJsonString(Map jsonMap) {
33 | String elementsString = jsonMap.entrySet()
34 | .stream()
35 | .map(entry -> "\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"")
36 | .collect(joining(","));
37 | return "{" + elementsString + "}";
38 | }
39 |
40 | private static String getSerializedKey(Field field) {
41 | String annotationValue = field.getAnnotation(JsonField.class).value();
42 |
43 | if (annotationValue.isEmpty()) {
44 | return field.getName();
45 | }
46 | else {
47 | return annotationValue;
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/com/dzone/albanoj2/examples/annotation/jsonserializer/JsonSerializerTest.java:
--------------------------------------------------------------------------------
1 | package com.dzone.albanoj2.examples.annotation.jsonserializer;
2 |
3 | import static org.hamcrest.Matchers.isOneOf;
4 | import static org.junit.Assert.assertThat;
5 |
6 | import org.hamcrest.Matcher;
7 | import org.junit.Test;
8 |
9 | import com.dzone.albanoj2.examples.annotation.jsonserializer.data.Car;
10 |
11 | public class JsonSerializerTest {
12 |
13 | @Test(expected = NullPointerException.class)
14 | public void serializeNullObjectEnsureNullPointerExceptionThrown() throws Exception {
15 | new JsonSerializer().serialize(null);
16 | }
17 |
18 | @Test
19 | public void serializeCarObjectEnsureCorrectOutputJson() throws Exception {
20 | JsonSerializer serializer = new JsonSerializer();
21 | Car testCar = new Car("Ford", "F150", "2018");
22 | String json = serializer.serialize(testCar);
23 | assertThat(json, isExpectedCarJson(testCar));
24 | }
25 |
26 | private static Matcher isExpectedCarJson(Car car) {
27 | return isOneOf(
28 | "{\"manufacturer\":\"" + car.getMake() + "\",\"model\":\"" + car.getModel() + "\"}",
29 | "{\"model\":\"" + car.getModel() + "\",\"manufacturer\":\"" + car.getMake() + "\"}"
30 | );
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/com/dzone/albanoj2/examples/annotation/jsonserializer/data/Car.java:
--------------------------------------------------------------------------------
1 | package com.dzone.albanoj2.examples.annotation.jsonserializer.data;
2 |
3 | import com.dzone.albanoj2.examples.annotation.jsonserializer.JsonField;
4 |
5 | public class Car {
6 |
7 | @JsonField("manufacturer")
8 | private final String make;
9 |
10 | @JsonField
11 | private final String model;
12 |
13 | private final String year;
14 |
15 | public Car(String make, String model, String year) {
16 | this.make = make;
17 | this.model = model;
18 | this.year = year;
19 | }
20 |
21 | public String getMake() {
22 | return make;
23 | }
24 |
25 | public String getModel() {
26 | return model;
27 | }
28 |
29 | public String getYear() {
30 | return year;
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | return year + " " + make + " " + model;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------