You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2015/11/23 16:56:44 UTC
svn commit: r1715857 - in /chemistry/opencmis/trunk:
chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/
chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/ma...
Author: fmui
Date: Mon Nov 23 15:56:44 2015
New Revision: 1715857
URL: http://svn.apache.org/viewvc?rev=1715857&view=rev
Log:
some JSON parser and writer improvements
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/AbstractBrowserBindingService.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONArray.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONObject.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONValue.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/JSONParseException.java
chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/Yylex.java
Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/AbstractBrowserBindingService.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/AbstractBrowserBindingService.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/AbstractBrowserBindingService.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/AbstractBrowserBindingService.java Mon Nov 23 15:56:44 2015
@@ -365,6 +365,8 @@ public abstract class AbstractBrowserBin
reader = new InputStreamReader(stream, charset);
JSONParser parser = new JSONParser();
obj = parser.parse(reader, containerFactory);
+ } catch (JSONParseException e) {
+ throw new CmisConnectionException("Parsing exception: " + e.getMessage(), e);
} catch (Exception e) {
throw new CmisConnectionException("Parsing exception!", e);
} finally {
Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONArray.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONArray.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONArray.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONArray.java Mon Nov 23 15:56:44 2015
@@ -90,8 +90,18 @@ public class JSONArray extends ArrayList
return "null";
}
- boolean first = true;
StringBuilder sb = new StringBuilder(1024);
+ addJSONString(list, sb);
+ return sb.toString();
+ }
+
+ public static void addJSONString(List<Object> list, StringBuilder sb) {
+ if (list == null) {
+ sb.append("null");
+ return;
+ }
+
+ boolean first = true;
sb.append('[');
for (Object value : list) {
@@ -108,7 +118,6 @@ public class JSONArray extends ArrayList
sb.append(JSONValue.toJSONString(value));
}
sb.append(']');
- return sb.toString();
}
@Override
Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONObject.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONObject.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONObject.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONObject.java Mon Nov 23 15:56:44 2015
@@ -32,14 +32,15 @@ import java.util.Map;
*
* @author FangYidong<fa...@yahoo.com.cn>
*/
-public class JSONObject extends LinkedHashMap<String, Object> implements Map<String, Object>, JSONAware, JSONStreamAware {
+public class JSONObject extends LinkedHashMap<String, Object> implements Map<String, Object>, JSONAware,
+ JSONStreamAware {
private static final long serialVersionUID = 1;
@Override
public Object put(String key, Object value) {
if (key == null) {
- throw new NullPointerException("JSON key must not be null!");
+ throw new IllegalArgumentException("JSON key must not be null!");
}
return super.put(key, value);
@@ -105,6 +106,16 @@ public class JSONObject extends LinkedHa
}
StringBuilder sb = new StringBuilder(1024);
+ addJSONString(map, sb);
+ return sb.toString();
+ }
+
+ public static void addJSONString(Map<String, Object> map, StringBuilder sb) {
+ if (map == null) {
+ sb.append("null");
+ return;
+ }
+
boolean first = true;
sb.append('{');
@@ -115,11 +126,9 @@ public class JSONObject extends LinkedHa
sb.append(',');
}
- toJSONString(entry.getKey(), entry.getValue(), sb);
+ addJSONString(entry.getKey(), entry.getValue(), sb);
}
sb.append('}');
-
- return sb.toString();
}
@Override
@@ -127,7 +136,7 @@ public class JSONObject extends LinkedHa
return toJSONString(this);
}
- private static String toJSONString(String key, Object value, StringBuilder sb) {
+ private static void addJSONString(String key, Object value, StringBuilder sb) {
sb.append('\"');
if (key == null) {
sb.append("null");
@@ -138,8 +147,6 @@ public class JSONObject extends LinkedHa
sb.append('\"').append(':');
sb.append(JSONValue.toJSONString(value));
-
- return sb.toString();
}
@Override
@@ -149,7 +156,7 @@ public class JSONObject extends LinkedHa
public static String toString(String key, Object value) {
StringBuilder sb = new StringBuilder(1024);
- toJSONString(key, value, sb);
+ addJSONString(key, value, sb);
return sb.toString();
}
Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONValue.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONValue.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONValue.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/JSONValue.java Mon Nov 23 15:56:44 2015
@@ -178,7 +178,7 @@ public final class JSONValue {
}
/**
- * Convert an object to JSON text.
+ * Converts an object to JSON text.
* <p>
* If this object is a Map or a List, and it's also a JSONAware, JSONAware
* will be considered firstly.
@@ -194,57 +194,81 @@ public final class JSONValue {
* @return JSON text, or "null" if value is null or it's an NaN or an INF
* number.
*/
- @SuppressWarnings("unchecked")
public static String toJSONString(Object value) {
+ StringBuilder sb = new StringBuilder(1024);
+ addJSONString(value, sb);
+ return sb.toString();
+ }
+
+ /**
+ * Converts an object to JSON text and attach it the the given
+ * StringBuilder.
+ *
+ * @see #toJSONString(Object)
+ */
+ @SuppressWarnings("unchecked")
+ public static void addJSONString(Object value, StringBuilder sb) {
if (value == null) {
- return "null";
+ sb.append("null");
+ return;
}
if (value instanceof String) {
- return "\"" + escape((String) value) + "\"";
+ sb.append('\"');
+ escape((String) value, sb);
+ sb.append('\"');
+ return;
}
if (value instanceof Double) {
if (((Double) value).isInfinite() || ((Double) value).isNaN()) {
- return "null";
+ sb.append("null");
} else {
- return value.toString();
+ sb.append(value.toString());
}
+ return;
}
if (value instanceof Float) {
if (((Float) value).isInfinite() || ((Float) value).isNaN()) {
- return "null";
+ sb.append("null");
} else {
- return value.toString();
+ sb.append(value.toString());
}
+ return;
}
if (value instanceof BigDecimal) {
- return ((BigDecimal) value).toPlainString();
+ sb.append(((BigDecimal) value).toPlainString());
+ return;
}
if (value instanceof Number) {
- return value.toString();
+ sb.append(value.toString());
+ return;
}
if (value instanceof Boolean) {
- return value.toString();
+ sb.append(value.toString());
+ return;
}
if (value instanceof JSONAware) {
- return ((JSONAware) value).toJSONString();
+ sb.append(((JSONAware) value).toJSONString());
+ return;
}
if (value instanceof Map) {
- return JSONObject.toJSONString((Map<String, Object>) value);
+ JSONObject.addJSONString((Map<String, Object>) value, sb);
+ return;
}
if (value instanceof List) {
- return JSONArray.toJSONString((List<Object>) value);
+ JSONArray.addJSONString((List<Object>) value, sb);
+ return;
}
- return value.toString();
+ sb.append(value.toString());
}
/**
@@ -270,7 +294,8 @@ public final class JSONValue {
* @param sb
*/
static void escape(String s, StringBuilder sb) {
- for (int i = 0; i < s.length(); i++) {
+ final int n = s.length();
+ for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
switch (ch) {
case '"':
Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/JSONParseException.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/JSONParseException.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/JSONParseException.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/JSONParseException.java Mon Nov 23 15:56:44 2015
@@ -46,11 +46,37 @@ public class JSONParseException extends
}
public JSONParseException(int position, int errorType, Object unexpectedObject) {
+ super();
+
this.position = position;
this.errorType = errorType;
this.unexpectedObject = unexpectedObject;
}
+ @Override
+ public String getMessage() {
+ StringBuilder sb = new StringBuilder(128);
+
+ switch (errorType) {
+ case ERROR_UNEXPECTED_CHAR:
+ sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position)
+ .append('.');
+ break;
+ case ERROR_UNEXPECTED_TOKEN:
+ sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position)
+ .append('.');
+ break;
+ case ERROR_UNEXPECTED_EXCEPTION:
+ sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject);
+ break;
+ default:
+ sb.append("Unkown error at position ").append(position).append('.');
+ break;
+ }
+
+ return sb.toString();
+ }
+
public int getErrorType() {
return errorType;
}
@@ -91,24 +117,6 @@ public class JSONParseException extends
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(128);
-
- switch (errorType) {
- case ERROR_UNEXPECTED_CHAR:
- sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position)
- .append('.');
- break;
- case ERROR_UNEXPECTED_TOKEN:
- sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position)
- .append('.');
- break;
- case ERROR_UNEXPECTED_EXCEPTION:
- sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject);
- break;
- default:
- sb.append("Unkown error at position ").append(position).append('.');
- break;
- }
- return sb.toString();
+ return getMessage();
}
}
Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/Yylex.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/Yylex.java?rev=1715857&r1=1715856&r2=1715857&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/Yylex.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/json/parser/Yylex.java Mon Nov 23 15:56:44 2015
@@ -519,7 +519,7 @@ class Yylex {
case 25:
break;
case 4: {
- sb.delete(0, sb.length());
+ sb.setLength(0);
yybegin(STRING_BEGIN);
break;
}