throw new IllegalStateException(
"Encoding 'UTF-8' is not supported by this System", e);
}
Triple[] sortedTriples = tc.toArray(new Triple[tc.size()]);
Arrays.sort(sortedTriples, SUBJECT_COMPARATOR);
Triple triple;
NonLiteral subject = null;
String subjectStr = null;
UriRef predicate = null;
Map<UriRef, JSONArray> predicateValues = new HashMap<UriRef, JSONArray>();
JSONObject jSubject = new JSONObject();
try {
out.write("{"); // start the root object
for (int i = 0; i < sortedTriples.length; i++) {
triple = sortedTriples[i];
boolean subjectChange = !triple.getSubject().equals(subject);
if (subjectChange) {
if (subject != null) {
// write the predicate values
for (Entry<UriRef, JSONArray> predicates : predicateValues
.entrySet()) {
jSubject.put(
predicates.getKey().getUnicodeString(),
predicates.getValue());
}
// write subject
out.write(JSONObject.toString(subjectStr, jSubject));
out.write(",");
jSubject.clear(); // just clear
predicateValues.clear();
}
// init next subject
subject = triple.getSubject();
if (subject instanceof BNode) {
subjectStr = bNodeMgr.getBNodeId((BNode) subject);
} else { // if (subject instanceof UriRef)
subjectStr = ((UriRef) subject).getUnicodeString();
}
}
predicate = triple.getPredicate();
JSONArray values = predicateValues.get(predicate);
if (values == null) {
values = new JSONArray();
predicateValues.put(predicate, values);
}
values.add(writeObject(bNodeMgr, triple.getObject()));
}
if (subjectStr != null) {
for (Entry<UriRef, JSONArray> predicates : predicateValues
.entrySet()) {
jSubject.put(predicates.getKey().getUnicodeString(),