diff --git a/pom.xml b/pom.xml index 2518539..177e69a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 de.arraying Kotys - 0.3.3 + 0.3.4 diff --git a/src/main/java/de/arraying/kotys/JSON.java b/src/main/java/de/arraying/kotys/JSON.java index eba2077..7e75cd0 100644 --- a/src/main/java/de/arraying/kotys/JSON.java +++ b/src/main/java/de/arraying/kotys/JSON.java @@ -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. @@ -276,15 +276,13 @@ public final String marshal() { Map.Entry 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(); } @@ -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. diff --git a/src/main/java/de/arraying/kotys/JSONArray.java b/src/main/java/de/arraying/kotys/JSONArray.java index ce43b33..5f3b9a7 100644 --- a/src/main/java/de/arraying/kotys/JSONArray.java +++ b/src/main/java/de/arraying/kotys/JSONArray.java @@ -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(); } diff --git a/src/main/java/de/arraying/kotys/JSONContainer.java b/src/main/java/de/arraying/kotys/JSONContainer.java deleted file mode 100644 index aacfeac..0000000 --- a/src/main/java/de/arraying/kotys/JSONContainer.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.arraying.kotys; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Copyright 2017 Arraying - *

- * 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. - */ -@SuppressWarnings("WeakerAccess") -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface JSONContainer {} diff --git a/src/main/java/de/arraying/kotys/JSONForger.java b/src/main/java/de/arraying/kotys/JSONForger.java deleted file mode 100644 index c8fe4f4..0000000 --- a/src/main/java/de/arraying/kotys/JSONForger.java +++ /dev/null @@ -1,36 +0,0 @@ -package de.arraying.kotys; - -/** - * Copyright 2017 Arraying - *

- * 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. - */ -@SuppressWarnings({"unused", "WeakerAccess"}) -public class JSONForger { - - /** - * "Forges" the {@link de.arraying.kotys.JSONContainer} annotation, treating instances as if they have this annotation. - * @param container The class of the container that should be forged. - * @return The current JSONForger object, for chaining purposes. - * @throws IllegalArgumentException If the container is null. - */ - public JSONForger treatAsClassAnnotation(Class container) - throws IllegalArgumentException { - if(container == null) { - throw new IllegalArgumentException("Container is null"); - } - JSONStorage.getInstance().register(container); - return this; - } - -} diff --git a/src/main/java/de/arraying/kotys/JSONFormatter.java b/src/main/java/de/arraying/kotys/JSONFormatter.java index 0fccaed..c9d7056 100644 --- a/src/main/java/de/arraying/kotys/JSONFormatter.java +++ b/src/main/java/de/arraying/kotys/JSONFormatter.java @@ -19,7 +19,6 @@ final class JSONFormatter { private final StringBuilder builder = new StringBuilder(); - private final JSONUtil util = new JSONUtil(); /** * Starts a new JSON object. @@ -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; } @@ -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) diff --git a/src/main/java/de/arraying/kotys/JSONORM.java b/src/main/java/de/arraying/kotys/JSONORM.java index aa468a0..b36017b 100644 --- a/src/main/java/de/arraying/kotys/JSONORM.java +++ b/src/main/java/de/arraying/kotys/JSONORM.java @@ -172,18 +172,13 @@ T mapTo(JSON json, String... ignoredJSONKeys) { * @return An immutable list of field containers. */ private List getAllFields() { - boolean classBasedAnnotation = container.getClass().isAnnotationPresent(JSONContainer.class); - boolean fakeClassBasedAnnotation = JSONStorage.getInstance().has(container.getClass()); List 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); diff --git a/src/main/java/de/arraying/kotys/JSONStorage.java b/src/main/java/de/arraying/kotys/JSONStorage.java deleted file mode 100644 index 9e35834..0000000 --- a/src/main/java/de/arraying/kotys/JSONStorage.java +++ /dev/null @@ -1,59 +0,0 @@ -package de.arraying.kotys; - -import java.util.HashSet; -import java.util.Set; - -/** - * Copyright 2017 Arraying - *

- * 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. - */ -class JSONStorage { - - private static JSONStorage instance; - private static final Object mutex = new Object(); - private final Set> classes = new HashSet<>(); - - /** - * Gets the storage singleton instance. - * @return The instance. - */ - static JSONStorage getInstance() { - if(instance == null) { - synchronized(mutex) { - if(instance == null) { - instance = new JSONStorage(); - } - } - } - return instance; - } - - /** - * Registers a class. - * @param clazz The class. Not null. - */ - synchronized void register(Class clazz) { - classes.add(clazz); - } - - /** - * Whether or not the class is registered. - * @param clazz The class. - * @return True if it is, false otherwise. - */ - synchronized boolean has(Class clazz) { - return classes.contains(clazz); - } - -} diff --git a/src/main/java/de/arraying/kotys/JSONUtil.java b/src/main/java/de/arraying/kotys/JSONUtil.java index bc1631a..5bd9f44 100644 --- a/src/main/java/de/arraying/kotys/JSONUtil.java +++ b/src/main/java/de/arraying/kotys/JSONUtil.java @@ -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.