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 2016/07/31 20:06:46 UTC
[4/5] jena git commit: Add JSON.toString, JSON.toStringFlat
Add JSON.toString, JSON.toStringFlat
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/f65b2a39
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/f65b2a39
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/f65b2a39
Branch: refs/heads/master
Commit: f65b2a39c8bb9d85b7f8e7f6805033fff0b81674
Parents: 5ca4d94
Author: Andy Seaborne <an...@apache.org>
Authored: Sun Jul 31 14:43:18 2016 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Sun Jul 31 14:43:18 2016 +0100
----------------------------------------------------------------------
.../java/org/apache/jena/atlas/json/JSON.java | 117 +++++++++----------
1 file changed, 58 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/f65b2a39/jena-arq/src/main/java/org/apache/jena/atlas/json/JSON.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/json/JSON.java b/jena-arq/src/main/java/org/apache/jena/atlas/json/JSON.java
index df3727f..633507d 100644
--- a/jena-arq/src/main/java/org/apache/jena/atlas/json/JSON.java
+++ b/jena-arq/src/main/java/org/apache/jena/atlas/json/JSON.java
@@ -21,6 +21,7 @@ package org.apache.jena.atlas.json;
import java.io.* ;
import org.apache.jena.atlas.io.IO ;
+import org.apache.jena.atlas.io.IndentedLineBuffer ;
import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.atlas.json.io.JSONMaker ;
import org.apache.jena.atlas.json.io.JsonWriter ;
@@ -30,37 +31,32 @@ import org.apache.jena.atlas.json.io.parserjavacc.JSONParserJavaCC ;
/** A class that is the front door to the JSON subsystem */
public class JSON
{
- /** Parse a complete JSON object */
- public static JsonObject parse(InputStream input)
- {
- JSONMaker maker = new JSONMaker();
+ /** Parse a complete JSON object */
+ public static JsonObject parse(InputStream input) {
+ JSONMaker maker = new JSONMaker() ;
_parse(input, maker) ;
return (JsonObject)maker.jsonValue() ;
}
-
- /** Parse a complete JSON object */
- public static JsonObject parse(String string)
- {
+
+ /** Parse a complete JSON object */
+ public static JsonObject parse(String string) {
return _parse(new StringReader(string)) ;
}
- /** Parse any JSON value, not just an object, from an input stream */
- public static JsonValue parseAny(InputStream input)
- {
- JSONMaker maker = new JSONMaker();
+ /** Parse any JSON value, not just an object, from an input stream */
+ public static JsonValue parseAny(InputStream input) {
+ JSONMaker maker = new JSONMaker() ;
_parseAny(input, maker) ;
return maker.jsonValue() ;
}
- /** Parse any JSON value, not just an object, from a file */
- public static JsonValue parseAny(String string)
- {
+ /** Parse any JSON value, not just an object, from a file */
+ public static JsonValue parseAny(String string) {
return _parseAny(new StringReader(string)) ;
}
- /** Read a JSON object from a file */
- public static JsonObject read(String filename)
- {
+ /** Read a JSON object from a file */
+ public static JsonObject read(String filename) {
try (InputStream in = IO.openFileEx(filename)) {
return JSON.parse(in) ;
}
@@ -73,92 +69,96 @@ public class JSON
return null ;
}
}
-
- /** Read any JSON value, not just an object, from a file */
- public static JsonValue readAny(String filename)
- {
- try
- {
- try (InputStream in = IO.openFileEx( filename ))
- {
- return JSON.parseAny( in );
+
+ /** Read any JSON value, not just an object, from a file */
+ public static JsonValue readAny(String filename) {
+ try {
+ try (InputStream in = IO.openFileEx(filename)) {
+ return JSON.parseAny(in) ;
}
}
- catch (FileNotFoundException ex)
- {
- throw new RuntimeException("File not found: "+filename, ex) ;
+ catch (FileNotFoundException ex) {
+ throw new RuntimeException("File not found: " + filename, ex) ;
}
- catch (IOException ex)
- {
- IO.exception("IOException: "+filename, ex);
+ catch (IOException ex) {
+ IO.exception("IOException: " + filename, ex) ;
return null ;
}
}
-
- // Hide the reader versions - not encouraged due to charset problems.
- private static JsonObject _parse(Reader r)
- {
- JSONMaker maker = new JSONMaker();
+ // Hide the reader versions - not encouraged due to charset problems.
+
+ private static JsonObject _parse(Reader r) {
+ JSONMaker maker = new JSONMaker() ;
_parse(r, maker) ;
return (JsonObject)maker.jsonValue() ;
}
-
- private static JsonValue _parseAny(Reader r)
- {
- JSONMaker maker = new JSONMaker();
+
+ private static JsonValue _parseAny(Reader r) {
+ JSONMaker maker = new JSONMaker() ;
_parseAny(r, maker) ;
return maker.jsonValue() ;
}
-
+
// PARSER CHOICES
// Switch on parser choice.
- private static final boolean useJavaCC = false ;
+ private static final boolean useJavaCC = false ;
- private static void _parse(Reader r, JSONMaker maker)
- {
+ private static void _parse(Reader r, JSONMaker maker) {
if ( useJavaCC )
JSONParserJavaCC.parse(r, maker) ;
else
JSONParser.parse(r, maker) ;
}
- private static void _parseAny(Reader r, JSONMaker maker)
- {
+ private static void _parseAny(Reader r, JSONMaker maker) {
if ( useJavaCC )
JSONParserJavaCC.parseAny(r, maker) ;
else
JSONParser.parseAny(r, maker) ;
}
- private static void _parse(InputStream r, JSONMaker maker)
- {
+ private static void _parse(InputStream r, JSONMaker maker) {
if ( useJavaCC )
JSONParserJavaCC.parse(r, maker) ;
else
JSONParser.parse(r, maker) ;
}
- private static void _parseAny(InputStream r, JSONMaker maker)
- {
+ private static void _parseAny(InputStream r, JSONMaker maker) {
if ( useJavaCC )
JSONParserJavaCC.parseAny(r, maker) ;
else
JSONParser.parseAny(r, maker) ;
}
+ /** JsonValue to a formatted, multiline string */
+ public static String toString(JsonValue jValue) {
+ try ( IndentedLineBuffer b = new IndentedLineBuffer() ) {
+ JSON.write(b, jValue);
+ return b.asString() ;
+ }
+ }
+
+ /** JsonValue to a string with no newlines */
+ public static String toStringFlat(JsonValue jValue) {
+ try ( IndentedLineBuffer b = new IndentedLineBuffer() ) {
+ b.setFlatMode(true);
+ JSON.write(b, jValue);
+ return b.asString() ;
+ }
+ }
+
/** Write out a JSON value - pass a JSON Object to get legal exchangeable JSON */
- public static void write(OutputStream output, JsonValue jValue)
- {
+ public static void write(OutputStream output, JsonValue jValue) {
IndentedWriter iOut = new IndentedWriter(output) ;
write(iOut, jValue) ;
iOut.flush() ;
}
-
+
/** Write out a JSON value - pass a JSON Object to get legal exchangeable JSON */
- public static void write(IndentedWriter output, JsonValue jValue)
- {
+ public static void write(IndentedWriter output, JsonValue jValue) {
JsonWriter w = new JsonWriter(output) ;
w.startOutput() ;
jValue.visit(w) ;
@@ -166,8 +166,7 @@ public class JSON
}
/** Write out a JSON value to - pass a JSON Object to get legal exchangeable JSON */
- public static void write(JsonValue jValue)
- {
+ public static void write(JsonValue jValue) {
write(IndentedWriter.stdout, jValue) ;
}
}