You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/08/30 18:17:54 UTC
svn commit: r1519014 - in
/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json:
JsonNumber.java io/JSWriter.java
Author: andy
Date: Fri Aug 30 16:17:54 2013
New Revision: 1519014
URL: http://svn.apache.org/r1519014
Log:
Print Numbers, the interval value of a JsonNumber.
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonNumber.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonNumber.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonNumber.java?rev=1519014&r1=1519013&r2=1519014&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonNumber.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/JsonNumber.java Fri Aug 30 16:17:54 2013
@@ -23,19 +23,13 @@ import java.math.BigDecimal ;
public class JsonNumber extends JsonPrimitive
{
public static JsonNumber valueDecimal(String image)
- {
- return new JsonNumber(image) ;
- }
+ { return new JsonNumber(image) ; }
public static JsonNumber valueDouble(String image)
- {
- return new JsonNumber(image) ;
- }
+ { return new JsonNumber(image) ; }
public static JsonNumber valueInteger(String image)
- {
- return new JsonNumber(image) ;
- }
+ { return new JsonNumber(image) ; }
public static JsonNumber value(long number)
{ return new JsonNumber(number) ; }
@@ -50,7 +44,6 @@ public class JsonNumber extends JsonPrim
private JsonNumber(String string) { this.number = new BigDecimal(string) ; }
-
private JsonNumber(long number) { this.number = BigDecimal.valueOf(number) ; }
private JsonNumber(double number) { this.number = BigDecimal.valueOf(number) ; }
private JsonNumber(BigDecimal number) { this.number = number ; }
@@ -60,7 +53,7 @@ public class JsonNumber extends JsonPrim
@Override
public boolean isNumber() { return true ; }
@Override
- public JsonNumber getAsNumber() { return this ; }
+ public JsonNumber getAsNumber() { return this ; }
public Number value() { return this.number ; }
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java?rev=1519014&r1=1519013&r2=1519014&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/json/io/JSWriter.java Fri Aug 30 16:17:54 2013
@@ -16,14 +16,14 @@
* limitations under the License.
*/
-package org.apache.jena.atlas.json.io;
-
+package org.apache.jena.atlas.json.io ;
import static org.apache.jena.atlas.lib.Chars.CH_QUOTE1 ;
import static org.apache.jena.atlas.lib.Chars.CH_QUOTE2 ;
import static org.apache.jena.atlas.lib.Chars.CH_ZERO ;
import java.io.OutputStream ;
+import java.math.BigDecimal ;
import java.util.ArrayDeque ;
import java.util.Deque ;
@@ -33,177 +33,167 @@ import org.apache.jena.atlas.lib.BitsInt
import org.apache.jena.atlas.lib.Chars ;
import org.apache.jena.atlas.lib.Ref ;
-
-/** A low level streaming JSON writer - assumes correct sequence of calls (e.g. keys in objects).
- * Useful when writing JSON directly from some other structure
+/**
+ * A low level streaming JSON writer - assumes correct sequence of calls (e.g.
+ * keys in objects). Useful when writing JSON directly from some other structure
*/
-public class JSWriter
-{
- private IndentedWriter out = IndentedWriter.stdout ;
-
- public JSWriter() { this(IndentedWriter.stdout) ; }
+public class JSWriter {
+ protected IndentedWriter out = IndentedWriter.stdout ;
+
+ public JSWriter() {
+ this(IndentedWriter.stdout) ;
+ }
+
public JSWriter(OutputStream ps) { this(new IndentedWriter(ps)) ; }
+
public JSWriter(IndentedWriter ps) { out = ps ; }
-
+
public void startOutput() {}
- public void finishOutput() { out.flush(); }
-
- // These apply in nested and flat modes (the difference is controlled by the IndentedWriter
-
- private static String ArrayStart = "[ " ;
- private static String ArrayFinish = " ]" ;
- private static String ArraySep = ", " ;
-
- private static String ObjectStart = "{ " ;
- private static String ObjectFinish = "}" ;
- private static String ObjectSep = " ," ;
- private static String ObjectPairSep = " : " ;
-
- // Remember whether we are in the first element of a compound (object or array).
+
+ public void finishOutput() { out.flush() ; }
+
+ // These apply in nested and flat modes (the difference is controlled by the
+ // IndentedWriter
+
+ public static final String ArrayStart = "[ " ;
+ public static final String ArrayFinish = " ]" ;
+ public static final String ArraySep = ", " ;
+
+ public static final String ObjectStart = "{ " ;
+ public static final String ObjectFinish = "}" ;
+ public static final String ObjectSep = " ," ;
+ public static final String ObjectPairSep = " : " ;
+
+ // Remember whether we are in the first element of a compound
+ // (object or array).
Deque<Ref<Boolean>> stack = new ArrayDeque<Ref<Boolean>>() ;
-
- public void startObject()
- {
+
+ public void startObject() {
startCompound() ;
out.print(ObjectStart) ;
out.incIndent() ;
}
-
- public void finishObject()
- {
+
+ public void finishObject() {
out.decIndent() ;
if ( isFirst() )
out.print(ObjectFinish) ;
- else
- {
+ else {
out.ensureStartOfLine() ;
out.println(ObjectFinish) ;
}
finishCompound() ;
}
-
- public void key(String key)
- {
- if ( isFirst() )
- {
- out.println();
+
+ public void key(String key) {
+ if ( isFirst() ) {
+ out.println() ;
setNotFirst() ;
- }
- else
+ } else
out.println(ObjectSep) ;
value(key) ;
out.print(ObjectPairSep) ;
// Ready to start the pair value.
}
-
- // "Pair" is the name used in the JSON spec.
- public void pair(String key, String value)
- {
+
+ // "Pair" is the name used in the JSON spec.
+ public void pair(String key, String value) {
key(key) ;
value(value) ;
}
-
-
- public void pair(String key, boolean val)
- {
+
+ public void pair(String key, boolean val) {
key(key) ;
value(val) ;
}
- public void pair(String key, long val)
- {
+ public void pair(String key, long val) {
key(key) ;
value(val) ;
}
- public void startArray()
- {
+ public void startArray() {
startCompound() ;
out.print(ArrayStart) ;
// Messy with objects out.incIndent() ;
}
-
- public void finishArray()
- {
-// out.decIndent() ;
- out.print(ArrayFinish) ; // Leave on same line.
+
+ public void finishArray() {
+ // out.decIndent() ;
+ out.print(ArrayFinish) ; // Leave on same line.
finishCompound() ;
}
- public void arrayElement(String str)
- {
+ public void arrayElement(String str) {
arrayElementProcess() ;
value(str) ;
}
- private void arrayElementProcess()
- {
+ private void arrayElementProcess() {
if ( isFirst() )
setNotFirst() ;
else
out.print(ArraySep) ;
}
-
- public void arrayElement(boolean b)
- {
+
+ public void arrayElement(boolean b) {
arrayElementProcess() ;
value(b) ;
}
- public void arrayElement(long integer)
- {
+ public void arrayElement(long integer) {
arrayElementProcess() ;
value(integer) ;
}
-
+
/**
- * Useful if you are manually creating arrays and so need to print array separators yourself
+ * Useful if you are manually creating arrays and so need to print array
+ * separators yourself
*/
- public void arraySep()
- {
- out.print(ArraySep);
+ public void arraySep() {
+ out.print(ArraySep) ;
}
-
- public static String outputQuotedString(String string)
- {
+
+ public static String outputQuotedString(String string) {
IndentedLineBuffer b = new IndentedLineBuffer() ;
outputQuotedString(b, string) ;
return b.asString() ;
}
-
- private static boolean writeJavaScript = false ;
-
- /* \" \\ \/ \b \f \n \r \t
- * control characters (def?)
- * \ u four-hex-digits
- * (if you don't know why the comment writes "\ u",
- * and not without space then ... ) */
- public static void outputQuotedString(IndentedWriter out, String string)
- {
- final boolean allowBareWords = writeJavaScript ;
-
+
+ /*
+ * Output a JSON string with escaping.
+ * \" \\ \/ \b \f \n \r \t control
+ * characters (def?) \ u four-hex-digits
+ */
+ public static void outputQuotedString(IndentedWriter out, String string) {
+ outputQuotedString(out, string, false) ;
+ }
+
+ /*
+ * Output a JSON string with escaping. Optionally allow base words (unquoted
+ * strings) which, for member names, is legal javascript but not legal JSON.
+ * The Jena JSON parser accepts them.
+ */
+ public static void outputQuotedString(IndentedWriter out, String string, boolean allowBareWords) {
char quoteChar = CH_QUOTE2 ;
int len = string.length() ;
-
- if ( allowBareWords )
- {
+
+ if ( allowBareWords ) {
boolean safeBareWord = true ;
if ( len != 0 )
safeBareWord = isA2Z(string.charAt(0)) ;
- if ( safeBareWord )
- {
- for (int i = 1; i < len; i++)
- {
- char ch = string.charAt(i);
- if ( isA2ZN(ch) ) continue ;
+ if ( safeBareWord ) {
+ for (int i = 1; i < len; i++) {
+ char ch = string.charAt(i) ;
+ if ( isA2ZN(ch) )
+ continue ;
safeBareWord = false ;
break ;
}
}
- if ( safeBareWord )
- {
+ if ( safeBareWord ) {
// It's safe as a bare word in JavaScript.
out.print(string) ;
return ;
@@ -212,48 +202,56 @@ public class JSWriter
if ( allowBareWords )
quoteChar = CH_QUOTE1 ;
-
+
out.print(quoteChar) ;
- for (int i = 0; i < len; i++)
- {
- char ch = string.charAt(i);
- if ( ch == quoteChar )
- {
+ for (int i = 0; i < len; i++) {
+ char ch = string.charAt(i) ;
+ if ( ch == quoteChar ) {
esc(out, quoteChar) ;
continue ;
}
-
-
-
- switch (ch)
- {
- // Done in default. Only \" is legal JSON.
- //case '"': esc(out, '"') ; break ;
- //case '\'': esc(out, '\'') ; break ;
- case '\\': esc(out, '\\') ; break ;
- case '/':
- // Avoid </ which confuses if it's in HTML (this is from json.org)
- if ( i > 0 && string.charAt(i-1) == '<' )
+
+ switch (ch) {
+ // Done in default. Only \" is legal JSON.
+ // case '"': esc(out, '"') ; break ;
+ // case '\'': esc(out, '\'') ; break ;
+ case '\\' :
+ esc(out, '\\') ;
+ break ;
+ case '/' :
+ // Avoid </ which confuses if it's in HTML (this is from
+ // json.org)
+ if ( i > 0 && string.charAt(i - 1) == '<' )
esc(out, '/') ;
else
out.print(ch) ;
break ;
- case '\b': esc(out, 'b') ; break ;
- case '\f': esc(out, 'f') ; break ;
- case '\n': esc(out, 'n') ; break ;
- case '\r': esc(out, 'r') ; break ;
- case '\t': esc(out, 't') ; break ;
- default:
-
- if ( ch == quoteChar ) { esc(out, '"') ; break ; }
-
- //Character.isISOControl(ch) ; //00-1F, 7F-9F
+ case '\b' :
+ esc(out, 'b') ;
+ break ;
+ case '\f' :
+ esc(out, 'f') ;
+ break ;
+ case '\n' :
+ esc(out, 'n') ;
+ break ;
+ case '\r' :
+ esc(out, 'r') ;
+ break ;
+ case '\t' :
+ esc(out, 't') ;
+ break ;
+ default :
+
+ if ( ch == quoteChar ) {
+ esc(out, '"') ;
+ break ;
+ }
+
+ // Character.isISOControl(ch) ; //00-1F, 7F-9F
// This is more than Character.isISOControl
-
- if ( ch < ' ' ||
- (ch >= '\u007F' && ch <= '\u009F') ||
- (ch >= '\u2000' && ch < '\u2100'))
- {
+
+ if ( ch < ' ' || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch < '\u2100') ) {
out.print("\\u") ;
int x = ch ;
x = oneHex(out, x, 3) ;
@@ -262,7 +260,7 @@ public class JSWriter
x = oneHex(out, x, 0) ;
break ;
}
-
+
out.print(ch) ;
break ;
}
@@ -270,68 +268,86 @@ public class JSWriter
if ( quoteChar != CH_ZERO )
out.print(quoteChar) ;
}
+
+ private void startCompound() {
+ stack.push(new Ref<Boolean>(true)) ;
+ }
+
+ private void finishCompound() {
+ stack.pop() ;
+ }
+
+ private boolean isFirst() {
+ return stack.peek().getValue() ;
+ }
+
+ private void setNotFirst() {
+ stack.peek().setValue(false) ;
+ }
+
+ private void value(String x) {
+ out.print(outputQuotedString(x)) ;
+ }
+
+ private void value(boolean b) {
+ out.print(Boolean.toString(b)) ;
+ }
+
+ private void value(long integer) {
+ out.print(Long.toString(integer)) ;
+ }
+
+ private void value(BigDecimal number) {
+ out.print(number.toString()) ;
+ }
+
+ // Caution - assumes "Number" outputs legal JSON format
+ private void value(Number number) {
+ out.print(number.toString()) ;
+ }
- private void startCompound() { stack.push(new Ref<Boolean>(true)) ; }
- private void finishCompound() { stack.pop(); }
- private boolean isFirst() { return stack.peek().getValue() ; }
- private void setNotFirst() { stack.peek().setValue(false) ; }
-
- // Can only write a value in some context.
- private void value(String x) { out.print(outputQuotedString(x)); }
-
- private void value(boolean b) { out.print(Boolean.toString(b)) ; }
-
- private void value(long integer) { out.print(Long.toString(integer)) ; }
-
-// void valueString(String image) {}
-// void valueInteger(String image) {}
-// void valueDouble(String image) {}
-// void valueBoolean(boolean b) {}
-// void valueNull() {}
-// void valueDecimal(String image) {}
+ // void valueString(String image) {}
+ // void valueInteger(String image) {}
+ // void valueDouble(String image) {}
+ // void valueBoolean(boolean b) {}
+ // void valueNull() {}
+ // void valueDecimal(String image) {}
// Library-ize.
-
- private static boolean isA2Z(int ch)
- {
+
+ private static boolean isA2Z(int ch) {
return range(ch, 'a', 'z') || range(ch, 'A', 'Z') ;
}
- private static boolean isA2ZN(int ch)
- {
+ private static boolean isA2ZN(int ch) {
return range(ch, 'a', 'z') || range(ch, 'A', 'Z') || range(ch, '0', '9') ;
}
- private static boolean isNumeric(int ch)
- {
+ private static boolean isNumeric(int ch) {
return range(ch, '0', '9') ;
}
-
- private static boolean isWhitespace(int ch)
- {
- return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' ;
+
+ private static boolean isWhitespace(int ch) {
+ return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' ;
}
-
- private static boolean isNewlineChar(int ch)
- {
+
+ private static boolean isNewlineChar(int ch) {
return ch == '\r' || ch == '\n' ;
}
-
- private static boolean range(int ch, char a, char b)
- {
- return ( ch >= a && ch <= b ) ;
+
+ private static boolean range(int ch, char a, char b) {
+ return (ch >= a && ch <= b) ;
}
-
- private static void esc(IndentedWriter out, char ch)
- {
- out.print('\\') ; out.print(ch) ;
+
+ private static void esc(IndentedWriter out, char ch) {
+ out.print('\\') ;
+ out.print(ch) ;
}
-
- private static int oneHex(IndentedWriter out, int x, int i)
- {
- int y = BitsInt.unpack(x, 4*i, 4*i+4) ;
+
+ private static int oneHex(IndentedWriter out, int x, int i) {
+ int y = BitsInt.unpack(x, 4 * i, 4 * i + 4) ;
char charHex = Chars.hexDigitsUC[y] ;
- out.print(charHex) ;
- return BitsInt.clear(x, 4*i, 4*i+4) ;
+ out.print(charHex) ;
+ return BitsInt.clear(x, 4 * i, 4 * i + 4) ;
}
}