You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by fc...@apache.org on 2010/12/02 16:36:38 UTC
svn commit: r1041413 - in /incubator/stanbol/trunk/fise: generic/jsonld/
generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/
generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/
jersey/src/main/java/eu/iksproject/fise/jersey/writers/ jersey/src/...
Author: fchrist
Date: Thu Dec 2 15:36:37 2010
New Revision: 1041413
URL: http://svn.apache.org/viewvc?rev=1041413&view=rev
Log:
Merged latest version of JSON-LD implementation from Google SVN. FISE now supports JSON-LD output with pretty printing.
Modified:
incubator/stanbol/trunk/fise/generic/jsonld/pom.xml
incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonComparator.java
incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonLd.java
incubator/stanbol/trunk/fise/generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/JsonLdTest.java
incubator/stanbol/trunk/fise/jersey/src/main/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProvider.java
incubator/stanbol/trunk/fise/jersey/src/test/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProviderTest.java
Modified: incubator/stanbol/trunk/fise/generic/jsonld/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/generic/jsonld/pom.xml?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/generic/jsonld/pom.xml (original)
+++ incubator/stanbol/trunk/fise/generic/jsonld/pom.xml Thu Dec 2 15:36:37 2010
@@ -55,9 +55,13 @@
<dependencies>
<dependency>
+ <groupId>org.codehaus.jettison</groupId>
+ <artifactId>jettison</artifactId>
+ </dependency>
+ <!-- dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
- </dependency>
+ </dependency -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Modified: incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonComparator.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonComparator.java?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonComparator.java (original)
+++ incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonComparator.java Thu Dec 2 15:36:37 2010
@@ -13,7 +13,9 @@ public class JsonComparator implements C
@Override
public int compare(Object arg0, Object arg1) {
int value = 0;
- if (arg0.equals("#"))
+ if (arg0.equals(arg1))
+ value = 0;
+ else if (arg0.equals("#"))
value = -1;
else if (arg1.equals("#"))
value = 1;
Modified: incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonLd.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonLd.java?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonLd.java (original)
+++ incubator/stanbol/trunk/fise/generic/jsonld/src/main/java/eu/iksproject/fise/jsonld/JsonLd.java Thu Dec 2 15:36:37 2010
@@ -8,9 +8,6 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
-
/**
* @author Fabian Christ
*
@@ -57,17 +54,30 @@ public class JsonLd {
if (useJointGraphs) {
Map<String, Object> json = createJointGraph();
- return JSONObject.toJSONString(json);
+ return JsonSerializer.toString(json);
+ }
+ else {
+ List<Object> json = createDisjointGraph();
+
+ return JsonSerializer.toString(json);
+ }
+ }
+
+ public String toString(int indent) {
+ if (useJointGraphs) {
+ Map<String, Object> json = createJointGraph();
+
+ return JsonSerializer.toString(json, indent);
}
else {
- JSONArray json = createDisjointGraph();
+ List<Object> json = createDisjointGraph();
- return JSONArray.toJSONString(json);
+ return JsonSerializer.toString(json, indent);
}
}
- private JSONArray createDisjointGraph() {
- JSONArray json = new JSONArray();
+ private List<Object> createDisjointGraph() {
+ List<Object> json = new ArrayList<Object>();
if (this.resourceMap.size() > 0) {
for (String subject : this.resourceMap.keySet()) {
@@ -104,10 +114,11 @@ public class JsonLd {
return json;
}
+ @SuppressWarnings("unchecked")
private Map<String, Object> createJointGraph() {
Map<String, Object> json = new TreeMap<String, Object>(new JsonComparator());
if (this.resourceMap.size() > 0) {
- JSONArray subjects = new JSONArray();
+ List<Object> subjects = new ArrayList<Object>();
for (String subject : this.resourceMap.keySet()) {
// put subject
@@ -133,7 +144,7 @@ public class JsonLd {
// put subjects
if (subjects.size() > 0) {
if (subjects.size() == 1) {
- json = (Map) subjects.get(0);
+ json = (Map<String, Object>) subjects.get(0);
} else {
json.put("@", subjects);
}
@@ -148,14 +159,13 @@ public class JsonLd {
}
json.put("#", nsObject);
}
-
+
return json;
}
- @SuppressWarnings("unchecked")
private void putTypes(Map<String, Object> subjectObject, JsonLdResource resource) {
if (resource.getTypes().size() > 0) {
- JSONArray types = new JSONArray();
+ List<String> types = new ArrayList<String>();
for (String type : resource.getTypes()) {
types.add(this.applyNamespace(type));
}
@@ -189,13 +199,12 @@ public class JsonLd {
for (int i=0; i<stringArray.length; i++) {
valueList.add(this.applyNamespace(stringArray[i]));
}
- JSONArray jsonArray = new JSONArray();
- jsonArray.addAll(valueList);
+ List<Object> jsonArray = new ArrayList<Object>(valueList);
jsonObject.put(this.applyNamespace(property), jsonArray);
}
else if (value instanceof Object[]) {
Object[] objectArray = (Object[]) value;
- JSONArray jsonArray = new JSONArray();
+ List<Object> jsonArray = new ArrayList<Object>();
for (Object object : objectArray) {
jsonArray.add(object);
}
Modified: incubator/stanbol/trunk/fise/generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/JsonLdTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/JsonLdTest.java?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/JsonLdTest.java (original)
+++ incubator/stanbol/trunk/fise/generic/jsonld/src/test/java/eu/iksproject/fise/jsonld/JsonLdTest.java Thu Dec 2 15:36:37 2010
@@ -36,8 +36,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"myvocab\":\"http:\\/\\/example.org\\/myvocab#\"},\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/manu.sporny.org\\/>\",\"foaf:name\":\"Manu Sporny\",\"myvocab:credits\":500,\"sioc:avatar\":\"<http:\\/\\/twitter.com\\/account\\/profile_image\\/manusporny>\"}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"myvocab\": \"http:\\/\\/example.org\\/myvocab#\"\n },\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/manu.sporny.org\\/>\",\n \"foaf:name\": \"Manu Sporny\",\n \"myvocab:credits\": 500, \"sioc:avatar\": \"<http:\\/\\/twitter.com\\/account\\/profile_image\\/manusporny>\"\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -69,8 +72,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"},\"@\":[{\"@\":\"_:bnode1\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/bob>\",\"foaf:name\":\"Bob\"},{\"@\":\"_:bnode2\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/eve>\",\"foaf:name\":\"Eve\"},{\"@\":\"_:bnode3\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/bert>\",\"foaf:name\":\"Bert\"}]}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"\n },\n \"@\": [\n {\n \"@\": \"_:bnode1\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/bob>\",\n \"foaf:name\": \"Bob\"\n },\n {\n \"@\": \"_:bnode2\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/eve>\",\n \"foaf:name\": \"Eve\"\n },\n {\n \"@\": \"_:bnode3\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/bert>\",\n \"foaf:name\": \"Bert\"\n }\n ]\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -103,8 +109,12 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "[{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"},\"@\":\"_:bnode1\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/bob>\",\"foaf:name\":\"Bob\"},{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"},\"@\":\"_:bnode2\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/eve>\",\"foaf:name\":\"Eve\"},{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"},\"@\":\"_:bnode3\",\"a\":\"foaf:Person\",\"foaf:homepage\":\"<http:\\/\\/example.com\\/eve>\",\"foaf:name\":\"Eve\"}]";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "[\n {\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"\n },\n \"@\": \"_:bnode1\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/bob>\",\n \"foaf:name\": \"Bob\"\n },\n {\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"\n },\n \"@\": \"_:bnode2\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/eve>\",\n \"foaf:name\": \"Eve\"\n },\n {\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\"\n },\n \"@\": \"_:bnode3\",\n \"a\": \"foaf:Person\",\n \"foaf:homepage\": \"<http:\\/\\/example.com\\/eve>\",\n \"foaf:name\": \"Eve\"\n }\n]";
+ assertEquals(expectedIndent, actualIndent);
+
}
@Test
@@ -124,8 +134,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"fn\":\"http:\\/\\/microformats.org\\/profile\\/hcard#fn\",\"url\":\"http:\\/\\/microformats.org\\/profile\\/hcard#url\",\"vcard\":\"http:\\/\\/microformats.org\\/profile\\/hcard#vcard\"},\"@\":\"_:bnode1\",\"a\":\"vcard\",\"fn\":\"Tantek Celik\",\"url\":\"<http:\\/\\/tantek.com\\/>\"}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"fn\": \"http:\\/\\/microformats.org\\/profile\\/hcard#fn\",\n \"url\": \"http:\\/\\/microformats.org\\/profile\\/hcard#url\",\n \"vcard\": \"http:\\/\\/microformats.org\\/profile\\/hcard#vcard\"\n },\n \"@\": \"_:bnode1\",\n \"a\": \"vcard\",\n \"fn\": \"Tantek Celik\",\n \"url\": \"<http:\\/\\/tantek.com\\/>\"\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -149,8 +162,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"#base\":\"http:\\/\\/example.org\\/baseurl\\/\",\"#vocab\":\"http:\\/\\/example.org\\/default-vocab#\",\"cal\":\"http:\\/\\/www.w3.org\\/2002\\/12\\/cal\\/ical#\",\"cc\":\"http:\\/\\/creativecommons.org\\/ns#\",\"dc\":\"http:\\/\\/purl.org\\/dc\\/terms\\/\",\"doap\":\"http:\\/\\/usefulinc.com\\/ns\\/doap#\",\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"geo\":\"http:\\/\\/www.w3.org\\/2003\\/01\\/geo\\/wgs84_pos#\",\"homepage\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/homepage\",\"name\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/name\",\"Person\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/Person\",\"sioc\":\"http:\\/\\/rdfs.org\\/sioc\\/ns#\",\"vcard\":\"http:\\/\\/www.w3.org\\/2006\\/vcard\\/ns#\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"}}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"#base\": \"http:\\/\\/example.org\\/baseurl\\/\",\n \"#vocab\": \"http:\\/\\/example.org\\/default-vocab#\",\n \"cal\": \"http:\\/\\/www.w3.org\\/2002\\/12\\/cal\\/ical#\",\n \"cc\": \"http:\\/\\/creativecommons.org\\/ns#\",\n \"dc\": \"http:\\/\\/purl.org\\/dc\\/terms\\/\",\n \"doap\": \"http:\\/\\/usefulinc.com\\/ns\\/doap#\",\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\n \"geo\": \"http:\\/\\/www.w3.org\\/2003\\/01\\/geo\\/wgs84_pos#\",\n \"homepage\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/homepage\",\n \"name\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/name\",\n \"Person\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/Person\",\n \"sioc\": \"http:\\/\\/rdfs.org\\/sioc\\/ns#\",\n \"vcard\": \"http:\\/\\/www.w3.org\\/2006\\/vcard\\/ns#\",\n \"xsd\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n }\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -181,8 +197,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "[{\"@\":\"<http:\\/\\/purl.oreilly.com\\/products\\/9780596007683.BOOK>\",\"a\":\"<http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Expression>\",\"http:\\/\\/purl.org\\/dc\\/terms\\/type\":\"<http:\\/\\/purl.oreilly.com\\/product-types\\/BOOK>\"},{\"@\":\"<http:\\/\\/purl.oreilly.com\\/products\\/9780596802189.EBOOK>\",\"a\":\"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Expression\",\"http:\\/\\/purl.org\\/dc\\/terms\\/type\":\"<http:\\/\\/purl.oreilly.com\\/product-types\\/BOOK>\"},{\"@\":\"<http:\\/\\/purl.oreilly.com\\/works\\/45U8QJGZSQKDH8N>\",\"a\":\"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Work\",\"http:\\/\\/purl.org\\/dc\\/terms\\/creator\":\"Whil Wheaton\",\"http:\\/\\/purl.org\\/dc\\/terms\\/title\":\"Just a Geek\",\"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#realization\":[\"<http:\\/\\/purl.oreilly.com\\/products\\/9780596007683.BOOK>\",\"<http:\\/\\/purl.oreilly.com\\/products\\/9780596802189.EBOOK>\"]}]";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "[\n {\n \"@\": \"<http:\\/\\/purl.oreilly.com\\/products\\/9780596007683.BOOK>\",\n \"a\": \"<http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Expression>\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/type\": \"<http:\\/\\/purl.oreilly.com\\/product-types\\/BOOK>\"\n },\n {\n \"@\": \"<http:\\/\\/purl.oreilly.com\\/products\\/9780596802189.EBOOK>\",\n \"a\": \"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Expression\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/type\": \"<http:\\/\\/purl.oreilly.com\\/product-types\\/BOOK>\"\n },\n {\n \"@\": \"<http:\\/\\/purl.oreilly.com\\/works\\/45U8QJGZSQKDH8N>\",\n \"a\": \"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#Work\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/creator\": \"Whil Wheaton\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/title\": \"Just a Geek\",\n \"http:\\/\\/purl.org\\/vocab\\/frbr\\/core#realization\": [\n \"<http:\
\/\\/purl.oreilly.com\\/products\\/9780596007683.BOOK>\",\n \"<http:\\/\\/purl.oreilly.com\\/products\\/9780596802189.EBOOK>\"\n ]\n }\n]";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -198,8 +217,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"dc\":\"http:\\/\\/purl.org\\/dc\\/terms\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"dc:modified\":\"2010-05-29T14:17:39+02:00^^xsd:dateTime\"}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"dc\": \"http:\\/\\/purl.org\\/dc\\/terms\\/\",\n \"xsd\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n },\n \"dc:modified\": \"2010-05-29T14:17:39+02:00^^xsd:dateTime\"\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -217,8 +239,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@\":\"<http:\\/\\/example.org\\/people#joebob>\",\"foaf:nick\":[\"\\\"stu\\\"^^xsd:string\",\"\\\"groknar\\\"^^xsd:string\",\"\\\"radface\\\"^^xsd:string\"]}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\n \"xsd\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n },\n \"@\": \"<http:\\/\\/example.org\\/people#joebob>\",\n \"foaf:nick\": [\n \"\\\"stu\\\"^^xsd:string\",\n \"\\\"groknar\\\"^^xsd:string\",\n \"\\\"radface\\\"^^xsd:string\"\n ]\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -237,8 +262,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@\":\"<http:\\/\\/example.org\\/people#joebob>\",\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/nick\":[\"\\\"stu\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\"\\\"groknar\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\"\\\"radface\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\"]}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\n \"xsd\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n },\n \"@\": \"<http:\\/\\/example.org\\/people#joebob>\",\n \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/nick\": [\n \"\\\"stu\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\n \"\\\"groknar\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\n \"\\\"radface\\\"^^http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\"\n ]\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@Test
@@ -257,8 +285,11 @@ public class JsonLdTest {
String actual = jsonLd.toString();
String expected = "{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@\":\"<http:\\/\\/example.org\\/people#joebob>\",\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/nick\":[\"\\\"stu\\\"^^xsd:string\",\"\\\"groknar\\\"^^xsd:string\",\"\\\"radface\\\"^^xsd:string\"]}";
-
assertEquals(expected, actual);
+
+ String actualIndent = jsonLd.toString(4);
+ String expectedIndent = "{\n \"#\": {\n \"foaf\": \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\n \"xsd\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n },\n \"@\": \"<http:\\/\\/example.org\\/people#joebob>\",\n \"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/nick\": [\n \"\\\"stu\\\"^^xsd:string\",\n \"\\\"groknar\\\"^^xsd:string\",\n \"\\\"radface\\\"^^xsd:string\"\n ]\n}";
+ assertEquals(expectedIndent, actualIndent);
}
@@ -268,6 +299,7 @@ public class JsonLdTest {
String s = actual;
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replace("\"", "\\\"");
+ s = s.replace("\n", "\\n");
System.out.println(s);
}
}
Modified: incubator/stanbol/trunk/fise/jersey/src/main/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProvider.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/jersey/src/main/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProvider.java?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/jersey/src/main/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProvider.java (original)
+++ incubator/stanbol/trunk/fise/jersey/src/main/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProvider.java Thu Dec 2 15:36:37 2010
@@ -47,6 +47,8 @@ public class JsonLdSerializerProvider im
// Map from Namespace -> to Prefix
private Map<String, String> namespacePrefixMap = new HashMap<String, String>();
+
+ private int indentation = 2;
@Override
public void serialize(OutputStream serializedGraph, TripleCollection tc, String formatIdentifier) {
@@ -87,7 +89,7 @@ public class JsonLdSerializerProvider im
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(serializedGraph));
- writer.write(jsonLd.toString());
+ writer.write(jsonLd.toString(this.indentation));
writer.flush();
} catch (IOException ioe) {
logger.error(ioe.getMessage());
@@ -130,4 +132,23 @@ public class JsonLdSerializerProvider im
this.namespacePrefixMap = knownNamespaces;
}
+ /**
+ * Returns the current number of space characters which are used
+ * to indent the serialized output.
+ *
+ * @return Number of space characters used for indentation.
+ */
+ public int getIndentation() {
+ return indentation;
+ }
+
+ /**
+ * Sets the number of characters used per indentation level for the serialized output.<br />
+ * Set this value to zero (0) if you don't want indentation. Default value is 2.
+ *
+ * @param indentation Number of space characters used for indentation.
+ */
+ public void setIndentation(int indentation) {
+ this.indentation = indentation;
+ }
}
Modified: incubator/stanbol/trunk/fise/jersey/src/test/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/fise/jersey/src/test/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProviderTest.java?rev=1041413&r1=1041412&r2=1041413&view=diff
==============================================================================
--- incubator/stanbol/trunk/fise/jersey/src/test/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProviderTest.java (original)
+++ incubator/stanbol/trunk/fise/jersey/src/test/java/eu/iksproject/fise/jersey/writers/JsonLdSerializerProviderTest.java Thu Dec 2 15:36:37 2010
@@ -50,11 +50,26 @@ public class JsonLdSerializerProviderTes
getTextAnnotation(ci, "Person", "Patrick Marshall", context, OntologicalClasses.DBPEDIA_PERSON);
OutputStream serializedGraph = new ByteArrayOutputStream();
+ this.jsonldProvider.setIndentation(0);
this.jsonldProvider.serialize(serializedGraph, ci.getMetadata(), formatIdentifier);
String expected = "{\"@\":\"<urn:iks-project:fise:test:text-annotation:Person>\",\"a\":[\"<http:\\/\\/fise.iks-project.eu\\/ontology\\/Enhancement>\",\"<http:\\/\\/fise.iks-project.eu\\/ontology\\/TextAnnotation>\"],\"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\":\"\\\"20\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int>\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\":\"\\\"Patrick Marshall\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string>\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\":\"\\\"Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string>\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\":\"\\\"4\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int>\",\"http:\\/\\/purl.org\\/dc\\/terms\\/created\":\"\\\"2010-10-27T14:00:00+02:00\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#dateTime>\",\"http:
\\/\\/purl.org\\/dc\\/terms\\/creator\":\"<urn:iks-project:fise:test:dummyEngine>\",\"http:\\/\\/purl.org\\/dc\\/terms\\/type\":\"<http:\\/\\/dbpedia.org\\/ontology\\/Person>\"}";
+ String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
+ public void testSingleSubjectSerializeNoNsWithIndent() {
+ String context = "Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.";
+
+ ContentItem ci = getContentItem("urn:iks-project:fise:test:content-item:person", context);
+ getTextAnnotation(ci, "Person", "Patrick Marshall", context, OntologicalClasses.DBPEDIA_PERSON);
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ this.jsonldProvider.serialize(serializedGraph, ci.getMetadata(), formatIdentifier);
+
+ String expected = "{\n \"@\": \"<urn:iks-project:fise:test:text-annotation:Person>\",\n \"a\": [\n \"<http:\\/\\/fise.iks-project.eu\\/ontology\\/Enhancement>\",\n \"<http:\\/\\/fise.iks-project.eu\\/ontology\\/TextAnnotation>\"\n ],\n \"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\": \"\\\"20\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int>\",\n \"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\": \"\\\"Patrick Marshall\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string>\",\n \"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\": \"\\\"Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string>\",\n \"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\": \"\\\"4\\\"^^<http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int>\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/created\": \"\\\"2010-10-27T14:00:00+02:00\\\"^^<http:\\/\
\/www.w3.org\\/2001\\/XMLSchema#dateTime>\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/creator\": \"<urn:iks-project:fise:test:dummyEngine>\",\n \"http:\\/\\/purl.org\\/dc\\/terms\\/type\": \"<http:\\/\\/dbpedia.org\\/ontology\\/Person>\"\n}";
String result = serializedGraph.toString();
-
Assert.assertEquals(expected, result);
}
@@ -71,12 +86,34 @@ public class JsonLdSerializerProviderTes
nsMap.put("http://www.w3.org/2001/XMLSchema#", "xmlns");
nsMap.put("http://dbpedia.org/ontology/", "dbpedia");
nsMap.put("http://purl.org/dc/terms", "dcterms");
+ this.jsonldProvider.setIndentation(0);
this.jsonldProvider.setNamespacePrefixMap(nsMap);
this.jsonldProvider.serialize(serializedGraph, ci.getMetadata(), formatIdentifier);
String expected = "{\"#\":{\"dbpedia\":\"http:\\/\\/dbpedia.org\\/ontology\\/\",\"dcterms\":\"http:\\/\\/purl.org\\/dc\\/terms\",\"fise\":\"http:\\/\\/fise.iks-project.eu\\/ontology\\/\",\"xmlns\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@\":\"<urn:iks-project:fise:test:text-annotation:Person>\",\"a\":[\"<fise:Enhancement>\",\"<fise:TextAnnotation>\"],\"dcterms:\\/created\":\"\\\"2010-10-27T14:00:00+02:00\\\"^^<xmlns:dateTime>\",\"dcterms:\\/creator\":\"<urn:iks-project:fise:test:dummyEngine>\",\"dcterms:\\/type\":\"<dbpedia:Person>\",\"fise:end\":\"\\\"20\\\"^^<xmlns:int>\",\"fise:selected-text\":\"\\\"Patrick Marshall\\\"^^<xmlns:string>\",\"fise:selection-context\":\"\\\"Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.\\\"^^<xmlns:string>\",\"fise:start\":\"\\\"4\\\"^^<xmlns:int>\"}";
String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
+ public void testSingleSubjectSerializeWithNsWithIndent() {
+ String context = "Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.";
+
+ ContentItem ci = getContentItem("urn:iks-project:fise:test:content-item:person", context);
+ getTextAnnotation(ci, "Person", "Patrick Marshall", context, OntologicalClasses.DBPEDIA_PERSON);
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ Map<String, String> nsMap = new HashMap<String, String>();
+ nsMap.put("http://fise.iks-project.eu/ontology/", "fise");
+ nsMap.put("http://www.w3.org/2001/XMLSchema#", "xmlns");
+ nsMap.put("http://dbpedia.org/ontology/", "dbpedia");
+ nsMap.put("http://purl.org/dc/terms", "dcterms");
+ this.jsonldProvider.setIndentation(4);
+ this.jsonldProvider.setNamespacePrefixMap(nsMap);
+ this.jsonldProvider.serialize(serializedGraph, ci.getMetadata(), formatIdentifier);
+
+ String expected = "{\n \"#\": {\n \"dbpedia\": \"http:\\/\\/dbpedia.org\\/ontology\\/\",\n \"dcterms\": \"http:\\/\\/purl.org\\/dc\\/terms\",\n \"fise\": \"http:\\/\\/fise.iks-project.eu\\/ontology\\/\",\n \"xmlns\": \"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"\n },\n \"@\": \"<urn:iks-project:fise:test:text-annotation:Person>\",\n \"a\": [\n \"<fise:Enhancement>\",\n \"<fise:TextAnnotation>\"\n ],\n \"dcterms:\\/created\": \"\\\"2010-10-27T14:00:00+02:00\\\"^^<xmlns:dateTime>\",\n \"dcterms:\\/creator\": \"<urn:iks-project:fise:test:dummyEngine>\",\n \"dcterms:\\/type\": \"<dbpedia:Person>\",\n \"fise:end\": \"\\\"20\\\"^^<xmlns:int>\",\n \"fise:selected-text\": \"\\\"Patrick Marshall\\\"^^<xmlns:string>\",\n \"fise:selection-context\": \"\\\"Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in New Zealand and worked at the University of Otago.\\\"^^<xmlns:string>\",\n \"fis
e:start\": \"\\\"4\\\"^^<xmlns:int>\"\n}";
+ String result = serializedGraph.toString();
Assert.assertEquals(expected, result);
}
@@ -138,6 +175,7 @@ public class JsonLdSerializerProviderTes
String s = result;
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replace("\"", "\\\"");
+ s = s.replace("\n", "\\n");
System.out.println(s);
}
}