Author: rwhitcomb Date: Thu Apr 5 15:41:46 2018 New Revision: 1828450 URL: http://svn.apache.org/viewvc?rev=1828450&view=rev Log: PIVOT-960: Add an attribute to JSONSerializer as to whether to allow macros (which are a non-standard feature) to the processing. Not allowing macros also should speed up the process. Note: the default is still "true" so that the existing behavior is maintained.
Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java?rev=1828450&r1=1828449&r2=1828450&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java (original) +++ pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Thu Apr 5 15:41:46 2018 @@ -59,6 +59,7 @@ public class JSONSerializer implements S private boolean alwaysDelimitMapKeys = false; private boolean verbose = false; + private boolean macros = true; private int c = -1; @@ -112,7 +113,7 @@ public class JSONSerializer implements S * Returns a flag indicating whether or not map keys will always be * quote-delimited. * <p> Note: the JSON "standard" requires keys to be delimited. - * @return <tt>true</tt> if map keys must always be delmited (that is, + * @return <tt>true</tt> if map keys must always be delimited (that is, * enclosed in double quotes), <tt>false</tt> for the default behavior * that does not require double quotes. */ @@ -149,6 +150,27 @@ public class JSONSerializer implements S } /** + * Returns the flag indicating whether this serializer will allow macros in the text. + * @return The "macros allowed" flag. + */ + public boolean getAllowMacros() { + return macros; + } + + /** + * Sets the flag indicating whether macros are allowed in the text. This is definitely + * a non-standard feature. See the documentation in {@link MacroReader} for more details + * on the specification of macros. + * <p> Note: must be called before {@link #readObject} is called. + * @param macros Flag indicating whether macros are allowed (default is {@code true}). + * Setting the flag to false will maintain standards-compliant behavior and possibly + * speed up operation as well. + */ + public void setAllowMacros(boolean macros) { + this.macros = macros; + } + + /** * Reads data from a JSON stream. * * @param inputStream The input stream from which data will be read. @@ -189,20 +211,23 @@ public class JSONSerializer implements S public Object readObject(Reader reader) throws IOException, SerializationException { Utils.checkNull(reader, "reader"); - // Move to the first character LineNumberReader lineNumberReader = new LineNumberReader(reader); - MacroReader macroReader = new MacroReader(lineNumberReader); - c = macroReader.read(); + Reader realReader = lineNumberReader; + if (macros) { + realReader = new MacroReader(realReader); + } + // Move to the first character + c = realReader.read(); // Ignore BOM (if present) if (c == 0xFEFF) { - c = macroReader.read(); + c = realReader.read(); } // Read the root value Object object; try { - object = readValue(macroReader, type, type.getTypeName()); + object = readValue(realReader, type, type.getTypeName()); } catch (SerializationException exception) { System.err.println("An error occurred while processing input at line number " + (lineNumberReader.getLineNumber() + 1));