diff --git a/.checkstyle b/.checkstyle index 34ed486..428926e 100644 --- a/.checkstyle +++ b/.checkstyle @@ -1,7 +1,7 @@ - + diff --git a/ejson.iml b/ejson.iml new file mode 100644 index 0000000..d47abfb --- /dev/null +++ b/ejson.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/org/atriasoft/ejson/Ejson.java b/src/org/atriasoft/ejson/Ejson.java index c7c42e3..eb10903 100644 --- a/src/org/atriasoft/ejson/Ejson.java +++ b/src/org/atriasoft/ejson/Ejson.java @@ -19,22 +19,23 @@ import org.atriasoft.etk.Uri; public class Ejson { /** - * Display the Document on console + * Display the Document on console */ public static void display(final JsonNode root) { final StringBuilder tmpp = new StringBuilder(); SerializerJson.serialize(root, tmpp, 0); Log.info("Generated JSON : \n" + tmpp.toString()); } - + /** * Generate a string that contain the created XML + * * @param data Data where the json is stored */ public static void generate(final JsonNode root, final StringBuilder data) { SerializerJson.serialize(root, data, 1); } - + public static JsonNode parse(final String data) throws Exception, EjsonBuilderException, EjsonParserErrorMulti { final Builder builder = new BuilderGeneric(); final ParseJson parser = new ParseJson(builder); @@ -42,14 +43,16 @@ public class Ejson { property.setDisplayError(true); return (JsonNode) parser.parse(data, property); } - + public static JsonNode parse(final Uri data) throws Exception, EjsonBuilderException, EjsonParserErrorMulti { final Builder builder = new BuilderGeneric(); final ParseJson parser = new ParseJson(builder); final ParsingProperty property = new ParsingProperty(); property.setDisplayError(true); + Log.critical("not implemented..."); return (JsonNode) parser.parse("", property); } - - private Ejson() {} + + private Ejson() { + } } diff --git a/src/org/atriasoft/ejson/annotation/EjsonAnnotation.java b/src/org/atriasoft/ejson/annotation/EjsonAnnotation.java index 21861b2..42e2377 100644 --- a/src/org/atriasoft/ejson/annotation/EjsonAnnotation.java +++ b/src/org/atriasoft/ejson/annotation/EjsonAnnotation.java @@ -12,7 +12,7 @@ import java.lang.annotation.Target; * Ejson annotations generically, and in future also for * passing other generic annotation configuration. */ -@Target({ ElementType.ANNOTATION_TYPE }) +@Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface EjsonAnnotation { // for now, a pure tag annotation, no parameters diff --git a/src/org/atriasoft/ejson/annotation/JsonDefaultManaged.java b/src/org/atriasoft/ejson/annotation/JsonDefaultManaged.java index d2c3587..14587c1 100644 --- a/src/org/atriasoft/ejson/annotation/JsonDefaultManaged.java +++ b/src/org/atriasoft/ejson/annotation/JsonDefaultManaged.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; * Marker annotation that set the element are not managed by default. Need to add @JsonManaged to be enable. * */ -@Target({ ElementType.TYPE }) +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @EjsonAnnotation public @interface JsonDefaultManaged { diff --git a/src/org/atriasoft/ejson/annotation/JsonDefaultOptional.java b/src/org/atriasoft/ejson/annotation/JsonDefaultOptional.java index fc19acc..e7e68d2 100644 --- a/src/org/atriasoft/ejson/annotation/JsonDefaultOptional.java +++ b/src/org/atriasoft/ejson/annotation/JsonDefaultOptional.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; * Marker annotation that set the element not found are ignored. * */ -@Target({ ElementType.TYPE }) +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @EjsonAnnotation public @interface JsonDefaultOptional { diff --git a/src/org/atriasoft/ejson/builder/BuilderGeneric.java b/src/org/atriasoft/ejson/builder/BuilderGeneric.java index 6372e6c..a6e3235 100644 --- a/src/org/atriasoft/ejson/builder/BuilderGeneric.java +++ b/src/org/atriasoft/ejson/builder/BuilderGeneric.java @@ -17,8 +17,7 @@ public class BuilderGeneric implements Builder { @Override public Object newArray(final Object parent) throws EjsonBuilderException { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonArray out = new JsonArray(); elem.add(out); return out; @@ -28,8 +27,7 @@ public class BuilderGeneric implements Builder { @Override public Object newArray(final Object parent, final String nodeName) throws EjsonBuilderException { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonArray out = new JsonArray(); elem.put(nodeName, out); return out; @@ -39,8 +37,7 @@ public class BuilderGeneric implements Builder { @Override public void newBoolean(final Object parent, final boolean value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonBoolean out = new JsonBoolean(value); elem.add(out); return; @@ -51,8 +48,7 @@ public class BuilderGeneric implements Builder { @Override public void newBoolean(final Object parent, final String nodeName, final boolean value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonBoolean out = new JsonBoolean(value); elem.put(nodeName, out); return; @@ -62,8 +58,7 @@ public class BuilderGeneric implements Builder { @Override public void newNull(final Object parent) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonNull out = new JsonNull(); elem.add(out); return; @@ -74,8 +69,7 @@ public class BuilderGeneric implements Builder { @Override public void newNull(final Object parent, final String nodeName) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonNull out = new JsonNull(); elem.put(nodeName, out); return; @@ -85,8 +79,7 @@ public class BuilderGeneric implements Builder { @Override public void newNumber(final Object parent, final double value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonNumber out = new JsonNumber(value); elem.add(out); return; @@ -97,8 +90,7 @@ public class BuilderGeneric implements Builder { @Override public void newNumber(final Object parent, final long value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonNumber out = new JsonNumber(value); elem.add(out); return; @@ -109,8 +101,7 @@ public class BuilderGeneric implements Builder { @Override public void newNumber(final Object parent, final String nodeName, final double value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonNumber out = new JsonNumber(value); elem.put(nodeName, out); return; @@ -120,8 +111,7 @@ public class BuilderGeneric implements Builder { @Override public void newNumber(final Object parent, final String nodeName, final long value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonNumber out = new JsonNumber(value); elem.put(nodeName, out); return; @@ -131,8 +121,7 @@ public class BuilderGeneric implements Builder { @Override public Object newObject(final Object parent) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonObject out = new JsonObject(); elem.add(out); return out; @@ -142,8 +131,7 @@ public class BuilderGeneric implements Builder { @Override public Object newObject(final Object parent, final String nodeName) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonObject out = new JsonObject(); elem.put(nodeName, out); return out; @@ -158,8 +146,7 @@ public class BuilderGeneric implements Builder { @Override public void newString(final Object parent, final String value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonArray) { - final JsonArray elem = (JsonArray) parent; + if (parent instanceof JsonArray elem) { final JsonString out = new JsonString(value); elem.add(out); return; @@ -170,8 +157,7 @@ public class BuilderGeneric implements Builder { @Override public void newString(final Object parent, final String nodeName, final String value) throws EjsonBuilderException, Exception { - if (parent instanceof JsonObject) { - final JsonObject elem = (JsonObject) parent; + if (parent instanceof JsonObject elem) { final JsonString out = new JsonString(value); elem.put(nodeName, out); return; diff --git a/src/org/atriasoft/ejson/exception/EjsonBuilderException.java b/src/org/atriasoft/ejson/exception/EjsonBuilderException.java index cbc7e32..0b82f23 100644 --- a/src/org/atriasoft/ejson/exception/EjsonBuilderException.java +++ b/src/org/atriasoft/ejson/exception/EjsonBuilderException.java @@ -5,10 +5,13 @@ */ package org.atriasoft.ejson.exception; +import java.io.Serial; + public class EjsonBuilderException extends EjsonException { /** * Generate Unique ID for serialization */ + @Serial private static final long serialVersionUID = 1L; public EjsonBuilderException(final String data) { diff --git a/src/org/atriasoft/ejson/exception/EjsonException.java b/src/org/atriasoft/ejson/exception/EjsonException.java index d0f4325..2de9ca7 100644 --- a/src/org/atriasoft/ejson/exception/EjsonException.java +++ b/src/org/atriasoft/ejson/exception/EjsonException.java @@ -5,10 +5,13 @@ */ package org.atriasoft.ejson.exception; +import java.io.Serial; + public class EjsonException extends Exception { /** * Generate Unique ID for serialization */ + @Serial private static final long serialVersionUID = 1L; public EjsonException(final String data) { diff --git a/src/org/atriasoft/ejson/exception/EjsonNodeDoesNotExist.java b/src/org/atriasoft/ejson/exception/EjsonNodeDoesNotExist.java index bebce5d..2a1825d 100644 --- a/src/org/atriasoft/ejson/exception/EjsonNodeDoesNotExist.java +++ b/src/org/atriasoft/ejson/exception/EjsonNodeDoesNotExist.java @@ -5,10 +5,13 @@ */ package org.atriasoft.ejson.exception; +import java.io.Serial; + public class EjsonNodeDoesNotExist extends EjsonException { /** * Generate Unique ID for serialization */ + @Serial private static final long serialVersionUID = 1L; public EjsonNodeDoesNotExist(final String data) { diff --git a/src/org/atriasoft/ejson/exception/EjsonParserError.java b/src/org/atriasoft/ejson/exception/EjsonParserError.java index 2ffcf8c..a95e3b5 100644 --- a/src/org/atriasoft/ejson/exception/EjsonParserError.java +++ b/src/org/atriasoft/ejson/exception/EjsonParserError.java @@ -2,7 +2,10 @@ package org.atriasoft.ejson.exception; import org.atriasoft.ejson.parser.FilePos; +import java.io.Serial; + public class EjsonParserError extends EjsonBuilderException { + @Serial private static final long serialVersionUID = 1L; private final String dataLine; //!< Parse line error (copy); diff --git a/src/org/atriasoft/ejson/exception/EjsonParserErrorMulti.java b/src/org/atriasoft/ejson/exception/EjsonParserErrorMulti.java index fc133ac..225439a 100644 --- a/src/org/atriasoft/ejson/exception/EjsonParserErrorMulti.java +++ b/src/org/atriasoft/ejson/exception/EjsonParserErrorMulti.java @@ -1,8 +1,10 @@ package org.atriasoft.ejson.exception; +import java.io.Serial; import java.util.List; public class EjsonParserErrorMulti extends EjsonBuilderException { + @Serial private static final long serialVersionUID = 1L; private final List errors; // list of errors diff --git a/src/org/atriasoft/ejson/model/JsonArray.java b/src/org/atriasoft/ejson/model/JsonArray.java index 58314d4..02810fa 100644 --- a/src/org/atriasoft/ejson/model/JsonArray.java +++ b/src/org/atriasoft/ejson/model/JsonArray.java @@ -1,4 +1,4 @@ -/** @file +/* @file * @author Edouard DUPIN * @copyright 2021, Edouard DUPIN, all right reserved * @license MPL v2.0 (see license file) @@ -12,11 +12,6 @@ import java.util.List; import org.atriasoft.ejson.exception.EjsonNodeDoesNotExist; import org.atriasoft.ejson.internal.Log; -/** @file - * @author Edouard DUPIN - * @copyright 2011, Edouard DUPIN, all right reserved - * @license MPL v2.0 (see license file) - */ /** * Basic element Node of an XML document lt;YYYYYgt; */ @@ -27,32 +22,31 @@ public class JsonArray extends JsonNode { * Constructor */ public JsonArray() { - super(); - }; + } /** * add a node at the element (not Attribute (move in the attribute automaticly). - * @param[in] _node Pointer of the node to add. + * @param node Pointer of the node to add. */ - public void add(final JsonNode _node) { - if (_node == null) { + public void add(final JsonNode node) { + if (node == null) { Log.error("Try to set an empty node"); return; } - for (int iii = 0; iii < this.listSub.size(); iii++) { - if (this.listSub.get(iii) == _node) { + for (final JsonNode jsonNode : this.listSub) { + if (jsonNode == node) { Log.error("Try to add a node that is already added before !!!"); return; } } - this.listSub.add(_node); + this.listSub.add(node); } @Override public void clear() { super.clear(); this.listSub.clear(); - }; + } @Override public JsonArray clone() throws CloneNotSupportedException { @@ -65,11 +59,11 @@ public class JsonArray extends JsonNode { /** * get the Node pointer of the element id. - * @param[in] _id Id of the element. + * @param id Id of the element. * @return true if the Node exist. */ - public boolean exist(final int _id) { - if (_id < 0 || _id >= this.listSub.size()) { + public boolean exist(final int id) { + if (id < 0 || id >= this.listSub.size()) { return false; } return true; @@ -77,15 +71,15 @@ public class JsonArray extends JsonNode { /** * get the Node pointer of the element id. - * @param[in] _id Id of the element. + * @param id Id of the element. * @return Pointer on node. * @throws EjsonNodeDoesNotExist The Node does not exist */ - public JsonNode get(final int _id) throws EjsonNodeDoesNotExist { - if (_id < 0 || _id >= this.listSub.size()) { - throw new EjsonNodeDoesNotExist("Node does not exist: " + _id + "/" + this.listSub.size()); + public JsonNode get(final int id) throws EjsonNodeDoesNotExist { + if (id < 0 || id >= this.listSub.size()) { + throw new EjsonNodeDoesNotExist("Node does not exist: " + id + "/" + this.listSub.size()); } - return this.listSub.get(_id); + return this.listSub.get(id); } /** @@ -103,7 +97,7 @@ public class JsonArray extends JsonNode { /** * Remove all element with this name - * @param[in] index index of nodes to remove. + * @param index index of nodes to remove. */ public void remove(final int index) { this.listSub.remove(index); @@ -117,4 +111,4 @@ public class JsonArray extends JsonNode { return this.listSub.size(); } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/org/atriasoft/ejson/model/JsonBoolean.java b/src/org/atriasoft/ejson/model/JsonBoolean.java index 27dff78..a360159 100644 --- a/src/org/atriasoft/ejson/model/JsonBoolean.java +++ b/src/org/atriasoft/ejson/model/JsonBoolean.java @@ -15,17 +15,15 @@ public class JsonBoolean extends JsonNode { * Constructor */ public JsonBoolean() { - super(); this.value = false; - }; + } /** * Constructor - * @param[in] _data Value of the boolean + * @param data Value of the boolean */ - public JsonBoolean(final boolean _data) { - super(); - setValue(_data); + public JsonBoolean(final boolean data) { + setValue(data); } @Override @@ -41,4 +39,4 @@ public class JsonBoolean extends JsonNode { this.value = value; } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/org/atriasoft/ejson/model/JsonNumber.java b/src/org/atriasoft/ejson/model/JsonNumber.java index 233cd7f..3ada4e8 100644 --- a/src/org/atriasoft/ejson/model/JsonNumber.java +++ b/src/org/atriasoft/ejson/model/JsonNumber.java @@ -15,68 +15,56 @@ public class JsonNumber extends JsonNode { * Constructor */ public JsonNumber() { - final Long tmp = 0L; - this.value = tmp; - }; - - /** - * Constructor - * @param[in] _data Value of the Number - */ - public JsonNumber(final byte _data) { - super(); - final Long tmp = (long) _data; - this.value = tmp; + this.value = 0L; } /** * Constructor - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public JsonNumber(final double _data) { - super(); - final Double tmp = (double) _data; - this.value = tmp; + public JsonNumber(final byte data) { + this.value = (long) data; } /** * Constructor - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public JsonNumber(final float _data) { - super(); - final Double tmp = (double) _data; - this.value = tmp; + public JsonNumber(final double data) { + this.value = (double) data; } /** * Constructor - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public JsonNumber(final int _data) { - super(); - final Long tmp = (long) _data; - this.value = tmp; + public JsonNumber(final float data) { + this.value = (double) data; } /** * Constructor - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public JsonNumber(final long _data) { - super(); - final Long tmp = _data; - this.value = tmp; + public JsonNumber(final int data) { + this.value = (long) data; } /** * Constructor - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public JsonNumber(final short _data) { - super(); - final Long tmp = (long) _data; - this.value = tmp; + public JsonNumber(final long data) { + final Long tmp = data; + this.value = data; + } + + /** + * Constructor + * @param data Value of the Number + */ + public JsonNumber(final short data) { + this.value = (long) data; } @Override @@ -85,16 +73,15 @@ public class JsonNumber extends JsonNode { } public double getValue() { - if (this.value instanceof Double) { - return (Double) this.value; + if (this.value instanceof Double tmp) { + return tmp; } return (Long) this.value; } public long getValueLong() { - if (this.value instanceof Double) { - final double val = (Double) this.value; - return (long) val; + if (this.value instanceof Double tmp) { + return (long) ((double) tmp); } return (Long) this.value; } @@ -109,56 +96,50 @@ public class JsonNumber extends JsonNode { /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final byte _data) { - final Long tmp = (long) _data; - this.value = tmp; + public void setValue(final byte data) { + this.value = (long) data; } /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final double _data) { - final Double tmp = (double) _data; - this.value = tmp; + public void setValue(final double data) { + this.value = (double) data; } /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final float _data) { - final Double tmp = (double) _data; - this.value = tmp; + public void setValue(final float data) { + this.value = (double) data; } /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final int _data) { - final Long tmp = (long) _data; - this.value = tmp; + public void setValue(final int data) { + this.value = (long) data; } /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final long _data) { - final Long tmp = _data; - this.value = tmp; + public void setValue(final long data) { + this.value = data; } /** * Set the value of the Number element - * @param[in] _data Value of the Number + * @param data Value of the Number */ - public void setValue(final short _data) { - final Long tmp = (long) _data; - this.value = tmp; + public void setValue(final short data) { + this.value = (long) data; } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/org/atriasoft/ejson/model/JsonObject.java b/src/org/atriasoft/ejson/model/JsonObject.java index 9214cd6..aabd970 100644 --- a/src/org/atriasoft/ejson/model/JsonObject.java +++ b/src/org/atriasoft/ejson/model/JsonObject.java @@ -18,17 +18,16 @@ import org.atriasoft.ejson.internal.Log; * @license MPL v2.0 (see license file) */ /** - * Basic element Node of an XML document lt;YYYYYgt; + * Basic element Node of an JSON document <> */ public class JsonObject extends JsonNode { - protected Map listSub = new LinkedHashMap<>(); //!< List of subNodes; + protected Map listSub = new LinkedHashMap<>(); // !< List of subNodes; /** - * Constructor + * Constructor */ public JsonObject() { - super(); - }; + } @Override public void clear() { @@ -49,29 +48,32 @@ public class JsonObject extends JsonNode { } }); return out; - }; - - /** - * get an element with his name (work only with Element) - * @param[in] _name Name of the element that is requested - * @return true if the Node exist. - */ - public boolean exist(final String _name) { - return this.listSub.get(_name) != null; } /** - * get an element with his name (work only with Element) - * @param[in] _name Name of the element that is requested + * get an element with his name (work only with Element) + * + * @param name Name of the element that is requested + * @return true if the Node exist. + */ + public boolean exist(final String name) { + return this.listSub.get(name) != null; + } + + /** + * get an element with his name (work only with Element) + * + * @param name Name of the element that is requested * @return Pointer on the node. * @throws EjsonNodeDoesNotExist The Node does not exist */ - public JsonNode get(final String _name) throws EjsonNodeDoesNotExist { - return this.listSub.get(_name); + public JsonNode get(final String name) throws EjsonNodeDoesNotExist { + return this.listSub.get(name); } /** - * Get the list of the sub-nodes. + * Get the list of the sub-nodes. + * * @return List of current nodes. */ public Map getNodes() { @@ -84,32 +86,36 @@ public class JsonObject extends JsonNode { } /** - * add a node at the element (not Attribute (move in the attribute automaticly). - * @param[in] nodeName Name of the node. - * @param[in] _node Node to add. + * add a node at the element (not Attribute (move in the attribute automaticly). + * + * @param nodeName Name of the node. + * @param node Node to add. */ - public void put(final String nodeName, final JsonNode _node) { - if (_node == null) { + public void put(final String nodeName, final JsonNode node) { + if (node == null) { Log.error("Try to set an empty node"); return; } - this.listSub.put(nodeName, _node); + this.listSub.put(nodeName, node); } /** - * Remove all element with this name - * @param[in] _nodeName Name of nodes to remove. + * Remove all element with this name + * + * @param nodeName Name of nodes to remove. */ public void remove(final String nodeName) { this.listSub.remove(nodeName); } /** - * get the number of sub element in the node (can be Comment ; Element ; Text :Declaration). + * get the number of sub element in the node (can be Comment ; Element ; Text + * :Declaration). + * * @return a number >=0. */ public int size() { return this.listSub.size(); } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/org/atriasoft/ejson/model/JsonString.java b/src/org/atriasoft/ejson/model/JsonString.java index 5807633..5d13724 100644 --- a/src/org/atriasoft/ejson/model/JsonString.java +++ b/src/org/atriasoft/ejson/model/JsonString.java @@ -15,17 +15,15 @@ public class JsonString extends JsonNode { * Constructor */ public JsonString() { - super(); setValue(""); - }; + } /** * Constructor - * @param[in] _data Value of the string + * @param data Value of the string */ - public JsonString(final String _data) { - super(); - setValue(_data); + public JsonString(final String data) { + setValue(data); } @Override @@ -41,4 +39,4 @@ public class JsonString extends JsonNode { this.value = value; } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/org/atriasoft/ejson/parser/FilePos.java b/src/org/atriasoft/ejson/parser/FilePos.java index 5a0c68e..0dc60d2 100644 --- a/src/org/atriasoft/ejson/parser/FilePos.java +++ b/src/org/atriasoft/ejson/parser/FilePos.java @@ -28,48 +28,48 @@ public class FilePos { /** * initialize constructor - * @param[in] _line Line in the file - * @param[in] _col Colomn in the file + * @param line Line in the file + * @param col Colomn in the file */ - public FilePos(final int _line, final int _col) { - this.col = _col; - this.line = _line; + public FilePos(final int line, final int col) { + this.col = col; + this.line = line; } /** * Addition operator - * @param[in] _obj Addition object.. + * @param obj Addition object.. * @return Reference on this */ - public FilePos add(final FilePos _obj) { - if (_obj.line == 0) { - this.col += _obj.col; + public FilePos add(final FilePos obj) { + if (obj.line == 0) { + this.col += obj.col; } else { - this.col = _obj.col; - this.line += _obj.line; + this.col = obj.col; + this.line += obj.line; } return this; } /** * Colomn addition operator - * @param[in] _col Number of colomn to add + * @param col Number of colomn to add * @return Reference on this */ - public FilePos add(final int _col) { - this.col += _col; + public FilePos add(final int col) { + this.col += col; return this; } /** * Check if the value is a new line and update internal property - * @param[in] _val Char value to check + * @param val Char value to check * @return true We find a new line * @return false We NOT find a new line */ - public boolean check(final Character _val) { + public boolean check(final Character val) { this.col++; - if (_val == '\n') { + if (val == '\n') { newLine(); return true; } @@ -153,23 +153,23 @@ public class FilePos { /** * Asignment operator - * @param[in] _obj Object to copy + * @param obj Object to copy * @return Reference on this */ - public FilePos set(final FilePos _obj) { - this.col = _obj.col; - this.line = _obj.line; + public FilePos set(final FilePos obj) { + this.col = obj.col; + this.line = obj.line; return this; } /** * Setter of specific data - * @param[in] _line Line in the file - * @param[in] _col Colomn in the file + * @param line Line in the file + * @param col Colomn in the file */ - public void set(final int _line, final int _col) { - this.col = _col; - this.line = _line; + public void set(final int line, final int col) { + this.col = col; + this.line = line; } @Override @@ -182,4 +182,4 @@ public class FilePos { return out; } -}; +} diff --git a/src/org/atriasoft/ejson/parser/ParseJson.java b/src/org/atriasoft/ejson/parser/ParseJson.java index 8dc2ceb..96c620a 100644 --- a/src/org/atriasoft/ejson/parser/ParseJson.java +++ b/src/org/atriasoft/ejson/parser/ParseJson.java @@ -14,109 +14,109 @@ public class ParseJson { this.builder = builder; } - boolean iParseArray(final Object parent, final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws Exception { - for (int iii = _pos.value + 1; iii < _data.length(); iii++) { - //Log.verbose("parse Array: '" + _data.charAt(iii) + "'"); - _filePos.check(_data.charAt(iii)); - if (_data.charAt(iii) == ' ' || _data.charAt(iii) == '\t' || _data.charAt(iii) == '\n' || _data.charAt(iii) == '\r') { + boolean iParseArray(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws Exception { + for (int iii = pos.value + 1; iii < data.length(); iii++) { + //Log.verbose("parse Array: '" + data.charAt(iii) + "'"); + filePos.check(data.charAt(iii)); + if (data.charAt(iii) == ' ' || data.charAt(iii) == '\t' || data.charAt(iii) == '\n' || data.charAt(iii) == '\r') { // white space == > nothing to do ... - } else if (_data.charAt(iii) == '#') { + } else if (data.charAt(iii) == '#') { // comment Line ... - for (iii++; iii < _data.length(); iii++) { - if (_data.charAt(iii) == '\n' || _data.charAt(iii) == '\r') { + for (iii++; iii < data.length(); iii++) { + if (data.charAt(iii) == '\n' || data.charAt(iii) == '\r') { break; } } - } else if (_data.charAt(iii) == ']') { + } else if (data.charAt(iii) == ']') { // find end of value: - _pos.value = iii; // == > return the end element type ==> usefull to check end and check if adding element is needed + pos.value = iii; // == > return the end element type ==> usefull to check end and check if adding element is needed return true; - } else if (_data.charAt(iii) == '{') { - _pos.value = iii + 1; + } else if (data.charAt(iii) == '{') { + pos.value = iii + 1; // find an object: if (parent == null) { // continue parsing without registering object ... - if (iParseObject(null, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseObject(null, data, pos, filePos, parsingProperty)) { return false; } } else { final Object obj = this.builder.newObject(parent); - if (iParseObject(obj, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseObject(obj, data, pos, filePos, parsingProperty)) { return false; } } - iii = _pos.value; - } else if (_data.charAt(iii) == '"' || _data.charAt(iii) == '\'') { - _pos.value = iii; + iii = pos.value; + } else if (data.charAt(iii) == '"' || data.charAt(iii) == '\'') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final String dataString = iParseString(_data, _pos, _filePos, parsingProperty); + final String dataString = iParseString(data, pos, filePos, parsingProperty); if (dataString == null) { return false; } } else { - final String dataString = iParseString(_data, _pos, _filePos, parsingProperty); + final String dataString = iParseString(data, pos, filePos, parsingProperty); if (dataString == null) { return false; } this.builder.newString(parent, dataString); } - iii = _pos.value; - } else if (_data.charAt(iii) == '[') { - _pos.value = iii + 1; + iii = pos.value; + } else if (data.charAt(iii) == '[') { + pos.value = iii + 1; // find an object: if (parent == null) { // continue parsing without registering object ... - if (iParseArray(null, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseArray(null, data, pos, filePos, parsingProperty)) { return false; } } else { final Object obj = this.builder.newArray(parent); - if (iParseArray(obj, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseArray(obj, data, pos, filePos, parsingProperty)) { return false; } } - iii = _pos.value; - } else if (_data.charAt(iii) == 'f' || _data.charAt(iii) == 't') { - _pos.value = iii; + iii = pos.value; + } else if (data.charAt(iii) == 'f' || data.charAt(iii) == 't') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final Boolean dataBoolean = iParseBoolean(_data, _pos, _filePos, parsingProperty); + final Boolean dataBoolean = iParseBoolean(data, pos, filePos, parsingProperty); if (dataBoolean == null) { return false; } } else { - final Boolean dataBoolean = iParseBoolean(_data, _pos, _filePos, parsingProperty); + final Boolean dataBoolean = iParseBoolean(data, pos, filePos, parsingProperty); if (dataBoolean == null) { return false; } this.builder.newBoolean(parent, dataBoolean); } - iii = _pos.value; - } else if (_data.charAt(iii) == 'n') { - _pos.value = iii; + iii = pos.value; + } else if (data.charAt(iii) == 'n') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - if (iParseNull(_data, _pos, _filePos, parsingProperty) == false) { + if (!iParseNull(data, pos, filePos, parsingProperty)) { return false; } } else { - if (iParseNull(_data, _pos, _filePos, parsingProperty) == false) { + if (!iParseNull(data, pos, filePos, parsingProperty)) { return false; } this.builder.newNull(parent); } - iii = _pos.value; - } else if (true == Tools.checkNumber(_data.charAt(iii))) { - _pos.value = iii; + iii = pos.value; + } else if (Tools.checkNumber(data.charAt(iii))) { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final Object dataNumber = iParseNumber(_data, _pos, _filePos, parsingProperty); + final Object dataNumber = iParseNumber(data, pos, filePos, parsingProperty); if (dataNumber == null) { return false; } } else { - final Object dataNumber = iParseNumber(_data, _pos, _filePos, parsingProperty); + final Object dataNumber = iParseNumber(data, pos, filePos, parsingProperty); if (dataNumber == null) { return false; } @@ -126,297 +126,290 @@ public class ParseJson { this.builder.newNumber(parent, (Long) dataNumber); } } - iii = _pos.value; - } else if (_data.charAt(iii) == ',') { + iii = pos.value; + } else if (data.charAt(iii) == ',') { // find Separator : Restart cycle ... // TODO : check if element are separated with ',' - } else if (_data.charAt(iii) == '}') { + } else if (data.charAt(iii) == '}') { // find an error .... - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, iii), _filePos, "Find '}' with no element in the element... Check if is not a ']' element (to stop array)")); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, iii), filePos, "Find '}' with no element in the element... Check if is not a ']' element (to stop array)")); // move the curent index - _pos.value = iii + 1; + pos.value = iii + 1; return false; } else { // find an error .... - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, iii + 1), _filePos, "Find '" + _data.charAt(iii) + "' with no element in the array...")); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, iii + 1), filePos, "Find '" + data.charAt(iii) + "' with no element in the array...")); // move the curent index - _pos.value = iii + 1; + pos.value = iii + 1; return false; } } - _pos.value = _data.length(); + pos.value = data.length(); return false; } - Boolean iParseBoolean(final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { - if (_data.charAt(_pos.value) == 't' && _pos.value + 3 < _data.length() && _data.charAt(_pos.value + 1) == 'r' && _data.charAt(_pos.value + 2) == 'u' && _data.charAt(_pos.value + 3) == 'e') { - _pos.value += 3; - _filePos.add(3); + Boolean iParseBoolean(final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { + if (data.charAt(pos.value) == 't' && pos.value + 3 < data.length() && data.charAt(pos.value + 1) == 'r' && data.charAt(pos.value + 2) == 'u' && data.charAt(pos.value + 3) == 'e') { + pos.value += 3; + filePos.add(3); return true; } - if (_data.charAt(_pos.value) == 'f' && _pos.value + 4 < _data.length() && _data.charAt(_pos.value + 1) == 'a' && _data.charAt(_pos.value + 2) == 'l' && _data.charAt(_pos.value + 3) == 's' - && _data.charAt(_pos.value + 4) == 'e') { - _pos.value += 4; - _filePos.add(4); + if (data.charAt(pos.value) == 'f' && pos.value + 4 < data.length() && data.charAt(pos.value + 1) == 'a' && data.charAt(pos.value + 2) == 'l' && data.charAt(pos.value + 3) == 's' + && data.charAt(pos.value + 4) == 'e') { + pos.value += 4; + filePos.add(4); return false; } - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, _pos.value), _filePos, "boolean parsing error ...")); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, pos.value), filePos, "boolean parsing error ...")); return null; } - boolean iParseNull(final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { - if (_pos.value + 3 >= _data.length()) { - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, _pos.value), _filePos, "can not parse null !!! ")); + boolean iParseNull(final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { + if (pos.value + 3 >= data.length()) { + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, pos.value), filePos, "can not parse null !!! ")); return false; } - if (_data.charAt(_pos.value) != 'n' || _data.charAt(_pos.value + 1) != 'u' || _data.charAt(_pos.value + 2) != 'l' || _data.charAt(_pos.value + 3) != 'l') { - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, _pos.value), _filePos, "Not a corect 'null' element")); + if (data.charAt(pos.value) != 'n' || data.charAt(pos.value + 1) != 'u' || data.charAt(pos.value + 2) != 'l' || data.charAt(pos.value + 3) != 'l') { + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, pos.value), filePos, "Not a corect 'null' element")); return false; } - _pos.value += 3; - _filePos.add(3); + pos.value += 3; + filePos.add(3); return true; } - Object iParseNumber(final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { + Object iParseNumber(final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { String tmpVal = ""; boolean isDouble = false; - for (int iii = _pos.value; iii < _data.length(); iii++) { - _filePos.check(_data.charAt(iii)); - if (Tools.checkNumber(_data.charAt(iii)) == true) { - if (_data.charAt(iii) == '.' || _data.charAt(iii) == 'e' || _data.charAt(iii) == '^') { - isDouble = true; - } - tmpVal += _data.charAt(iii); - } else { - _pos.value = iii - 1; - if (isDouble == true) { + for (int iii = pos.value; iii < data.length(); iii++) { + filePos.check(data.charAt(iii)); + if (!Tools.checkNumber(data.charAt(iii))) { + pos.value = iii - 1; + if (isDouble) { return Double.valueOf(tmpVal); } else { return Long.valueOf(tmpVal); } } + if (data.charAt(iii) == '.' || data.charAt(iii) == 'e' || data.charAt(iii) == '^') { + isDouble = true; + } + tmpVal += data.charAt(iii); } - _pos.value = _data.length(); - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, _pos.value), _filePos, "get end of string whithout fincding end of quote")); + pos.value = data.length(); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, pos.value), filePos, "get end of string whithout fincding end of quote")); return null; } - boolean iParseObject(final Object parent, final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws Exception { - statusParsing mode = statusParsing.parseName; - String currentName = ""; + boolean iParseObject(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws Exception { + StatusParsing mode = StatusParsing.parseName; + StringBuilder currentName = new StringBuilder(); boolean standalone = true; - int startPos = _pos.value + 1; - if (_data.charAt(_pos.value) != '{') { // when the main node call it, it can be start with != '{' + int startPos = pos.value + 1; + if (data.charAt(pos.value) != '{') { // when the main node call it, it can be start with != '{' standalone = false; - startPos = _pos.value; + startPos = pos.value; } - for (int iii = startPos; iii < _data.length(); iii++) { - //Log.verbose("parse Object: '" + _data.charAt(iii) + "'"); - _filePos.check(_data.charAt(iii)); + for (int iii = startPos; iii < data.length(); iii++) { + //Log.verbose("parse Object: '" + data.charAt(iii) + "'"); + filePos.check(data.charAt(iii)); final FilePos tmpPos; - if (_data.charAt(iii) == ' ' || _data.charAt(iii) == '\t' || _data.charAt(iii) == '\n' || _data.charAt(iii) == '\r') { + if (data.charAt(iii) == ' ' || data.charAt(iii) == '\t' || data.charAt(iii) == '\n' || data.charAt(iii) == '\r') { // white space == > nothing to do ... - } else if (_data.charAt(iii) == '#') { + } else if (data.charAt(iii) == '#') { // comment Line ... - for (iii++; iii < _data.length(); iii++) { - if (_data.charAt(iii) == '\n' || _data.charAt(iii) == '\r') { + for (iii++; iii < data.length(); iii++) { + if (data.charAt(iii) == '\n' || data.charAt(iii) == '\r') { break; } } - } else if (_data.charAt(iii) == '}') { + } else if (data.charAt(iii) == '}') { // find end of value: - _pos.value = iii; // == > return the end element type ==> usefull to check end and check if adding element is needed + pos.value = iii; // == > return the end element type ==> usefull to check end and check if adding element is needed return true; - } else if (mode == statusParsing.parseName) { - if (_data.charAt(iii) == '"' || _data.charAt(iii) == '\'') { - final char startValue = _data.charAt(iii); - currentName = ""; - for (iii++; iii < _data.length(); iii++) { - _filePos.check(_data.charAt(iii)); - if (_data.charAt(iii) == startValue) { - mode = statusParsing.parseMiddle; + } else if (mode == StatusParsing.parseName) { + if (data.charAt(iii) == '"' || data.charAt(iii) == '\'') { + final char startValue = data.charAt(iii); + currentName = new StringBuilder(); + for (iii++; iii < data.length(); iii++) { + filePos.check(data.charAt(iii)); + if (data.charAt(iii) == startValue) { + mode = StatusParsing.parseMiddle; break; - } else { - currentName += _data.charAt(iii); } + currentName.append(data.charAt(iii)); } - } else if (Tools.checkString(_data.charAt(iii))) { - currentName += _data.charAt(iii); - for (iii++; iii < _data.length(); iii++) { - _filePos.check(_data.charAt(iii)); - if (false == Tools.checkString(_data.charAt(iii))) { - mode = statusParsing.parseMiddle; + } else if (Tools.checkString(data.charAt(iii))) { + currentName.append(data.charAt(iii)); + for (iii++; iii < data.length(); iii++) { + filePos.check(data.charAt(iii)); + if (!Tools.checkString(data.charAt(iii))) { + mode = StatusParsing.parseMiddle; iii--; break; - } else { - currentName += _data.charAt(iii); } + currentName.append(data.charAt(iii)); } } else { - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, iii), _filePos, "element unknow ...")); - _pos.value = iii; + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, iii), filePos, "element unknow ...")); + pos.value = iii; return false; } - } else if (mode == statusParsing.parseMiddle) { - if (_data.charAt(iii) == ':') { - mode = statusParsing.parseValue; - } else { - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, iii), _filePos, "separator is not ':'")); + } else if (mode == StatusParsing.parseMiddle) { + if (data.charAt(iii) != ':') { + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, iii), filePos, "separator is not ':'")); return false; } - } else if (mode == statusParsing.parseValue) { - if (_data.charAt(iii) == '{') { - _pos.value = iii + 1; + mode = StatusParsing.parseValue; + } else if (mode == StatusParsing.parseValue) { + if (data.charAt(iii) == '{') { + pos.value = iii + 1; // find an object: if (parent == null) { // continue parsing without registering object ... - if (iParseObject(null, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseObject(null, data, pos, filePos, parsingProperty)) { return false; } } else { - final Object obj = this.builder.newObject(parent, currentName); - if (iParseObject(obj, _data, _pos, _filePos, parsingProperty) == false) { + final Object obj = this.builder.newObject(parent, currentName.toString()); + if (!iParseObject(obj, data, pos, filePos, parsingProperty)) { return false; } } - iii = _pos.value; - currentName = ""; - } else if (_data.charAt(iii) == '"' || _data.charAt(iii) == '\'') { - _pos.value = iii; + iii = pos.value; + currentName = new StringBuilder(); + } else if (data.charAt(iii) == '"' || data.charAt(iii) == '\'') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final String dataString = iParseString(_data, _pos, _filePos, parsingProperty); + final String dataString = iParseString(data, pos, filePos, parsingProperty); if (dataString == null) { return false; } } else { - final String dataString = iParseString(_data, _pos, _filePos, parsingProperty); + final String dataString = iParseString(data, pos, filePos, parsingProperty); if (dataString == null) { return false; } - this.builder.newString(parent, currentName, dataString); + this.builder.newString(parent, currentName.toString(), dataString); } - iii = _pos.value; - currentName = ""; - } else if (_data.charAt(iii) == '[') { - _pos.value = iii + 1; + iii = pos.value; + currentName = new StringBuilder(); + } else if (data.charAt(iii) == '[') { + pos.value = iii + 1; if (parent == null) { // continue parsing without registering object ... - if (iParseArray(null, _data, _pos, _filePos, parsingProperty) == false) { + if (!iParseArray(null, data, pos, filePos, parsingProperty)) { return false; } } else { - final Object obj = this.builder.newArray(parent, currentName); - if (iParseArray(obj, _data, _pos, _filePos, parsingProperty) == false) { + final Object obj = this.builder.newArray(parent, currentName.toString()); + if (!iParseArray(obj, data, pos, filePos, parsingProperty)) { return false; } } - iii = _pos.value; - currentName = ""; - } else if (_data.charAt(iii) == 'f' || _data.charAt(iii) == 't') { - _pos.value = iii; + iii = pos.value; + currentName = new StringBuilder(); + } else if (data.charAt(iii) == 'f' || data.charAt(iii) == 't') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final Boolean dataBoolean = iParseBoolean(_data, _pos, _filePos, parsingProperty); + final Boolean dataBoolean = iParseBoolean(data, pos, filePos, parsingProperty); if (dataBoolean == null) { return false; } } else { - final Boolean dataBoolean = iParseBoolean(_data, _pos, _filePos, parsingProperty); + final Boolean dataBoolean = iParseBoolean(data, pos, filePos, parsingProperty); if (dataBoolean == null) { return false; } - this.builder.newBoolean(parent, currentName, dataBoolean); + this.builder.newBoolean(parent, currentName.toString(), dataBoolean); } - iii = _pos.value; - currentName = ""; - } else if (_data.charAt(iii) == 'n') { - _pos.value = iii; + iii = pos.value; + currentName = new StringBuilder(); + } else if (data.charAt(iii) == 'n') { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - if (iParseNull(_data, _pos, _filePos, parsingProperty) == false) { + if (!iParseNull(data, pos, filePos, parsingProperty)) { return false; } } else { - if (iParseNull(_data, _pos, _filePos, parsingProperty) == false) { + if (!iParseNull(data, pos, filePos, parsingProperty)) { return false; } - this.builder.newNull(parent, currentName); + this.builder.newNull(parent, currentName.toString()); } - iii = _pos.value; - currentName = ""; - } else if (true == Tools.checkNumber(_data.charAt(iii))) { - _pos.value = iii; + iii = pos.value; + currentName = new StringBuilder(); + } else if (Tools.checkNumber(data.charAt(iii))) { + pos.value = iii; if (parent == null) { // continue parsing without registering object ... - final Object dataNumber = iParseNumber(_data, _pos, _filePos, parsingProperty); + final Object dataNumber = iParseNumber(data, pos, filePos, parsingProperty); if (dataNumber == null) { return false; } } else { - final Object dataNumber = iParseNumber(_data, _pos, _filePos, parsingProperty); + final Object dataNumber = iParseNumber(data, pos, filePos, parsingProperty); if (dataNumber == null) { return false; } if (dataNumber instanceof Double) { - this.builder.newNumber(parent, currentName, (Double) dataNumber); + this.builder.newNumber(parent, currentName.toString(), (Double) dataNumber); } else if (dataNumber instanceof Long) { - this.builder.newNumber(parent, currentName, (Long) dataNumber); + this.builder.newNumber(parent, currentName.toString(), (Long) dataNumber); } } - iii = _pos.value; - currentName = ""; - } else if (_data.charAt(iii) == ',') { + iii = pos.value; + currentName = new StringBuilder(); + } else if (data.charAt(iii) == ',') { // find Separator : Restart cycle ... - mode = statusParsing.parseName; - currentName = ""; + mode = StatusParsing.parseName; + currentName = new StringBuilder(); } else { // find an error .... - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, iii), _filePos, "Find '" + _data.charAt(iii) + "' with no element in the element...")); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, iii), filePos, "Find '" + data.charAt(iii) + "' with no element in the element...")); // move the curent index - _pos.value = iii + 1; + pos.value = iii + 1; return false; } } } - _pos.value = _data.length(); - if (standalone == false) { - return true; - } - return false; + pos.value = data.length(); + return !standalone; } - String iParseString(final String _data, final PositionParsing _pos, final FilePos _filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { - final Character end = _data.charAt(_pos.value); + String iParseString(final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws EjsonBuilderException { + final Character end = data.charAt(pos.value); boolean backslashPrevious = false; - String out = ""; - for (int iii = _pos.value + 1; iii < _data.length(); iii++) { - //Log.verbose("parse String: '" + _data.charAt(iii) + "'"); - _filePos.check(_data.charAt(iii)); - if (_data.charAt(iii) == '\\') { - if (backslashPrevious == true) { - out += '\\'; + StringBuilder out = new StringBuilder(); + for (int iii = pos.value + 1; iii < data.length(); iii++) { + //Log.verbose("parse String: '" + data.charAt(iii) + "'"); + filePos.check(data.charAt(iii)); + if (data.charAt(iii) == '\\') { + if (backslashPrevious) { + out.append('\\'); backslashPrevious = false; } else { backslashPrevious = true; } - } else if (_data.charAt(iii) != end) { - if (backslashPrevious == true) { - out += '\\'; + } else if (data.charAt(iii) != end) { + if (backslashPrevious) { + out.append('\\'); backslashPrevious = false; } - out += _data.charAt(iii); - } else if (backslashPrevious == true) { - out += '"'; + out.append(data.charAt(iii)); + } else if (backslashPrevious) { + out.append('"'); backslashPrevious = false; } else { - _pos.value = iii; - return out; + pos.value = iii; + return out.toString(); } } - _pos.value = _data.length(); - parsingProperty.createError(new EjsonParserError(Tools.extractLine(_data, _pos.value), _filePos, "get end of string whithout fincding end of quote")); + pos.value = data.length(); + parsingProperty.createError(new EjsonParserError(Tools.extractLine(data, pos.value), filePos, "get end of string whithout fincding end of quote")); return null; } @@ -429,8 +422,8 @@ public class ParseJson { final Object rootNode = this.builder.newRoot(); iParseObject(rootNode, data, parsePos, pos, property); - if (property.isErrorDetected() == true) { - if (property.isThrowOnError() == true) { + if (property.isErrorDetected()) { + if (property.isThrowOnError()) { throw new EjsonParserErrorMulti("Parsing error multiple error detected", property.getErrors()); } return null; @@ -438,10 +431,8 @@ public class ParseJson { return rootNode; } -}; - -enum statusParsing { - parseName, - parseMiddle, - parseValue, +} + +enum StatusParsing { + parseMiddle, parseName, parseValue, } diff --git a/src/org/atriasoft/ejson/parser/ParsingProperty.java b/src/org/atriasoft/ejson/parser/ParsingProperty.java index 1f8b74f..eb3b420 100644 --- a/src/org/atriasoft/ejson/parser/ParsingProperty.java +++ b/src/org/atriasoft/ejson/parser/ParsingProperty.java @@ -10,16 +10,16 @@ import org.atriasoft.ejson.internal.Log; public class ParsingProperty { /// check the case sensitive of the nodes (end marker) and attribute (duplicates) private boolean caseSensitive = true; - // Mode to store the Element name or the Attibute name - private StoreMode storeMode = StoreMode.NORMAL; - /// write error when not throw on error. - private boolean writeError = false; - /// throw when an error when it is detected (if permissive XML it throw only at the end of parsing). - private boolean throwOnError = true; - /// Permissive XML parsing (allows some errors must not be critical). - private boolean permisiveXML = false; // List of all error detected private final List errors = new ArrayList<>(); + /// Permissive XML parsing (allows some errors must not be critical). + private boolean permisiveXML = false; + // Mode to store the Element name or the Attibute name + private StoreMode storeMode = StoreMode.NORMAL; + /// throw when an error when it is detected (if permissive XML it throw only at the end of parsing). + private boolean throwOnError = true; + /// write error when not throw on error. + private boolean writeError = false; /** * Constructor @@ -48,23 +48,23 @@ public class ParsingProperty { */ public boolean createError(final EjsonParserError error) throws EjsonParserError { // need display the error - if (this.writeError == true) { + if (this.writeError) { displayError(error); } // need throw the error - if (this.throwOnError == true && this.permisiveXML == false) { + if (this.throwOnError && !this.permisiveXML) { throw error; } // Keep it in case this.errors.add(error); - return this.permisiveXML == false; + return !this.permisiveXML; } /** * Request Display in log all the errors. */ public void displayError() { - this.errors.forEach(o -> displayError(o)); + this.errors.forEach(this::displayError); } /** @@ -113,7 +113,7 @@ public class ParsingProperty { * @return true if some error are stored. */ public boolean isErrorDetected() { - return this.errors.isEmpty() == false; + return !this.errors.isEmpty(); } /** @@ -134,18 +134,18 @@ public class ParsingProperty { /** * Enable or diasable the case sensitive (must be done before the call of parsing) - * @param[in] _val true if enable; false else. + * @param val true if enable; false else. */ - public void setCaseSensitive(final boolean _val) { - this.caseSensitive = _val; + public void setCaseSensitive(final boolean val) { + this.caseSensitive = val; } /** * Set the display of the error when detected. - * @param[in] _value true: display error, false not display error (get it at end) + * @param value true: display error, false not display error (get it at end) */ - public void setDisplayError(final boolean _value) { - this.writeError = _value; + public void setDisplayError(final boolean value) { + this.writeError = value; } /** diff --git a/src/org/atriasoft/ejson/parser/Tools.java b/src/org/atriasoft/ejson/parser/Tools.java index 96ade77..039c416 100644 --- a/src/org/atriasoft/ejson/parser/Tools.java +++ b/src/org/atriasoft/ejson/parser/Tools.java @@ -3,49 +3,45 @@ package org.atriasoft.ejson.parser; public class Tools { /** * add indentation of the string input. - * @param[in,out] _data String where the indentation is done. - * @param[in] _indent Number of tab to add at the string. + * @param data String where the indentation is done. + * @param indent Number of tab to add at the string. */ - public static void addIndent(final StringBuilder _data, final int _indent) { - for (int iii = 0; iii < _indent; iii++) { - _data.append("\t"); - } + public static void addIndent(final StringBuilder data, final int indent) { + data.append("\t".repeat(Math.max(0, indent))); } /** * check if an element or attribute is availlable (not : !"#$%&'()*+,/;<=>?@[\]^`{|}~ \\n\\t\\r and for first char : not -.0123456789). - * @param[in] _val Value to check the conformity. - * @param[in] _firstChar True if the element check is the first char. - * @return true The value can be a part of attribute name - * @return false The value can NOT be a part of attribute name + * @param val Value to check the conformity. + * @param firstChar True if the element check is the first char. + * @return true The value can be a part of attribute name / false The value can NOT be a part of attribute name */ - public static boolean checkAvaillable(final Character _val, final boolean _firstChar) { - if (_val == '!' || _val == '"' || _val == '#' || _val == '$' || _val == '%' || _val == '&' || _val == '\'' // ' - || _val == '(' || _val == ')' || _val == '*' || _val == '+' || _val == ',' || _val == '/' || _val == ';' || _val == '<' || _val == '=' || _val == '>' || _val == '?' || _val == '@' - || _val == '[' || _val == '\\' || _val == ']' || _val == '^' || _val == '`' || _val == '{' || _val == '|' || _val == '}' || _val == '~' || _val == ' ' || _val == '\n' || _val == '\t' - || _val == '\r') { + public static boolean checkAvaillable(final Character val, final boolean firstChar) { + if (val == '!' || val == '"' || val == '#' || val == '$' || val == '%' || val == '&' || val == '\'' // ' + || val == '(' || val == ')' || val == '*' || val == '+' || val == ',' || val == '/' || val == ';' || val == '<' || val == '=' || val == '>' || val == '?' || val == '@' || val == '[' + || val == '\\' || val == ']' || val == '^' || val == '`' || val == '{' || val == '|' || val == '}' || val == '~' || val == ' ' || val == '\n' || val == '\t' || val == '\r') { return false; } - if (_firstChar == true) { - if (_val == '-' || _val == '.' || (_val >= '0' && _val <= '9')) { + if (firstChar) { + if (val == '-' || val == '.' || (val >= '0' && val <= '9')) { return false; } } return true; } - public static boolean checkNumber(final Character _val) { - if (_val == '-' || _val == '+' || _val == 'e' || _val == '.' || (_val >= '0' && _val <= '9')) { + public static boolean checkNumber(final Character val) { + if (val == '-' || val == '+' || val == 'e' || val == '.' || (val >= '0' && val <= '9')) { return true; } return false; } - public static boolean checkString(final Character _val) { - if (_val == '!' || _val == '"' || _val == '#' || _val == '$' || _val == '%' || _val == '&' || _val == '\'' // ' - || _val == '(' || _val == ')' || _val == '*' || _val == '+' || _val == ',' || _val == '/' || _val == ':' || _val == ';' || _val == '<' || _val == '=' || _val == '>' || _val == '?' - || _val == '@' || _val == '[' || _val == '\\' || _val == ']' || _val == '^' || _val == '`' || _val == '{' || _val == '|' || _val == '}' || _val == '~' || _val == ' ' || _val == '\n' - || _val == '\t' || _val == '\r') { + public static boolean checkString(final Character val) { + if (val == '!' || val == '"' || val == '#' || val == '$' || val == '%' || val == '&' || val == '\'' // ' + || val == '(' || val == ')' || val == '*' || val == '+' || val == ',' || val == '/' || val == ':' || val == ';' || val == '<' || val == '=' || val == '>' || val == '?' || val == '@' + || val == '[' || val == '\\' || val == ']' || val == '^' || val == '`' || val == '{' || val == '|' || val == '}' || val == '~' || val == ' ' || val == '\n' || val == '\t' + || val == '\r') { return false; } return true; @@ -57,41 +53,40 @@ public class Tools { /** * count the number of white char in the string from the specify position (stop at the first element that is not a white char) - * @param[in] _data Data to parse. - * @param[in] _pos Start position in the string. - * @param[out] _filePos new poistion of te file to add. + * @param data Data to parse. + * @param pos Start position in the string. + * @param filePos new poistion of te file to add. * @return number of white element. */ - public static int countWhiteChar(final String _data, final int _pos, final FilePos _filePos) { - _filePos.clear(); + public static int countWhiteChar(final String data, final int pos, final FilePos filePos) { + filePos.clear(); int white = 0; - for (int iii = _pos; iii < _data.length(); iii++) { - _filePos.check(_data.charAt(iii)); - if (Tools.isWhiteChar(_data.charAt(iii)) == true) { - white++; - } else { + for (int iii = pos; iii < data.length(); iii++) { + filePos.check(data.charAt(iii)); + if (!Tools.isWhiteChar(data.charAt(iii))) { break; } + white++; } - _filePos.decrement(); + filePos.decrement(); return white; } - public static String createPosPointer(final String _line, final int _pos) { - String out = ""; + public static String createPosPointer(final String line, final int pos) { + final StringBuilder out = new StringBuilder(); int iii; - for (iii = 0; iii < _pos && iii < _line.length(); iii++) { - if (_line.charAt(iii) == '\t') { - out += "\t"; + for (iii = 0; iii < pos && iii < line.length(); iii++) { + if (line.charAt(iii) == '\t') { + out.append("\t"); } else { - out += " "; + out.append(" "); } } - for (; iii < _pos; iii++) { - out += " "; + for (; iii < pos; iii++) { + out.append(" "); } - out += "^"; - return out; + out.append("^"); + return out.toString(); } // based on this: https://stackoverflow.com/questions/4052840/most-efficient-way-to-make-the-first-character-of-a-string-lower-case @@ -99,67 +94,61 @@ public class Tools { if (string == null || string.length() == 0) { return string; } - final char c[] = string.toCharArray(); + final char[] c = string.toCharArray(); c[0] = Character.toLowerCase(c[0]); return new String(c); } /** - * Display the cuurent element that is curently parse. - * @param[in] _val Char that is parsed. - * @param[in] _filePos Position of the char in the file. + * Display the current element that is currently parse. + * @param val Char that is parsed. + * @param filePos Position of the char in the file. */ - public static void drawElementParsed(final Character _val, final FilePos _filePos) { - // if (_val == '\n') { - // Log.debug(_filePos + " parse '\\n'"); - // } else if (_val == '\t') { - // Log.debug(_filePos + " parse '\\t'"); + public static void drawElementParsed(final Character val, final FilePos filePos) { + // if (val == '\n') { + // Log.debug(filePos + " parse '\\n'"); + // } else if (val == '\t') { + // Log.debug(filePos + " parse '\\t'"); // } else { - // Log.debug(_filePos + " parse '" + _val + "'"); + // Log.debug(filePos + " parse '" + val + "'"); // } } - public static String extractLine(final String data, final int _pos) { + public static String extractLine(final String data, final int pos) { // search back : '\n' - int startPos = data.lastIndexOf('\n', _pos); - if (startPos == _pos) { + int startPos = data.lastIndexOf('\n', pos); + if (startPos == pos) { startPos = 0; } else { startPos++; } // search forward : '\n' - int stopPos = _pos; - if (data.length() == _pos) { - stopPos = _pos; - } else if (data.charAt(_pos) != '\n') { - stopPos = data.indexOf('\n', _pos); - if (stopPos == _pos) { + int stopPos = pos; + if (data.length() != pos && data.charAt(pos) != '\n') { + stopPos = data.indexOf('\n', pos); + if (stopPos == pos) { stopPos = data.length(); } } - if (startPos == -1) { - startPos = 0; - } else if (startPos >= data.length()) { + if (startPos >= data.length()) { return ""; } if (stopPos == -1) { return ""; - } else if (stopPos >= data.length()) { + } + if (stopPos >= data.length()) { stopPos = data.length(); } return data.substring(startPos, stopPos); } - public static boolean isWhiteChar(final Character _val) { - if (_val == ' ' || _val == '\t' || _val == '\n' || _val == '\r') { - return true; - } - return false; + public static boolean isWhiteChar(final Character val) { + return val == ' ' || val == '\t' || val == '\n' || val == '\r'; } public static Boolean[] parseBooleanClassStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final Boolean[] out = new Boolean[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -169,19 +158,19 @@ public class Tools { } public static boolean[] parseBooleanStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final boolean[] out = new boolean[dataArray.length]; int count = 0; for (final String str : dataArray) { - out[count++] = Boolean.valueOf(str); + out[count++] = Boolean.parseBoolean(str); } return out; } public static Byte[] parseByteClassStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final Byte[] out = new Byte[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -191,8 +180,8 @@ public class Tools { } public static byte[] parseByteStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final byte[] out = new byte[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -202,8 +191,8 @@ public class Tools { } public static Integer[] parseIntegerClassStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final Integer[] out = new Integer[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -213,8 +202,8 @@ public class Tools { } public static int[] parseIntegerStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final int[] out = new int[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -224,8 +213,8 @@ public class Tools { } public static Long[] parseLongClassStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final Long[] out = new Long[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -235,8 +224,8 @@ public class Tools { } public static long[] parseLongStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final long[] out = new long[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -246,8 +235,8 @@ public class Tools { } public static Short[] parseShortClassStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final Short[] out = new Short[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -257,8 +246,8 @@ public class Tools { } public static short[] parseShortStringList(String data) { // throws NumberFormatException - data = cleanNumberList(data); - final String dataArray[] = data.split(";"); + data = Tools.cleanNumberList(data); + final String[] dataArray = data.split(";"); final short[] out = new short[dataArray.length]; int count = 0; for (final String str : dataArray) { @@ -273,25 +262,25 @@ public class Tools { // "&" == "&" // "'" == "'" // """ == """ - public static String replaceSpecialChar(final String _inval) { - final String out = _inval; - out.replace("<", "<"); - out.replace(">", ">"); - out.replace("'", "'"); - out.replace(""", "\""); - out.replace("&", "&"); - //EXML_ERROR("INNN '"<< _inval << "' => '" << out << "'"); + public static String replaceSpecialChar(final String inval) { + String out = inval; + out = out.replace("<", "<"); + out = out.replace(">", ">"); + out = out.replace("'", "'"); + out = out.replace(""", "\""); + out = out.replace("&", "&"); + //EXMLERROR("INNN '"<< inval << "' => '" << out << "'"); return out; } - public static String replaceSpecialCharOut(final String _inval) { - final String out = _inval; - out.replace("<", "<"); - out.replace(">", ">"); - out.replace("'", "'"); - out.replace("\"", """); - out.replace("&", "&"); - //EXML_ERROR("OUTTT '"<< _inval << "' => '" << out << "'"); + public static String replaceSpecialCharOut(final String inval) { + String out = inval; + out = out.replace("<", "<"); + out = out.replace(">", ">"); + out = out.replace("'", "'"); + out = out.replace("\"", """); + out = out.replace("&", "&"); + //EXMLERROR("OUTTT '"<< inval << "' => '" << out << "'"); return out; } diff --git a/src/org/atriasoft/ejson/serializer/SerializerJson.java b/src/org/atriasoft/ejson/serializer/SerializerJson.java index 5693718..7d5bd04 100644 --- a/src/org/atriasoft/ejson/serializer/SerializerJson.java +++ b/src/org/atriasoft/ejson/serializer/SerializerJson.java @@ -14,20 +14,19 @@ import org.atriasoft.ejson.model.JsonString; import org.atriasoft.ejson.parser.Tools; public class SerializerJson { - public static void serialize(final JsonNode node, final StringBuilder data, final int indent) { if (node instanceof JsonObject) { - serializeObject((JsonObject) node, data, indent); + SerializerJson.serializeObject((JsonObject) node, data, indent); } else if (node instanceof JsonArray) { - serializeArray((JsonArray) node, data, indent); + SerializerJson.serializeArray((JsonArray) node, data, indent); } else if (node instanceof JsonNumber) { - serializeNumber((JsonNumber) node, data, indent); + SerializerJson.serializeNumber((JsonNumber) node, data, indent); } else if (node instanceof JsonNull) { - serializeNull((JsonNull) node, data, indent); + SerializerJson.serializeNull((JsonNull) node, data, indent); } else if (node instanceof JsonString) { - serializeString((JsonString) node, data, indent); + SerializerJson.serializeString((JsonString) node, data, indent); } else if (node instanceof JsonBoolean) { - serializeBoolean((JsonBoolean) node, data, indent); + SerializerJson.serializeBoolean((JsonBoolean) node, data, indent); } else { // TODO throw an error ... } @@ -38,14 +37,14 @@ public class SerializerJson { if (indent == -1) { data.append("["); boolean needComa = false; - for (int iii = 0; iii < tmp.size(); iii++) { - if (tmp.get(iii) == null) { + for (final JsonNode jsonNode : tmp) { + if (jsonNode == null) { continue; } - if (needComa == true) { + if (needComa) { data.append(","); } - serialize(tmp.get(iii), data, -1); + SerializerJson.serialize(jsonNode, data, -1); needComa = true; } data.append("]"); @@ -54,8 +53,7 @@ public class SerializerJson { if (tmp.size() > 3) { oneLine = false; } else { - for (int iii = 0; iii < tmp.size(); iii++) { - final JsonNode tmpInspect = tmp.get(iii); + for (final JsonNode tmpInspect : tmp) { if (tmpInspect == null) { continue; } @@ -75,28 +73,28 @@ public class SerializerJson { } } } - if (true == oneLine) { + if (oneLine) { data.append("[ "); } else { data.append("[\n"); } for (int iii = 0; iii < tmp.size(); iii++) { - if (false == oneLine) { + if (!oneLine) { Tools.addIndent(data, indent); } if (tmp.get(iii) != null) { - serialize(tmp.get(iii), data, indent + 1); + SerializerJson.serialize(tmp.get(iii), data, indent + 1); if (iii < tmp.size() - 1) { data.append(","); } } - if (oneLine == true) { + if (oneLine) { data.append(" "); } else { data.append("\n"); } } - if (false == oneLine) { + if (!oneLine) { Tools.addIndent(data, indent - 1); } data.append("]"); @@ -105,7 +103,7 @@ public class SerializerJson { } private static void serializeBoolean(final JsonBoolean node, final StringBuilder data, final int indent) { - if (node.getValue() == true) { + if (node.getValue()) { data.append("true"); } else { data.append("false"); @@ -117,7 +115,7 @@ public class SerializerJson { } private static void serializeNumber(final JsonNumber node, final StringBuilder data, final int indent) { - if (node.isDouble() == true) { + if (node.isDouble()) { data.append(node.getValue()); } else { data.append(node.getValueLong()); @@ -129,17 +127,15 @@ public class SerializerJson { if (indent == -1) { data.append("{"); boolean needComa = false; - final Iterator> iterator = tmp.entrySet().iterator(); - while (iterator.hasNext()) { - final Map.Entry entry = iterator.next(); - if (needComa == true) { + for (final Map.Entry entry : tmp.entrySet()) { + if (needComa) { data.append(","); } needComa = true; data.append("\""); data.append(entry.getKey()); data.append("\":"); - serialize(entry.getValue(), data, -1); + SerializerJson.serialize(entry.getValue(), data, -1); } data.append("}"); } else { @@ -149,9 +145,7 @@ public class SerializerJson { } else if (indent <= 1) { oneLine = false; } else { - final Iterator> iterator = tmp.entrySet().iterator(); - while (iterator.hasNext()) { - final Map.Entry entry = iterator.next(); + for (final Map.Entry entry : tmp.entrySet()) { final JsonNode tmpInstect = entry.getValue(); if (tmpInstect == null) { continue; @@ -172,7 +166,7 @@ public class SerializerJson { } } } - if (oneLine == true) { + if (oneLine) { data.append("{ "); } else { data.append("{\n"); @@ -180,23 +174,23 @@ public class SerializerJson { final Iterator> iterator = tmp.entrySet().iterator(); while (iterator.hasNext()) { final Map.Entry entry = iterator.next(); - if (oneLine == false) { + if (!oneLine) { Tools.addIndent(data, indent); } data.append("\""); data.append(entry.getKey()); data.append("\": "); - serialize(entry.getValue(), data, indent + 1); - if (iterator.hasNext() == true) { + SerializerJson.serialize(entry.getValue(), data, indent + 1); + if (iterator.hasNext()) { data.append(","); } - if (oneLine == true) { + if (oneLine) { data.append(" "); } else { data.append("\n"); } } - if (oneLine == false) { + if (!oneLine) { Tools.addIndent(data, indent - 1); } data.append("}"); @@ -214,4 +208,6 @@ public class SerializerJson { data.append("\""); } + private SerializerJson() {} + } diff --git a/test/src/test/atriasoft/ejson/EjsonLocal.java b/test/src/test/atriasoft/ejson/EjsonLocal.java index 75b385a..b6b5445 100644 --- a/test/src/test/atriasoft/ejson/EjsonLocal.java +++ b/test/src/test/atriasoft/ejson/EjsonLocal.java @@ -12,48 +12,45 @@ import org.atriasoft.ejson.model.JsonNode; import org.junit.jupiter.api.Assertions; class EjsonLocal { - // _errorPos : -1 : no error , 1 : parsing error, 2 generation error, 3 comparaison error ???? - public static void test(final String _ref, final String _input, final int _errorPos) { - //doc.setCaseSensitive(!_caseInSensitive); - Log.verbose("parse : \n" + _input); + // errorPos : -1 : no error , 1 : parsing error, 2 generation error, 3 comparaison error ???? + public static void test(final String ref, final String input, final int errorPos) { + //doc.setCaseSensitive(!caseInSensitive); + Log.verbose("parse : \n" + input); JsonNode root = null; try { - root = Ejson.parse(_input); - if (_errorPos == 1) { + root = Ejson.parse(input); + if (errorPos == 1) { Assertions.fail("Must have detected an error"); return; } } catch (final EjsonParserErrorMulti e) { - if (_errorPos == 1) { + if (errorPos == 1) { return; - } else { - e.printStackTrace(); - Assertions.fail("Must have NOT detected an error " + e.getMessage()); } + e.printStackTrace(); + Assertions.fail("Must have NOT detected an error " + e.getMessage()); } catch (final EjsonBuilderException e) { - if (_errorPos == 1) { + if (errorPos == 1) { return; - } else { - e.printStackTrace(); - Assertions.fail("Must have NOT detected an error " + e.getMessage()); } + e.printStackTrace(); + Assertions.fail("Must have NOT detected an error " + e.getMessage()); } catch (final Exception e) { - if (_errorPos == 1) { + if (errorPos == 1) { return; - } else { - e.printStackTrace(); - Assertions.fail("Must have NOT detected an error " + e.getMessage()); } + e.printStackTrace(); + Assertions.fail("Must have NOT detected an error " + e.getMessage()); } final StringBuilder out = new StringBuilder(); - // TODO: 2 is for failing in generate ... + // TODO 2 is for failing in generate ... Ejson.generate(root, out); final String data = out.toString(); - if (_errorPos == 3) { - Assertions.assertNotEquals(_ref, data); - return; - } else { - Assertions.assertEquals(_ref, data); + if (errorPos == 3) { + Assertions.assertNotEquals(ref, data); } + Assertions.assertEquals(ref, data); } + + private EjsonLocal() {} } diff --git a/test/src/test/atriasoft/ejson/EjsonTestBoolean.java b/test/src/test/atriasoft/ejson/EjsonTestBoolean.java index be642f8..81127dc 100644 --- a/test/src/test/atriasoft/ejson/EjsonTestBoolean.java +++ b/test/src/test/atriasoft/ejson/EjsonTestBoolean.java @@ -10,9 +10,9 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; public class EjsonTestBoolean { - static private String refOutputBoolean1 = "{\n\t\"tmpElement\": true\n}"; + private static String refOutputBoolean1 = "{\n\t\"tmpElement\": true\n}"; - static private String refOutputBoolean2 = "{\n\t\"tmpElement\": false\n}"; + private static String refOutputBoolean2 = "{\n\t\"tmpElement\": false\n}"; @BeforeAll public static void beforeClass() { @@ -28,60 +28,60 @@ public class EjsonTestBoolean { @Test @Order(2) public void test010BaseTrue() { - EjsonLocal.test(refOutputBoolean1, "{ tmpElement:true }\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean1, "{ tmpElement:true }\n", -1); } @Test @Order(3) public void test020TabbedTrue() { - EjsonLocal.test(refOutputBoolean1, "{ \t\ntmpElement:true \t\n }\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean1, "{ \t\ntmpElement:true \t\n }\n", -1); } @Test @Order(4) public void test030NoneTrue() { - EjsonLocal.test(refOutputBoolean1, "tmpElement:true\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean1, "tmpElement:true\n", -1); } @Test @Order(5) public void test040BaseTrue1() { - EjsonLocal.test(refOutputBoolean1, "{ tmpElement:TRUE }\n", 1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean1, "{ tmpElement:TRUE }\n", 1); } @Test @Order(6) public void test050BaseTrue2() { - EjsonLocal.test(refOutputBoolean1, "{ tmpElement:True }\n", 1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean1, "{ tmpElement:True }\n", 1); } @Test @Order(7) public void test110BaseFalse() { - EjsonLocal.test(refOutputBoolean2, "{ tmpElement:false }\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean2, "{ tmpElement:false }\n", -1); } @Test @Order(8) public void test120TabbedFalse() { - EjsonLocal.test(refOutputBoolean2, "{ \t\ntmpElement:false \t\n }\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean2, "{ \t\ntmpElement:false \t\n }\n", -1); } @Test @Order(9) public void test130NoneFalse() { - EjsonLocal.test(refOutputBoolean2, "tmpElement:false\n", -1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean2, "tmpElement:false\n", -1); } @Test @Order(10) public void test140BaseFalse1() { - EjsonLocal.test(refOutputBoolean2, "{ tmpElement:FALSE }\n", 1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean2, "{ tmpElement:FALSE }\n", 1); } @Test @Order(11) public void test150BaseFalse2() { - EjsonLocal.test(refOutputBoolean2, "{ tmpElement:False }\n", 1); + EjsonLocal.test(EjsonTestBoolean.refOutputBoolean2, "{ tmpElement:False }\n", 1); } } diff --git a/test/src/test/atriasoft/ejson/EjsonTestNull.java b/test/src/test/atriasoft/ejson/EjsonTestNull.java index 84a9102..f2638b3 100644 --- a/test/src/test/atriasoft/ejson/EjsonTestNull.java +++ b/test/src/test/atriasoft/ejson/EjsonTestNull.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; public class EjsonTestNull { - static private String refOutputNull = "{\n\t\"tmpElement\": null\n}"; + private static final String REF_OUTPUT_NULL = "{\n\t\"tmpElement\": null\n}"; @BeforeAll public static void beforeClass() { @@ -20,19 +20,19 @@ public class EjsonTestNull { @Test @Order(1) public void test10BasicNullElement() { - EjsonLocal.test(refOutputNull, "{ tmpElement:null }\n", -1); + EjsonLocal.test(EjsonTestNull.REF_OUTPUT_NULL, "{ tmpElement:null }\n", -1); } @Test @Order(2) public void test20TabbedNullElement() { - EjsonLocal.test(refOutputNull, "{ \t\ntmpElement:null \t\n }\n", -1); + EjsonLocal.test(EjsonTestNull.REF_OUTPUT_NULL, "{ \t\ntmpElement:null \t\n }\n", -1); } @Test @Order(3) public void test30TabbedNullElementNoPThese() { - EjsonLocal.test(refOutputNull, "tmpElement:null\n", -1); + EjsonLocal.test(EjsonTestNull.REF_OUTPUT_NULL, "tmpElement:null\n", -1); } @Test diff --git a/test/src/test/atriasoft/ejson/EjsonTestNumber.java b/test/src/test/atriasoft/ejson/EjsonTestNumber.java index 1625fd4..f2511cf 100644 --- a/test/src/test/atriasoft/ejson/EjsonTestNumber.java +++ b/test/src/test/atriasoft/ejson/EjsonTestNumber.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; public class EjsonTestNumber { - static private String refOutputNumber = "{\n\t\"tmpElement\": 956256\n}"; + private static final String REF_OUTPUT_NUMBER = "{\n\t\"tmpElement\": 956256\n}"; @BeforeAll public static void beforeClass() { @@ -20,13 +20,13 @@ public class EjsonTestNumber { @Test @Order(1) public void test10Base() { - EjsonLocal.test(refOutputNumber, "{ tmpElement:956256 }\n", -1); + EjsonLocal.test(EjsonTestNumber.REF_OUTPUT_NUMBER, "{ tmpElement:956256 }\n", -1); } @Test @Order(2) public void test20Tabbed() { - EjsonLocal.test(refOutputNumber, "{ \t\ntmpElement:956256 \t\n }\n", -1); + EjsonLocal.test(EjsonTestNumber.REF_OUTPUT_NUMBER, "{ \t\ntmpElement:956256 \t\n }\n", -1); } @Test @@ -44,6 +44,6 @@ public class EjsonTestNumber { @Test @Order(5) public void test50None() { - EjsonLocal.test(refOutputNumber, "tmpElement:956256\n", -1); + EjsonLocal.test(EjsonTestNumber.REF_OUTPUT_NUMBER, "tmpElement:956256\n", -1); } }