Skip to content

Commit

Permalink
refactor: moved class for json formatting settings to specific file
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Jul 15, 2024
1 parent 057b0c3 commit 836cd89
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerJSON;
import com.orientechnologies.orient.core.serialization.serializer.record.string.FormatSettings;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Collection;
Expand Down Expand Up @@ -434,8 +434,7 @@ private static void processRecord(
if (!fetchListener.requireFieldProcessing() && fetchPlan == OFetchHelper.DEFAULT_FETCHPLAN) {
return;
}
final ORecordSerializerJSON.FormatSettings settings =
new ORecordSerializerJSON.FormatSettings(format);
final FormatSettings settings = new FormatSettings(format);

// Pre-process to gather fieldTypes
fetchContext.onBeforeFetch(record);
Expand Down Expand Up @@ -549,8 +548,7 @@ private static void process(
final String format,
final Set<String> toRemove,
final String fieldName) {
final ORecordSerializerJSON.FormatSettings settings =
new ORecordSerializerJSON.FormatSettings(format);
final FormatSettings settings = new FormatSettings(format);

Object fieldValue;
final String fieldPath =
Expand Down Expand Up @@ -673,7 +671,7 @@ private static void fetch(
final String iFieldPathFromRoot,
final OFetchListener iListener,
final OFetchContext iContext,
final ORecordSerializerJSON.FormatSettings settings)
final FormatSettings settings)
throws IOException {
int currentLevel = iCurrentLevel + 1;
int fieldDepthLevel = iFieldDepthLevel;
Expand Down Expand Up @@ -761,7 +759,7 @@ private static void fetchMap(
final String iFieldPathFromRoot,
final OFetchListener iListener,
final OFetchContext iContext,
final ORecordSerializerJSON.FormatSettings settings)
final FormatSettings settings)
throws IOException {
final Map<String, ODocument> linked = (Map<String, ODocument>) fieldValue;
iContext.onBeforeMap(iRootRecord, fieldName, iUserObject);
Expand Down Expand Up @@ -859,7 +857,7 @@ private static void fetchArray(
final String iFieldPathFromRoot,
final OFetchListener iListener,
final OFetchContext context,
ORecordSerializerJSON.FormatSettings settings) {
FormatSettings settings) {
if (fieldValue instanceof ODocument[]) {
final ODocument[] linked = (ODocument[]) fieldValue;
context.onBeforeArray(rootRecord, fieldName, iUserObject, linked);
Expand Down Expand Up @@ -911,7 +909,7 @@ private static void fetchCollection(
final String iFieldPathFromRoot,
final OFetchListener iListener,
final OFetchContext context,
final ORecordSerializerJSON.FormatSettings settings)
final FormatSettings settings)
throws IOException {
final Iterable<?> linked;
if (fieldValue instanceof Iterable<?> || fieldValue instanceof ORidBag) {
Expand Down Expand Up @@ -1038,7 +1036,7 @@ private static void fetchDocument(
final String iFieldPathFromRoot,
final OFetchListener iListener,
final OFetchContext iContext,
final ORecordSerializerJSON.FormatSettings settings) {
final FormatSettings settings) {
if (fieldValue instanceof ORID && !((ORID) fieldValue).isValid()) {
// RID NULL: TREAT AS "NULL" VALUE
iContext.onBeforeStandardField(fieldValue, fieldName, iRootRecord, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentHelper;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
import com.orientechnologies.orient.core.serialization.serializer.record.string.FormatSettings;
import com.orientechnologies.orient.core.serialization.serializer.record.string.OFieldTypesString;
import com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerJSON.FormatSettings;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.orientechnologies.orient.core.serialization.serializer.record.string;

public class FormatSettings {
public boolean includeVer;
public boolean includeType;
public boolean includeId;
public boolean includeClazz;
public boolean attribSameRow;
public boolean alwaysFetchEmbeddedDocuments;
public int indentLevel;
public String fetchPlan = null;
public boolean keepTypes = true;
public boolean dateAsLong = false;
public boolean prettyPrint = false;

public FormatSettings(final String iFormat) {
if (iFormat == null) {
includeType = true;
includeVer = true;
includeId = true;
includeClazz = true;
attribSameRow = true;
indentLevel = 0;
fetchPlan = "";
keepTypes = true;
alwaysFetchEmbeddedDocuments = true;
} else {
includeType = false;
includeVer = false;
includeId = false;
includeClazz = false;
attribSameRow = false;
alwaysFetchEmbeddedDocuments = false;
indentLevel = 0;
keepTypes = false;

if (iFormat != null && !iFormat.isEmpty()) {
final String[] format = iFormat.split(",");
for (String f : format)
if (f.equals("type")) includeType = true;
else if (f.equals("rid")) includeId = true;
else if (f.equals("version")) includeVer = true;
else if (f.equals("class")) includeClazz = true;
else if (f.equals("attribSameRow")) attribSameRow = true;
else if (f.startsWith("indent"))
indentLevel = Integer.parseInt(f.substring(f.indexOf(':') + 1));
else if (f.startsWith("fetchPlan")) fetchPlan = f.substring(f.indexOf(':') + 1);
else if (f.startsWith("keepTypes")) keepTypes = true;
else if (f.startsWith("alwaysFetchEmbedded")) alwaysFetchEmbeddedDocuments = true;
else if (f.startsWith("dateAsLong")) dateAsLong = true;
else if (f.startsWith("prettyPrint")) prettyPrint = true;
else if (f.startsWith("graph") || f.startsWith("shallow"))
// SUPPORTED IN OTHER PARTS
;
else throw new IllegalArgumentException("Unrecognized JSON formatting option: " + f);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,64 +85,6 @@ private interface CollectionItemVisitor {
void visitItem(Object item);
}

public static class FormatSettings {
public boolean includeVer;
public boolean includeType;
public boolean includeId;
public boolean includeClazz;
public boolean attribSameRow;
public boolean alwaysFetchEmbeddedDocuments;
public int indentLevel;
public String fetchPlan = null;
public boolean keepTypes = true;
public boolean dateAsLong = false;
public boolean prettyPrint = false;

public FormatSettings(final String iFormat) {
if (iFormat == null) {
includeType = true;
includeVer = true;
includeId = true;
includeClazz = true;
attribSameRow = true;
indentLevel = 0;
fetchPlan = "";
keepTypes = true;
alwaysFetchEmbeddedDocuments = true;
} else {
includeType = false;
includeVer = false;
includeId = false;
includeClazz = false;
attribSameRow = false;
alwaysFetchEmbeddedDocuments = false;
indentLevel = 0;
keepTypes = false;

if (iFormat != null && !iFormat.isEmpty()) {
final String[] format = iFormat.split(",");
for (String f : format)
if (f.equals("type")) includeType = true;
else if (f.equals("rid")) includeId = true;
else if (f.equals("version")) includeVer = true;
else if (f.equals("class")) includeClazz = true;
else if (f.equals("attribSameRow")) attribSameRow = true;
else if (f.startsWith("indent"))
indentLevel = Integer.parseInt(f.substring(f.indexOf(':') + 1));
else if (f.startsWith("fetchPlan")) fetchPlan = f.substring(f.indexOf(':') + 1);
else if (f.startsWith("keepTypes")) keepTypes = true;
else if (f.startsWith("alwaysFetchEmbedded")) alwaysFetchEmbeddedDocuments = true;
else if (f.startsWith("dateAsLong")) dateAsLong = true;
else if (f.startsWith("prettyPrint")) prettyPrint = true;
else if (f.startsWith("graph") || f.startsWith("shallow"))
// SUPPORTED IN OTHER PARTS
;
else throw new IllegalArgumentException("Unrecognized JSON formatting option: " + f);
}
}
}
}

@Override
public int getCurrentVersion() {
return 0;
Expand Down

0 comments on commit 836cd89

Please sign in to comment.