Skip to content

Commit b016df9

Browse files
committed
Just some fun changes :)
- Added some unit tests - Refactored JSONElement to JSONElement<interface> & JSONElementImpl<class> - removed TestProgram & test.txt - other cleanup
1 parent 92b2f3c commit b016df9

12 files changed

Lines changed: 951 additions & 316 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
target/
3+

EasyJSON.iml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" exported="" scope="PROVIDED" name="json-simple-1.1.1" level="project" />
15+
<orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter:5.7.2" level="project" />
16+
<orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.2" level="project" />
17+
<orderEntry type="library" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
18+
<orderEntry type="library" name="Maven: org.junit.platform:junit-platform-commons:1.7.2" level="project" />
19+
<orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter-params:5.7.2" level="project" />
20+
<orderEntry type="library" scope="RUNTIME" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.7.2" level="project" />
21+
<orderEntry type="library" scope="RUNTIME" name="Maven: org.junit.platform:junit-platform-engine:1.7.2" level="project" />
22+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-java8:6.11.0" level="project" />
23+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-core:6.11.0" level="project" />
24+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-gherkin:6.11.0" level="project" />
25+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-gherkin-messages:6.11.0" level="project" />
26+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:messages:15.0.0" level="project" />
27+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:tag-expressions:3.0.1" level="project" />
28+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-expressions:10.3.0" level="project" />
29+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:datatable:3.5.0" level="project" />
30+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-plugin:6.11.0" level="project" />
31+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:docstring:6.11.0" level="project" />
32+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:html-formatter:13.0.0" level="project" />
33+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:create-meta:4.0.0" level="project" />
34+
<orderEntry type="library" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
35+
<orderEntry type="library" scope="TEST" name="Maven: net.jodah:typetools:0.6.3" level="project" />
36+
<orderEntry type="library" scope="TEST" name="Maven: io.cucumber:cucumber-junit:6.11.0" level="project" />
37+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" />
38+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
39+
</component>
40+
</module>

pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>xyz.victorolatain</groupId>
8+
<artifactId>easyjson</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<properties>
11+
<maven.compiler.source>16</maven.compiler.source>
12+
<maven.compiler.target>16</maven.compiler.target>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter</artifactId>
19+
<version>5.7.2</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.cucumber</groupId>
24+
<artifactId>cucumber-java8</artifactId>
25+
<version>6.11.0</version>
26+
<scope>test</scope>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>io.cucumber</groupId>
31+
<artifactId>cucumber-junit</artifactId>
32+
<version>6.11.0</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
37+
</project>

src/main/java/xyz/victorolaitan/easyjson/DatabaseHelper.java

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,69 @@
11
package xyz.victorolaitan.easyjson;
22

33
import java.lang.reflect.Field;
4+
import java.lang.reflect.InvocationTargetException;
45
import java.util.ArrayList;
6+
import java.util.List;
57

