Skip to content

Commit 1fb45af

Browse files
committed
Fixed JSON formatting by adding the new "prettyPrint" format setting
1 parent 2169c8c commit 1fb45af

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

core/src/main/java/com/orientechnologies/orient/core/fetch/OFetchHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ private static void processRecord(final ORecordSchemaAware<?> record, final Obje
345345
// EMBEDDED, GO DEEPER
346346
fetch = true;
347347

348-
if (iFormat.contains("shallow") || fieldValue == null
348+
if (iFormat.contains("shallow")
349+
|| fieldValue == null
349350
|| (!fetch && fieldValue instanceof OIdentifiable)
350351
|| !(fieldValue instanceof OIdentifiable)
351352
&& (!(fieldValue instanceof ORecordLazyMultiValue) || !((ORecordLazyMultiValue) fieldValue).rawIterator().hasNext() || !(((ORecordLazyMultiValue) fieldValue)

core/src/main/java/com/orientechnologies/orient/core/fetch/json/OJSONFetchContext.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public void onAfterArray(final ORecordSchemaAware<?> iRootRecord, final String i
8484

8585
public void onBeforeCollection(final ORecordSchemaAware<?> iRootRecord, final String iFieldName, final Object iUserObject,
8686
final Collection<?> iCollection) {
87-
settings.indentLevel++;
8887
try {
8988
manageTypes(iFieldName, iCollection);
9089
jsonWriter.beginCollection(settings.indentLevel, true, iFieldName);
@@ -101,7 +100,6 @@ public void onAfterCollection(final ORecordSchemaAware<?> iRootRecord, final Str
101100
} catch (IOException e) {
102101
throw new OFetchException("Error writing collection field " + iFieldName + " of record " + iRootRecord.getIdentity(), e);
103102
}
104-
settings.indentLevel--;
105103
}
106104

107105
public void onBeforeMap(final ORecordSchemaAware<?> iRootRecord, final String iFieldName, final Object iUserObject) {
@@ -178,9 +176,6 @@ private void appendType(final StringBuilder iBuffer, final String iFieldName, fi
178176
public void writeSignature(final OJSONWriter json, final ORecordInternal<?> record) throws IOException {
179177
boolean firstAttribute = true;
180178

181-
if (settings.indentLevel > -1)
182-
settings.indentLevel++;
183-
184179
if (settings.includeType) {
185180
json.writeAttribute(firstAttribute ? settings.indentLevel : 0, firstAttribute, ODocumentHelper.ATTRIBUTE_TYPE, ""
186181
+ (char) record.getRecordType());

core/src/main/java/com/orientechnologies/orient/core/serialization/serializer/OJSONWriter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public OJSONWriter(final Writer out) {
5151
public OJSONWriter(final Writer out, final String iJsonFormat) {
5252
this.out = out;
5353
this.format = iJsonFormat;
54+
if (iJsonFormat.contains("prettyPrint"))
55+
prettyPrint = true;
5456
}
5557

5658
public OJSONWriter beginObject() throws IOException {

core/src/main/java/com/orientechnologies/orient/core/serialization/serializer/record/string/ORecordSerializerJSON.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ public class FormatSettings {
7777
public boolean attribSameRow;
7878
public boolean alwaysFetchEmbeddedDocuments;
7979
public int indentLevel;
80-
public String fetchPlan = null;
80+
public String fetchPlan = null;
8181
public boolean keepTypes;
82-
public boolean dateAsLong = false;
82+
public boolean dateAsLong = false;
83+
public boolean prettyPrint = false;
8384

8485
public FormatSettings(final String iFormat) {
8586
if (iFormat == null) {
@@ -88,7 +89,7 @@ public FormatSettings(final String iFormat) {
8889
includeId = true;
8990
includeClazz = true;
9091
attribSameRow = true;
91-
indentLevel = 0;
92+
indentLevel = 1;
9293
fetchPlan = "";
9394
keepTypes = true;
9495
alwaysFetchEmbeddedDocuments = true;
@@ -99,7 +100,7 @@ public FormatSettings(final String iFormat) {
99100
includeClazz = false;
100101
attribSameRow = false;
101102
alwaysFetchEmbeddedDocuments = false;
102-
indentLevel = 0;
103+
indentLevel = 1;
103104
keepTypes = true;
104105

105106
final String[] format = iFormat.split(",");
@@ -124,6 +125,8 @@ else if (f.startsWith("alwaysFetchEmbedded"))
124125
alwaysFetchEmbeddedDocuments = true;
125126
else if (f.startsWith("dateAsLong"))
126127
dateAsLong = true;
128+
else if (f.startsWith("prettyPrint"))
129+
prettyPrint = true;
127130
}
128131
}
129132
}
@@ -569,7 +572,7 @@ public StringBuilder toString(final ORecordInternal<?> iRecord, final StringBuil
569572
final OJSONWriter json = new OJSONWriter(buffer, iFormat);
570573
final FormatSettings settings = new FormatSettings(iFormat);
571574

572-
json.beginObject(settings.indentLevel);
575+
json.beginObject();
573576
OJSONFetchContext context = new OJSONFetchContext(json, settings);
574577
context.writeSignature(json, iRecord);
575578

@@ -592,7 +595,7 @@ public StringBuilder toString(final ORecordInternal<?> iRecord, final StringBuil
592595
throw new OSerializationException("Error on marshalling record of type '" + iRecord.getClass()
593596
+ "' to JSON. The record type cannot be exported to JSON");
594597

595-
json.endObject(settings.indentLevel);
598+
json.endObject(0, true);
596599

597600
iOutput.append(buffer);
598601
return iOutput;

0 commit comments

Comments
 (0)