Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,6 @@ public void readExternal(final ObjectInput in) throws IOException {

@Override
public JSONObject toJSON(final String... includeProperties) {
return new JSONSerializer(database).map2json(propertiesAsMap(), null, includeProperties);
return new JSONSerializer(database).map2json(propertiesAsMap(), type, false, includeProperties);
}
}
16 changes: 6 additions & 10 deletions engine/src/main/java/com/arcadedb/database/DetachedDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import com.arcadedb.schema.Property;
import com.arcadedb.serializer.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static com.arcadedb.schema.Property.RID_PROPERTY;

Expand Down Expand Up @@ -93,8 +89,8 @@ public synchronized MutableDocument modify() {
public synchronized Map<String, Object> toMap(final boolean includeMetadata) {
final Map<String, Object> result = new HashMap<>(map);
if (includeMetadata) {
result.put("@cat", "d");
result.put("@type", type.getName());
result.put(Property.CAT_PROPERTY, "d");
result.put(Property.TYPE_PROPERTY, type.getName());
if (getIdentity() != null)
result.put(RID_PROPERTY, getIdentity().toString());
}
Expand All @@ -103,10 +99,10 @@ public synchronized Map<String, Object> toMap(final boolean includeMetadata) {

@Override
public synchronized JSONObject toJSON(final boolean includeMetadata) {
final JSONObject result = new JSONSerializer(database).map2json(map, null);
final JSONObject result = new JSONSerializer(database).map2json(map, type, includeMetadata);
if (includeMetadata) {
result.put("@cat", "d");
result.put("@type", type.getName());
result.put(Property.CAT_PROPERTY, "d");
result.put(Property.TYPE_PROPERTY, type.getName());
if (getIdentity() != null)
result.put(RID_PROPERTY, getIdentity().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public JSONObject toJSON(final boolean includeMetadata) {
checkForLazyLoading();
final Map<String, Object> map = database.getSerializer()
.deserializeProperties(database, buffer, new EmbeddedModifierObject(this), rid);
final JSONObject result = new JSONSerializer(database).map2json(map, null);
final JSONObject result = new JSONSerializer(database).map2json(map, type, includeMetadata);
if (includeMetadata) {
result.put(CAT_PROPERTY, "d");
result.put(TYPE_PROPERTY, type.getName());
Expand Down
53 changes: 31 additions & 22 deletions engine/src/main/java/com/arcadedb/database/JSONSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,15 @@
import com.arcadedb.log.LogManager;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.LocalVertexType;
import com.arcadedb.schema.Property;
import com.arcadedb.schema.Type;
import com.arcadedb.serializer.json.JSONArray;
import com.arcadedb.serializer.json.JSONObject;
import com.arcadedb.utility.DateUtils;

import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.time.temporal.*;
import java.util.*;
import java.util.logging.*;

public class JSONSerializer {
private final Database database;
Expand All @@ -48,7 +39,8 @@ public JSONSerializer(final Database database) {
this.database = database;
}

public JSONObject map2json(final Map<String, Object> map, final DocumentType type, final String... includeProperties) {
public JSONObject map2json(final Map<String, Object> map, final DocumentType type, final boolean includeMetadata,
final String... includeProperties) {
final JSONObject json = new JSONObject();

final Set<String> includePropertiesSet;
Expand All @@ -57,28 +49,45 @@ public JSONObject map2json(final Map<String, Object> map, final DocumentType typ
else
includePropertiesSet = null;

final StringBuilder propertyTypes = includeMetadata ? new StringBuilder() : null;

for (final Map.Entry<String, Object> entry : map.entrySet()) {
if (includePropertiesSet != null && !includePropertiesSet.contains(entry.getKey()))
final String propertyName = entry.getKey();

if (includePropertiesSet != null && !includePropertiesSet.contains(propertyName))
continue;

Object value = entry.getValue();

final Type propertyType;
if (type != null && type.existsProperty(entry.getKey()))
propertyType = type.getProperty(entry.getKey()).getType();
if (type != null && type.existsProperty(propertyName))
propertyType = type.getProperty(propertyName).getType();
else if (value != null)
propertyType = Type.getTypeByClass(value.getClass());
else
propertyType = null;

final Object value = convertToJSONType(entry.getValue(), propertyType);
if (includeMetadata && propertyType != null) {
if (propertyTypes.length() > 0)
propertyTypes.append(",");
propertyTypes.append(propertyName).append(":").append(propertyType.getId());
}

value = convertToJSONType(value, propertyType);

if (value instanceof Number number && !Float.isFinite(number.floatValue())) {
LogManager.instance()
.log(this, Level.SEVERE, "Found non finite number in map with key '%s', ignore this entry in the conversion",
entry.getKey());
propertyName);
continue;
}

json.put(entry.getKey(), value);
json.put(propertyName, value);
}

if (propertyTypes != null && !propertyTypes.isEmpty())
json.put(Property.PROPERTY_TYPES_PROPERTY, propertyTypes);

return json;
}

Expand All @@ -98,7 +107,7 @@ private Object convertToJSONType(Object value) {

private Object convertToJSONType(Object value, final Type type) {
if (value instanceof Document document) {
value = document.toJSON();
value = document.toJSON(true);
} else if (value instanceof Collection c) {
final JSONArray array = new JSONArray();
for (final Iterator it = c.iterator(); it.hasNext(); )
Expand All @@ -121,7 +130,7 @@ else if (value instanceof Map) {

private Object convertFromJSONType(Object value) {
if (value instanceof JSONObject json) {
final String embeddedTypeName = json.getString("@type");
final String embeddedTypeName = json.getString(Property.TYPE_PROPERTY);

final DocumentType type = database.getSchema().getType(embeddedTypeName);

Expand Down
14 changes: 4 additions & 10 deletions engine/src/main/java/com/arcadedb/database/MutableDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@
import com.arcadedb.schema.Type;
import com.arcadedb.serializer.json.JSONObject;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.*;
import java.util.*;

import static com.arcadedb.schema.Property.CAT_PROPERTY;
import static com.arcadedb.schema.Property.RID_PROPERTY;
Expand Down Expand Up @@ -121,7 +115,7 @@ public MutableDocument fromJSON(final JSONObject json) {
@Override
public JSONObject toJSON(final boolean includeMetadata) {
checkForLazyLoadingProperties();
final JSONObject result = new JSONSerializer(database).map2json(map, type);
final JSONObject result = new JSONSerializer(database).map2json(map, type, includeMetadata);
if (includeMetadata) {
result.put(CAT_PROPERTY, "d");
result.put(TYPE_PROPERTY, type.getName());
Expand Down Expand Up @@ -407,7 +401,6 @@ protected Object convertValueToSchemaType(final String name, final Object value,
if (property != null) {
try {
final Type propType = property.getType();
final String ofType = property.getOfType();

final Class javaImplementation;
if (propType == Type.DATE)
Expand All @@ -423,6 +416,7 @@ else if (propType == Type.DATETIME)
final Map<String, Object> map = (Map<String, Object>) value;
final String embType = (String) map.get("@type");

final String ofType = property.getOfType();
if (ofType != null) {
// VALIDATE CONSTRAINT
if (!ofType.equals(embType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public JSONArray toJSON() {

EdgeSegment current = lastSegment;
while (current != null) {
final JSONObject j = current.toJSON();
final JSONObject j = current.toJSON(false);
if (j.has("array")) {
final JSONArray a = j.getJSONArray("array");
for (int i = 0; i < a.length(); ++i)
Expand Down
13 changes: 7 additions & 6 deletions engine/src/main/java/com/arcadedb/graph/ImmutableEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.arcadedb.exception.DatabaseOperationException;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.EdgeType;
import com.arcadedb.schema.Property;
import com.arcadedb.serializer.BinaryTypes;
import com.arcadedb.serializer.json.JSONObject;

Expand Down Expand Up @@ -89,9 +90,9 @@ else if (!database.getTransaction().hasPageForRecord(rid.getPageId())) {

@Override
public synchronized Object get(final String propertyName) {
if ("@in".equals(propertyName))
if (Property.IN_PROPERTY.equals(propertyName))
return in;
else if ("@out".equals(propertyName))
else if (Property.OUT_PROPERTY.equals(propertyName))
return out;
return super.get(propertyName);
}
Expand Down Expand Up @@ -150,9 +151,9 @@ public Edge asEdge(final boolean loadContent) {
public synchronized Map<String, Object> toMap(final boolean includeMetadata) {
final Map<String, Object> map = super.toMap(includeMetadata);
if (includeMetadata) {
map.put("@cat", "e");
map.put("@in", in);
map.put("@out", out);
map.put(Property.CAT_PROPERTY, "e");
map.put(Property.IN_PROPERTY, in);
map.put(Property.OUT_PROPERTY, out);
}
return map;
}
Expand All @@ -161,7 +162,7 @@ public synchronized Map<String, Object> toMap(final boolean includeMetadata) {
public synchronized JSONObject toJSON(final boolean includeMetadata) {
final JSONObject json = super.toJSON(includeMetadata);
if (includeMetadata)
json.put("@cat", "e").put("@in", in).put("@out", out);
json.put(Property.CAT_PROPERTY, "e").put(Property.IN_PROPERTY, in).put(Property.OUT_PROPERTY, out);
return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.arcadedb.database.ImmutableDocument;
import com.arcadedb.database.RID;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.Property;
import com.arcadedb.serializer.json.JSONObject;

import java.util.*;
Expand Down Expand Up @@ -112,7 +113,7 @@ public Edge asEdge(final boolean loadContent) {
public JSONObject toJSON(final boolean includeMetadata) {
final JSONObject json = super.toJSON(includeMetadata);
if (includeMetadata)
json.put("@cat", "e").put("@in", in).put("@out", out);
json.put(Property.CAT_PROPERTY, "e").put("@in", in).put("@out", out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It seems @in and @out are still hardcoded here, while @cat has been updated to use Property.CAT_PROPERTY. For consistency with other classes and to leverage the benefits of constants, should @in and @out also be replaced with Property.IN_PROPERTY and Property.OUT_PROPERTY respectively?

return json;
}

Expand Down
5 changes: 3 additions & 2 deletions engine/src/main/java/com/arcadedb/graph/ImmutableVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.arcadedb.exception.DatabaseOperationException;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.EdgeType;
import com.arcadedb.schema.Property;
import com.arcadedb.schema.VertexType;
import com.arcadedb.serializer.json.JSONObject;

Expand Down Expand Up @@ -212,15 +213,15 @@ public Vertex asVertex(final boolean loadContent) {
public Map<String, Object> toMap(final boolean includeMetadata) {
final Map<String, Object> map = super.toMap(includeMetadata);
if (includeMetadata)
map.put("@cat", "v");
map.put(Property.CAT_PROPERTY, "v");
return map;
}

@Override
public JSONObject toJSON(final boolean includeMetadata) {
final JSONObject json = super.toJSON(includeMetadata);
if (includeMetadata)
json.put("@cat", "v");
json.put(Property.CAT_PROPERTY, "v");
return json;
}

Expand Down
5 changes: 3 additions & 2 deletions engine/src/main/java/com/arcadedb/graph/MutableEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.arcadedb.database.RID;
import com.arcadedb.database.Transaction;
import com.arcadedb.schema.EdgeType;
import com.arcadedb.schema.Property;
import com.arcadedb.serializer.BinaryTypes;
import com.arcadedb.serializer.json.JSONObject;

Expand Down Expand Up @@ -187,7 +188,7 @@ public void setIn(final RID in) {
public Map<String, Object> toMap(final boolean includeMetadata) {
final Map<String, Object> map = super.toMap(includeMetadata);
if (includeMetadata) {
map.put("@cat", "e");
map.put(Property.CAT_PROPERTY, "e");
map.put("@in", in);
map.put("@out", out);
Comment on lines +191 to 193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to other graph classes, @cat has been updated to use Property.CAT_PROPERTY. However, @in and @out are still hardcoded strings here. Would it be better to use Property.IN_PROPERTY and Property.OUT_PROPERTY for consistency and maintainability?

}
Expand All @@ -197,7 +198,7 @@ public Map<String, Object> toMap(final boolean includeMetadata) {
public JSONObject toJSON(final boolean includeMetadata) {
JSONObject json = super.toJSON(includeMetadata);
if (includeMetadata)
json.put("@cat", "e").put("@in", in).put("@out", out);
json.put(Property.CAT_PROPERTY, "e").put("@in", in).put("@out", out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consistent with the toMap method and other classes, @in and @out are hardcoded here while @cat uses a constant. Should these also be updated to use Property.IN_PROPERTY and Property.OUT_PROPERTY?

return json;
}

Expand Down
5 changes: 3 additions & 2 deletions engine/src/main/java/com/arcadedb/graph/MutableVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.arcadedb.database.RID;
import com.arcadedb.database.Transaction;
import com.arcadedb.schema.EdgeType;
import com.arcadedb.schema.Property;
import com.arcadedb.schema.VertexType;
import com.arcadedb.serializer.json.JSONObject;

Expand Down Expand Up @@ -222,15 +223,15 @@ public Vertex asVertex(final boolean loadContent) {
public Map<String, Object> toMap(final boolean includeMetadata) {
final Map<String, Object> map = super.toMap(includeMetadata);
if (includeMetadata)
map.put("@cat", "v");
map.put(Property.CAT_PROPERTY, "v");
return map;
}

@Override
public JSONObject toJSON(final boolean includeMetadata) {
final JSONObject json = super.toJSON(includeMetadata);
if (includeMetadata)
json.put("@cat", "v");
json.put(Property.CAT_PROPERTY, "v");
return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ default JSONObject toJSON() {

final JSONObject result = new JSONObject();
for (final String prop : getPropertyNames())
result.put(prop, toJson(getProperty(prop)));
result.put(prop, valueToJSON(getProperty(prop)));

return result;
}

default Object toJson(final Object val) {
default Object valueToJSON(final Object val) {
if (val != null) {
if (val instanceof Result result) {
return result.toJSON();
Expand All @@ -125,12 +125,12 @@ default Object toJson(final Object val) {
} else if (val instanceof Iterable<?> iterable) {
final JSONArray array = new JSONArray();
for (final Object o : iterable)
array.put(toJson(o));
array.put(valueToJSON(o));
return array;
} else if (val instanceof Iterator<?> iterator) {
final JSONArray array = new JSONArray();
while (iterator.hasNext())
array.put(toJson(iterator.next()));
array.put(valueToJSON(iterator.next()));
return array;
} else if (val instanceof Map) {
return new JSONObject((Map<String, Object>) val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.arcadedb.database.Identifiable;
import com.arcadedb.database.RID;
import com.arcadedb.database.Record;
import com.arcadedb.schema.Property;

import java.util.*;
import java.util.stream.*;
Expand Down Expand Up @@ -182,7 +183,7 @@ public static Object wrap(final Object input) {
final Document elem = document;
final ResultInternal result = new ResultInternal(elem.toMap(false));
if (elem.getTypeName() != null)
result.setProperty("@type", elem.getTypeName());
result.setProperty(Property.TYPE_PROPERTY, elem.getTypeName());
return result;

} else if (input instanceof List list) {
Expand Down
Loading
Loading