68
public class DatabaseHelper {
79

8-
public static <T> T deserializeClass(Class<T> aClass, EasyJSON jsonStructure) throws EasyJSONException {
9-
return deserializeClass(aClass, jsonStructure.getRootNode());
10+
public static <T> T deserializeToClass(Class<T> aClass, EasyJSON jsonStructure) throws EasyJSONException {
11+
return deserializeToClass(aClass, jsonStructure.getRootNode());
1012
}
1113

12-
public static <T> T deserializeClass(Class<T> aClass, JSONElement jsonElement) throws EasyJSONException {
13-
T instance = null;
14+
private static <T> T deserializeToClass(Class<T> aClass, JSONElement jsonElement) throws EasyJSONException {
15+
T instance;
1416
try {
15-
instance = aClass.newInstance();
16-
} catch (InstantiationException e) {
17+
instance = aClass.getDeclaredConstructor().newInstance();
18+
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException e) {
1719
throw new EasyJSONException(EasyJSONException.INSTANTIATION_ERROR, e);
1820
} catch (IllegalAccessException e) {
1921
throw new EasyJSONException(EasyJSONException.ILLEGAL_ACCESS, e);
2022
}
21-
deserializeInstance(instance, jsonElement);
23+
deserializeToInstance(instance, jsonElement);
2224
return instance;
2325
}
2426

25-
public static <T> void deserializeInstance(T instance, EasyJSON jsonStructure) throws EasyJSONException {
26-
deserializeInstance(instance, jsonStructure.getRootNode());
27+
public static <T> void deserializeToInstance(T instance, EasyJSON jsonStructure) throws EasyJSONException {
28+
deserializeToInstance(instance, jsonStructure.getRootNode());
2729
}
2830

29-
public static <T> void deserializeInstance(T instance, JSONElement jsonElement) throws EasyJSONException {
30-
for (int i = 0; i < jsonElement.getChildren().size(); i++) {
31-
JSONElement child = (JSONElement) jsonElement.getChildren().get(i);
32-
31+
private static <T> void deserializeToInstance(T instance, JSONElement jsonElement) throws EasyJSONException {
32+
for (JSONElement childElement : jsonElement) {
3333
try {
34-
if (child.getKey() != null) {
35-
Field field = instance.getClass().getDeclaredField(child.getKey());
36-
field.setAccessible(true);
37-
switch (child.getType()) {
38-
case PRIMITIVE:
39-
field.set(instance, child.getValue());
40-
break;
41-
case ARRAY:
42-
field.set(instance, extractArrayElements(child));
43-
break;
44-
case STRUCTURE:
45-
field.set(instance, deserializeClass(field.getType(), child));
46-
break;
47-
case ROOT:
48-
break;
49-
}
34+
Field field = instance.getClass().getDeclaredField(childElement.getKey());
35+
field.setAccessible(true);
36+
switch (childElement.getType()) {
37+
case PRIMITIVE:
38+
field.set(instance, childElement.getValue());
39+
break;
40+
case ARRAY:
41+
if (field.getType().isAssignableFrom(List.class)) {
42+
field.set(
43+
instance,
44+
extractArrayElements(childElement)
45+
);
46+
}
47+
break;
48+
case STRUCTURE:
49+
field.set(instance, deserializeToClass(field.getType(), childElement));
50+
break;
51+
case ROOT:
52+
break;
5053
}
5154
} catch (IllegalAccessException e) {
5255
throw new EasyJSONException(EasyJSONException.ILLEGAL_ACCESS, e);
5356
} catch (NoSuchFieldException e) {
5457
throw new EasyJSONException(EasyJSONException.FIELD_NOT_FOUND, e);
58+
} catch (InstantiationException e) {
59+
throw new EasyJSONException(EasyJSONException.INSTANTIATION_ERROR, e);
5560
}
5661
}
5762
}
5863

59-
private static ArrayList extractArrayElements(JSONElement jsonElement) {
60-
ArrayList values = new ArrayList();
61-
for (int i = 0; i < jsonElement.getChildren().size(); i++) {
62-
JSONElement e = (JSONElement) jsonElement.getChildren().get(i);
64+
private static List<?> extractArrayElements(JSONElement arrayElement) throws IllegalAccessException, InstantiationException {
65+
List<Object> values = new ArrayList<>();
66+
for (JSONElement e : arrayElement) {
6367
values.add(e.getValue());
6468
}
6569
return values;
@@ -81,36 +85,43 @@ public static <T> JSONElement serializeClass(Class<T> aClass, JSONElement source
8185
return serialize(null, aClass, sourceElement, excludeFields);
8286
}
8387

84-
private static <T> JSONElement serialize(T instance, Class tClass, JSONElement sourceElement, Object... excludeFields) throws EasyJSONException {
88+
private static <T> JSONElement serialize(T instance, Class<? extends T> tClass, JSONElement sourceElement, Object... excludeFields) throws EasyJSONException {
8589
for (Field field : tClass.getDeclaredFields()) {
86-
if (!excludeContains(field, instance, excludeFields)) {
87-
try {
90+
try {
91+
if (!excludeContains(field, instance, excludeFields)) {
8892
Object fieldValue = field.get(instance);
89-
Class enclosingClass = fieldValue.getClass().getEnclosingClass();
93+
Class<?> enclosingClass = fieldValue.getClass().getEnclosingClass();
9094
if (enclosingClass != null && enclosingClass.equals(tClass)) {
91-
sourceElement.putStructure(field.getName(),
92-
serialize(fieldValue, fieldValue.getClass(), EasyJSON.create().getRootNode(), excludeFields));
93-
} else if (fieldValue instanceof ArrayList) {
94-
sourceElement.putArray(field.getName(), ((ArrayList) fieldValue).toArray());
95+
// typeof fieldValue == some subclass of tClass
96+
sourceElement.putStructure(
97+
field.getName(),
98+
serialize(fieldValue, fieldValue.getClass(), EasyJSON.create().getRootNode(), excludeFields)
99+
);
100+
} else if (fieldValue instanceof List) {
101+
// typeof fieldValue == List<?>
102+
sourceElement.putArray(field.getName(), ((List<?>) fieldValue).toArray());
95103
} else {
104+
// typeof fieldValue == some class implementing .toString()
96105
sourceElement.putPrimitive(field.getName(), fieldValue);
97106
}
98-
} catch (Exception ignored) {
99107
}
108+
} catch (IllegalAccessException e) {
109+
throw new EasyJSONException(EasyJSONException.ILLEGAL_ACCESS, e);
100110
}
101111
}
102112
return sourceElement;
103113
}
104114

105-
private static boolean excludeContains(Field field, Object instance, Object... excludeFields) {
115+
private static boolean excludeContains(Field field, Object instance, Object... excludeFields) throws IllegalAccessException {
116+
Object fieldValue;
117+
try {
118+
fieldValue = field.get(instance);
119+
} catch (NullPointerException ignored) {
120+
return true; // all instance fields in a static context are excluded
121+
}
106122
for (Object e : excludeFields) {
107-
try {
108-
// fml
109-
Object value = field.get(instance);
110-
if (value == e) {
111-
return true;
112-
}
113-
} catch (Exception ignored) {
123+
if (fieldValue == e) {
124+
return true;
114125
}
115126
}
116127
return false;

0 commit comments

Comments
 (0)