Skip to content

Commit

Permalink
Fixed marshalling issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Arraying authored and Arraying committed Dec 10, 2017
1 parent f58fe7c commit 821a852
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 151 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.arraying</groupId>
<artifactId>Kotys</artifactId>
<version>0.3.3</version>
<version>0.3.4</version>
<build>
<plugins>
<plugin>
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/de/arraying/kotys/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public JSON(Map<?, ?> map)

/**
* Creates a new JSON object from a container.
* This container object MUST be annotated with {@link de.arraying.kotys.JSONContainer} OR every wanted field MUST be annotated with {@link de.arraying.kotys.JSONField}.
* The JSON key will correspond to the field name, unless the field is annotated with {@link de.arraying.kotys.JSONField}.
* All selected fields will then be attempted to be converted into JSON.
* If a field is not a valid JSON datatype the object will then be taken, and this method will be called recursively.
* @param container A container.
Expand Down Expand Up @@ -276,15 +276,13 @@ public final String marshal() {
Map.Entry<String, Object> entry = iterator.next();
formatter.objectKey(entry.getKey());
Object valueRaw = entry.getValue();
Object value;
if(valueRaw instanceof JSON) {
value = ((JSON) valueRaw).marshal();
formatter.object(((JSON) valueRaw).marshal());
} else if(valueRaw instanceof JSONArray) {
value = ((JSONArray) valueRaw).marshal();
formatter.array(((JSONArray) valueRaw).marshal());
} else {
value = valueRaw;
formatter.value(valueRaw);
}
formatter.objectValue(value);
if(iterator.hasNext()) {
formatter.comma();
}
Expand All @@ -296,9 +294,7 @@ public final String marshal() {
/**
* Marshals (maps) the JSON object to the specified object.
* An instance of this object will be created.
* This object object MUST be annotated with {@link de.arraying.kotys.JSONContainer} OR every wanted field MUST be annotated with {@link de.arraying.kotys.JSONField}.
* If the object is annotated with {@link de.arraying.kotys.JSONContainer} then all JSON keys will be mapped to fields with the name of the key.
* Fields who's names are not in the JSON object will not get set.
* The JSON entry will be mapped to a field with the same name as the key, unless the field is anntoated with {@link de.arraying.kotys.JSONField}.
* If the field is annotated with {@link de.arraying.kotys.JSONField}, then the field will receive the value of the JSON key specified in the annotation.
* In the case that the JSON object does not contain the key specified in the annotation, the field will be initialized as null instead.
* When using arrays, then the array in T MUST be non primitive.
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/de/arraying/kotys/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,13 @@ public final String marshal() {
.startArray();
for(int i = 0; i < length(); i++) {
Object valueRaw = rawContent.get(i);
Object value;
if(valueRaw instanceof JSON) {
value = ((JSON) valueRaw).marshal();
formatter.object(((JSON) valueRaw).marshal());
} else if(valueRaw instanceof JSONArray) {
value = ((JSONArray) valueRaw).marshal();
formatter.array(((JSONArray) valueRaw).marshal());
} else {
value = valueRaw;
formatter.value(valueRaw);
}
formatter.array(value);
if(i + 1 < length()) {
formatter.comma();
}
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/de/arraying/kotys/JSONContainer.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/de/arraying/kotys/JSONForger.java

This file was deleted.

23 changes: 16 additions & 7 deletions src/main/java/de/arraying/kotys/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
final class JSONFormatter {

private final StringBuilder builder = new StringBuilder();
private final JSONUtil util = new JSONUtil();

/**
* Starts a new JSON object.
Expand Down Expand Up @@ -67,12 +66,22 @@ JSONFormatter comma() {
}

/**
* Appends an array entry.
* @param value The value.
* Appends an object.
* @param object The object.
* @return The formatter for chaining purposes.
*/
JSONFormatter array(Object value) {
builder.append(value);
JSONFormatter object(String object) {
builder.append(object);
return this;
}

/**
* Appends an array.
* @param array The array.
* @return The formatter for chaining purposes.
*/
JSONFormatter array(String array) {
builder.append(array);
return this;
}

Expand All @@ -90,11 +99,11 @@ JSONFormatter objectKey(String key) {
}

/**
* Appends the object value.
* Appends the value.
* @param value The value of the object.
* @return The formatter for chaining purposes.
*/
JSONFormatter objectValue(Object value) {
JSONFormatter value(Object value) {
if(value instanceof String) {
builder.append("\"")
.append(value)
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/de/arraying/kotys/JSONORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,13 @@ T mapTo(JSON json, String... ignoredJSONKeys) {
* @return An immutable list of field containers.
*/
private List<FieldContainer> getAllFields() {
boolean classBasedAnnotation = container.getClass().isAnnotationPresent(JSONContainer.class);
boolean fakeClassBasedAnnotation = JSONStorage.getInstance().has(container.getClass());
List<FieldContainer> fieldContainers = new ArrayList<>();
for(Field classField : container.getClass().getDeclaredFields()) {
if(classBasedAnnotation
|| fakeClassBasedAnnotation) {
fieldContainers.add(new FieldContainer(classField, classField.getName()));
continue;
}
if(classField.isAnnotationPresent(JSONField.class)) {
String key = classField.getAnnotation(JSONField.class).key();
fieldContainers.add(new FieldContainer(classField, key));
} else {
fieldContainers.add(new FieldContainer(classField, classField.getName()));
}
}
return Collections.unmodifiableList(fieldContainers);
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/de/arraying/kotys/JSONStorage.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/de/arraying/kotys/JSONUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ Object getFinalValue(Object value, String... ignoredFields) {
}
}


/**
* Checks if the specified object is a valid JSON data type.
* This presumes that all arrays have already been parsed.
Expand Down

0 comments on commit 821a852

Please sign in to comment.