diff --git a/src/org/atriasoft/ejson/JsonMapper.java__ b/src/org/atriasoft/ejson/JsonMapper.java
similarity index 92%
rename from src/org/atriasoft/ejson/JsonMapper.java__
rename to src/org/atriasoft/ejson/JsonMapper.java
index 4036c01..2a5aa52 100644
--- a/src/org/atriasoft/ejson/JsonMapper.java__
+++ b/src/org/atriasoft/ejson/JsonMapper.java
@@ -8,12 +8,12 @@ import java.nio.file.Path;
import org.atriasoft.aknot.exception.AknotException;
import org.atriasoft.aknot.model.ModelType;
-import org.atriasoft.aknot.pojo.IntrospectionObject;
-import org.atriasoft.ejson.builder.BuilderGeneric;
+import org.atriasoft.ejson.builder.Builder;
import org.atriasoft.ejson.builder.BuilderIntrospection;
-import org.atriasoft.ejson.exception.EjsonBuilderException;
+import org.atriasoft.ejson.builder.IntrospectionObject;
import org.atriasoft.ejson.exception.EjsonException;
import org.atriasoft.ejson.exception.EjsonNodeDoesNotExist;
+import org.atriasoft.ejson.internal.Log;
import org.atriasoft.ejson.parser.ParseJson;
import org.atriasoft.ejson.parser.ParsingProperty;
import org.atriasoft.etk.Uri;
@@ -29,6 +29,7 @@ public class JsonMapper {
}
+ /*
public void generate(final Object root, final StringBuilder data) throws EjsonException, AknotException {
GeneratorIntrospection generator;
try {
@@ -41,7 +42,7 @@ public class JsonMapper {
e.printStackTrace();
}
}
-
+ */
/**
* Parse a single root node that have the name of the Class Parsed:
*
@@ -56,7 +57,7 @@ public class JsonMapper {
*/
@SuppressWarnings("unchecked")
public T parse(final String data, final Class classType) throws EjsonException, EjsonNodeDoesNotExist, AknotException {
- BuilderGeneric builder;
+ Builder builder;
try {
builder = new BuilderIntrospection(ModelType.NORMAL, classType);
final ParseJson parser = new ParseJson(builder);
@@ -64,9 +65,10 @@ public class JsonMapper {
property.setDisplayError(true);
final IntrospectionObject introspectionObject = (IntrospectionObject) parser.parse(data, property);
- introspectionObject.generateTheObject();
+ introspectionObject.generateTheObject(true);
final Object listRet = introspectionObject.getData();
- if (listRet != null && !listRet.getClass().isArray() && listRet.getClass().componentType() == classType) {
+ Log.error("elements: {} , {}", listRet.getClass(), classType);
+ if (listRet != null && !listRet.getClass().isArray() && listRet.getClass() == classType) {
return (T) listRet;
} else if (listRet != null && listRet.getClass().isArray() && listRet.getClass().componentType() == classType) {
final T[] tmp = (T[]) listRet;
@@ -101,7 +103,7 @@ public class JsonMapper {
final String content = new String(elemData);
return parse(content, classType);
}
-
+ /*
public void store(final Object root, final Path path) throws EjsonException, AknotException, IOException {
final StringBuilder builder = new StringBuilder();
generate(root, builder);
@@ -113,5 +115,5 @@ public class JsonMapper {
generate(root, builder);
Uri.writeAll(uri, builder.toString());
}
-
+ */
}
diff --git a/src/org/atriasoft/ejson/builder/Builder.java b/src/org/atriasoft/ejson/builder/Builder.java
index 55b61d0..64199d2 100644
--- a/src/org/atriasoft/ejson/builder/Builder.java
+++ b/src/org/atriasoft/ejson/builder/Builder.java
@@ -5,7 +5,10 @@
*/
package org.atriasoft.ejson.builder;
+import org.atriasoft.aknot.exception.AknotException;
import org.atriasoft.ejson.exception.EjsonBuilderException;
+import org.atriasoft.ejson.exception.EjsonException;
+import org.atriasoft.ejson.exception.EjsonNodeDoesNotExist;
public interface Builder {
@@ -13,8 +16,9 @@ public interface Builder {
* End of parsing element
* @param parent Element representing the end of declaration
* @throws EjsonBuilderException Error with this node or element.
+ * @throws AknotException
*/
- void endParsing(Object parent) throws EjsonBuilderException;
+ void endParsing(Object parent) throws EjsonBuilderException, AknotException;
/**
* New comment added on this Element (For an Array)
@@ -30,8 +34,12 @@ public interface Builder {
* @param nodeName name of the element where the node is added
* @return Declaration object value
* @throws EjsonBuilderException Error with this node or element.
+ * @throws AknotException
+ * @throws EjsonNodeDoesNotExist
*/
- Object newArray(Object parent, String nodeName) throws EjsonBuilderException;
+ Object newArray(Object parent, String nodeName) throws EjsonBuilderException, AknotException, EjsonNodeDoesNotExist;
+
+ void newArrayFinished(Object parent, String string, Object obj) throws EjsonException, AknotException;
/**
* Add a property on the Element. (For an Array)
@@ -40,7 +48,7 @@ public interface Builder {
* @param propertyValue Value of the property
* @throws EjsonBuilderException Error with this node or element.
*/
- void newBoolean(Object parent, boolean value) throws EjsonBuilderException, Exception;
+ void newBoolean(Object parent, boolean value) throws EjsonException, AknotException;
/**
* Add a property on the Element. (For an Object)
@@ -48,14 +56,14 @@ public interface Builder {
* @param nodeName name of the element where the node is added
* @throws EjsonBuilderException Error with this node or element.
*/
- void newBoolean(Object parent, String nodeName, boolean value) throws EjsonBuilderException, Exception;
+ void newBoolean(Object parent, String nodeName, boolean value) throws EjsonException, AknotException;
/**
* New null element detected on the current node (For an Array)
* @param parent Parent representing the node of the null is set
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNull(Object parent) throws EjsonBuilderException, Exception;
+ void newNull(Object parent) throws EjsonException, AknotException;
/**
* New null element detected on the current node (For an Object)
@@ -63,7 +71,7 @@ public interface Builder {
* @param nodeName name of the element where the node is added
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNull(Object parent, String nodeName) throws EjsonBuilderException, Exception;
+ void newNull(Object parent, String nodeName) throws EjsonException, AknotException;
/**
* New number xxx.yyy element detected on the current node (For an Array)
@@ -71,7 +79,7 @@ public interface Builder {
* @param value Double value of the number
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNumber(Object parent, double value) throws EjsonBuilderException, Exception;
+ void newNumber(Object parent, double value) throws EjsonException, AknotException;
/**
* New number xxx element detected on the current node (For an Array)
@@ -79,7 +87,7 @@ public interface Builder {
* @param value Long value of the number
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNumber(Object parent, long value) throws EjsonBuilderException, Exception;
+ void newNumber(Object parent, long value) throws EjsonException, AknotException;
/**
* New number xxx.yyy element detected on the current node (For an Object)
@@ -88,7 +96,7 @@ public interface Builder {
* @param value Double value of the number
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNumber(Object parent, String nodeName, double value) throws EjsonBuilderException, Exception;
+ void newNumber(Object parent, String nodeName, double value) throws EjsonException, AknotException;
/**
* New number xxx element detected on the current node (For an Object)
@@ -97,7 +105,7 @@ public interface Builder {
* @param value Long value of the number
* @throws EjsonBuilderException Error with this node or element.
*/
- void newNumber(Object parent, String nodeName, long value) throws EjsonBuilderException, Exception;
+ void newNumber(Object parent, String nodeName, long value) throws EjsonException, AknotException;
/**
* Add a new sub-element on the current parent element (For an Array)
@@ -105,7 +113,7 @@ public interface Builder {
* @return the object representing the Element.
* @throws EjsonBuilderException Error with this node or element.
*/
- Object newObject(Object parent) throws EjsonBuilderException, Exception;
+ Object newObject(Object parent) throws EjsonException, AknotException;
/**
* Add a new sub-element on the current parent element (For an Object)
@@ -114,14 +122,14 @@ public interface Builder {
* @return the object representing the Element.
* @throws EjsonBuilderException Error with this node or element.
*/
- Object newObject(Object parent, String nodeName) throws EjsonBuilderException, Exception;
+ Object newObject(Object parent, String nodeName) throws EjsonException, AknotException;
/**
* Create or get the root element of the document
* @return An object that id a root element.
* @throws EjsonBuilderException Error with this node or element.
*/
- Object newRoot() throws EjsonBuilderException;
+ Object newRoot() throws EjsonBuilderException, AknotException;
/**
* Add a new sub-element on the current parent element (For an Array)
@@ -130,7 +138,7 @@ public interface Builder {
* @return the object representing the Element.
* @throws EjsonBuilderException Error with this node or element.
*/
- void newString(Object parent, String value) throws EjsonBuilderException, Exception;
+ void newString(Object parent, String value) throws EjsonException, AknotException;
/**
* Add a new sub-element on the current parent element (For an Object)
@@ -140,5 +148,5 @@ public interface Builder {
* @return the object representing the Element.
* @throws EjsonBuilderException Error with this node or element.
*/
- void newString(Object parent, String nodeName, String value) throws EjsonBuilderException, Exception;
+ void newString(Object parent, String nodeName, String value) throws EjsonException, AknotException;
}
diff --git a/src/org/atriasoft/ejson/builder/BuilderGeneric.java b/src/org/atriasoft/ejson/builder/BuilderGeneric.java
index c30f61d..65aaca0 100644
--- a/src/org/atriasoft/ejson/builder/BuilderGeneric.java
+++ b/src/org/atriasoft/ejson/builder/BuilderGeneric.java
@@ -5,7 +5,9 @@
*/
package org.atriasoft.ejson.builder;
+import org.atriasoft.aknot.exception.AknotException;
import org.atriasoft.ejson.exception.EjsonBuilderException;
+import org.atriasoft.ejson.exception.EjsonException;
import org.atriasoft.ejson.model.JsonArray;
import org.atriasoft.ejson.model.JsonBoolean;
import org.atriasoft.ejson.model.JsonNull;
@@ -41,7 +43,13 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newBoolean(final Object parent, final boolean value) throws EjsonBuilderException, Exception {
+ public void newArrayFinished(final Object parent, final String string, final Object obj) throws EjsonException, AknotException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void newBoolean(final Object parent, final boolean value) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonBoolean out = new JsonBoolean(value);
elem.add(out);
@@ -52,7 +60,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newBoolean(final Object parent, final String nodeName, final boolean value) throws EjsonBuilderException, Exception {
+ public void newBoolean(final Object parent, final String nodeName, final boolean value) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonBoolean out = new JsonBoolean(value);
elem.put(nodeName, out);
@@ -62,7 +70,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNull(final Object parent) throws EjsonBuilderException, Exception {
+ public void newNull(final Object parent) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonNull out = new JsonNull();
elem.add(out);
@@ -73,7 +81,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNull(final Object parent, final String nodeName) throws EjsonBuilderException, Exception {
+ public void newNull(final Object parent, final String nodeName) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonNull out = new JsonNull();
elem.put(nodeName, out);
@@ -83,7 +91,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNumber(final Object parent, final double value) throws EjsonBuilderException, Exception {
+ public void newNumber(final Object parent, final double value) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonNumber out = new JsonNumber(value);
elem.add(out);
@@ -94,7 +102,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNumber(final Object parent, final long value) throws EjsonBuilderException, Exception {
+ public void newNumber(final Object parent, final long value) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonNumber out = new JsonNumber(value);
elem.add(out);
@@ -105,7 +113,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNumber(final Object parent, final String nodeName, final double value) throws EjsonBuilderException, Exception {
+ public void newNumber(final Object parent, final String nodeName, final double value) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonNumber out = new JsonNumber(value);
elem.put(nodeName, out);
@@ -115,7 +123,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newNumber(final Object parent, final String nodeName, final long value) throws EjsonBuilderException, Exception {
+ public void newNumber(final Object parent, final String nodeName, final long value) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonNumber out = new JsonNumber(value);
elem.put(nodeName, out);
@@ -125,7 +133,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public Object newObject(final Object parent) throws EjsonBuilderException, Exception {
+ public Object newObject(final Object parent) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonObject out = new JsonObject();
elem.add(out);
@@ -135,7 +143,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public Object newObject(final Object parent, final String nodeName) throws EjsonBuilderException, Exception {
+ public Object newObject(final Object parent, final String nodeName) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonObject out = new JsonObject();
elem.put(nodeName, out);
@@ -150,7 +158,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newString(final Object parent, final String value) throws EjsonBuilderException, Exception {
+ public void newString(final Object parent, final String value) throws EjsonException, AknotException {
if (parent instanceof final JsonArray elem) {
final JsonString out = new JsonString(value);
elem.add(out);
@@ -161,7 +169,7 @@ public class BuilderGeneric implements Builder {
}
@Override
- public void newString(final Object parent, final String nodeName, final String value) throws EjsonBuilderException, Exception {
+ public void newString(final Object parent, final String nodeName, final String value) throws EjsonException, AknotException {
if (parent instanceof final JsonObject elem) {
final JsonString out = new JsonString(value);
elem.put(nodeName, out);
diff --git a/src/org/atriasoft/ejson/builder/BuilderIntrospection.java b/src/org/atriasoft/ejson/builder/BuilderIntrospection.java
index 19b9415..91ff880 100644
--- a/src/org/atriasoft/ejson/builder/BuilderIntrospection.java
+++ b/src/org/atriasoft/ejson/builder/BuilderIntrospection.java
@@ -5,18 +5,18 @@
*/
package org.atriasoft.ejson.builder;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.atriasoft.aknot.StringSerializer;
import org.atriasoft.aknot.exception.AknotException;
import org.atriasoft.aknot.model.IntrospectionModel;
import org.atriasoft.aknot.model.ModelType;
import org.atriasoft.aknot.pojo.CacheIntrospectionModel;
import org.atriasoft.ejson.exception.EjsonBuilderException;
import org.atriasoft.ejson.exception.EjsonException;
-import org.atriasoft.ejson.model.JsonArray;
-import org.atriasoft.ejson.model.JsonBoolean;
-import org.atriasoft.ejson.model.JsonNull;
-import org.atriasoft.ejson.model.JsonNumber;
-import org.atriasoft.ejson.model.JsonObject;
-import org.atriasoft.ejson.model.JsonString;
+import org.atriasoft.ejson.exception.EjsonNodeDoesNotExist;
+import org.atriasoft.ejson.internal.Log;
public class BuilderIntrospection implements Builder {
// Keep in cach all the object alredy parsed ==> optimize CPU
@@ -32,158 +32,477 @@ public class BuilderIntrospection implements Builder {
}
@Override
- public void endParsing(final Object parent) throws EjsonBuilderException {
- // nothing to do ... this is for revert build.
+ public void endParsing(final Object element) throws EjsonBuilderException, AknotException {
+ Log.verbose("End of Element: {}", element);
+ if (element == null) {
+ return;
+ }
+ final IntrospectionObject introspectionObject = (IntrospectionObject) element;
+ if (introspectionObject.getModelIntrospection() == null) {
+ // property on nothing ???
+ return;
+ }
+ introspectionObject.generateTheObject(true);
}
@Override
public Object newArray(final Object parent) throws EjsonBuilderException {
+ throw new EjsonBuilderException("Must be implemented ...");
+ /*
if (parent instanceof final JsonArray elem) {
final JsonArray out = new JsonArray();
elem.add(out);
return out;
}
throw new EjsonBuilderException("can not add Comment on something else than array");
+ */
}
@Override
- public Object newArray(final Object parent, final String nodeName) throws EjsonBuilderException {
- if (parent instanceof final JsonObject elem) {
- final JsonArray out = new JsonArray();
- elem.put(nodeName, out);
- return out;
+ public Object newArray(final Object parent, final String nodeName) throws EjsonBuilderException, AknotException, EjsonNodeDoesNotExist {
+ if (parent == null) {
+ return null;
}
- throw new EjsonBuilderException("can not add Comment on something else than Object");
+ Log.verbose("new element on NodeName=" + nodeName);
+ final IntrospectionObject introspectionObject = (IntrospectionObject) parent;
+ final IntrospectionModel model = introspectionObject.getModelIntrospection();
+ Class> typeClass = null;
+ String listTreeName = null;
+ if (!introspectionObject.isSubNodeOrPropertyExist(nodeName) && model.isIgnoreUnknown()) {
+ Log.debug("Ignore array: '" + nodeName + "', it does not exist.");
+ return null;
+ }
+ try {
+ typeClass = introspectionObject.getTypeOfSubNode(nodeName, true);
+ } catch (final AknotException e) {
+ throw new EjsonNodeDoesNotExist("Fait to get type of node " + nodeName + " does not exist: " + e.getMessage());
+ }
+ try {
+ listTreeName = introspectionObject.getTreeNameOfSubNode(nodeName, true);
+ } catch (final AknotException e) {
+ throw new EjsonNodeDoesNotExist("Node does not exist: " + e.getMessage());
+ }
+ if (typeClass != null) {
+ // specific case for List ==> need to get the subType in introspection ...
+ if (typeClass.isArray()) {
+ final Class> subTypeClass = typeClass.getComponentType();
+ Log.verbose("Create array new 'SUB' class: '" + typeClass.getCanonicalName() + "' for node '" + nodeName + "'");
+ IntrospectionModel inferData = null;
+ inferData = this.cacheModel.findOrCreate(ModelType.LIST, listTreeName, subTypeClass);
+ // Create the data when object is ended created...
+ return new IntrospectionObject(inferData);
+ }
+ if (List.class.isAssignableFrom(typeClass)) {
+ Class> subTypeClass = null;
+ try {
+ subTypeClass = introspectionObject.getTypeOfSubNodeSubType(nodeName);
+ } catch (final AknotException e) {
+ throw new EjsonBuilderException("Node does not exist: " + e.getMessage());
+ }
+ Log.verbose("Create List new 'SUB' class: '" + typeClass.getCanonicalName() + "' for node '" + nodeName + "'");
+ IntrospectionModel inferData = null;
+ inferData = this.cacheModel.findOrCreate(ModelType.LIST, listTreeName, subTypeClass);
+ // Create the data when object is ended created...
+ return new IntrospectionObject(inferData);
+ }
+ Log.verbose("Create new class: '" + typeClass.getCanonicalName() + "' for node '" + nodeName + "'");
+ final IntrospectionModel inferData = this.cacheModel.findOrCreate(ModelType.NORMAL, null, typeClass);
+ // Create the data when object is ended created...
+ return new IntrospectionObject(inferData);
+ }
+ return null;
}
@Override
- public void newBoolean(final Object parent, final boolean value) throws EjsonBuilderException, Exception {
- if (parent instanceof final JsonArray elem) {
- final JsonBoolean out = new JsonBoolean(value);
- elem.add(out);
+ public void newArrayFinished(final Object parent, String tmpName, final Object element) throws EjsonException, AknotException {
+ Log.error("New element fionished : ==> " + tmpName);
+ if (parent == null || element == null) {
return;
}
- throw new EjsonBuilderException("can not add Comment on something else than array");
+ final IntrospectionObject introspectionElementObject = (IntrospectionObject) element;
+ if (introspectionElementObject.getModelIntrospection() == null) {
+ // property on nothing ???
+ return;
+ }
+ final IntrospectionObject introspectionParentObject = (IntrospectionObject) parent;
+ if (introspectionParentObject.getModelIntrospection() == null) {
+ if (tmpName.equals(this.rootNodeName)) {
+ // this is the root node ...
+ final Object tmpp = introspectionParentObject.getData();
+ if (tmpp instanceof List) {
+ @SuppressWarnings("unchecked")
+ final List