Skip to content

Commit

Permalink
Refactor "map2json" (#1790)
Browse files Browse the repository at this point in the history
* Refactor map2json method

* Adapt callers
  • Loading branch information
gramian authored Oct 28, 2024
1 parent a5b6d58 commit c63bb08
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ public void readExternal(final ObjectInput in) throws IOException {

@Override
public JSONObject toJSON(final String... includeProperties) {
return new JSONSerializer(database).map2json(propertiesAsMap(), includeProperties);
return new JSONSerializer(database).map2json(propertiesAsMap(), null, includeProperties);
}
}
10 changes: 4 additions & 6 deletions engine/src/main/java/com/arcadedb/database/JSONSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ public JSONSerializer(final Database database) {
this.database = database;
}

public JSONObject map2json(final Map<String, Object> map, final String... includeProperties) {
return map2json(map, null, includeProperties);
}

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

final Set<String> includePropertiesSet;
if (includeProperties != null && includeProperties.length > 0) {
if (includeProperties.length > 0) {
includePropertiesSet = new HashSet<>();
for (String p : includeProperties)
includePropertiesSet.add(p);
Expand All @@ -58,9 +54,11 @@ public JSONObject map2json(final Map<String, Object> map, final DocumentType typ
if (includePropertiesSet != null && !includePropertiesSet.contains(entry.getKey()))
continue;

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

final Object value = convertToJSONType(entry.getValue(), propertyType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public MutableDocument fromJSON(final JSONObject json) {
@Override
public JSONObject toJSON(final boolean includeMetadata) {
checkForLazyLoadingProperties();
final JSONObject result = new JSONSerializer(database).map2json(map, type, null);
final JSONObject result = new JSONSerializer(database).map2json(map, type);
if (includeMetadata) {
result.put("@cat", "d");
result.put("@type", type.getName());
Expand Down

0 comments on commit c63bb08

Please sign in to comment.