From c63bb08b21cdc8b3384b97ef0292b75a4ca8397f Mon Sep 17 00:00:00 2001 From: Christian Himpe Date: Tue, 29 Oct 2024 00:50:53 +0100 Subject: [PATCH] Refactor "map2json" (#1790) * Refactor map2json method * Adapt callers --- .../main/java/com/arcadedb/database/BaseDocument.java | 2 +- .../java/com/arcadedb/database/JSONSerializer.java | 10 ++++------ .../java/com/arcadedb/database/MutableDocument.java | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/engine/src/main/java/com/arcadedb/database/BaseDocument.java b/engine/src/main/java/com/arcadedb/database/BaseDocument.java index a6564580c..fe6d9fe16 100644 --- a/engine/src/main/java/com/arcadedb/database/BaseDocument.java +++ b/engine/src/main/java/com/arcadedb/database/BaseDocument.java @@ -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); } } diff --git a/engine/src/main/java/com/arcadedb/database/JSONSerializer.java b/engine/src/main/java/com/arcadedb/database/JSONSerializer.java index d77a256b4..98a0ad15b 100644 --- a/engine/src/main/java/com/arcadedb/database/JSONSerializer.java +++ b/engine/src/main/java/com/arcadedb/database/JSONSerializer.java @@ -39,15 +39,11 @@ public JSONSerializer(final Database database) { this.database = database; } - public JSONObject map2json(final Map map, final String... includeProperties) { - return map2json(map, null, includeProperties); - } - public JSONObject map2json(final Map map, final DocumentType type, final String... includeProperties) { final JSONObject json = new JSONObject(); final Set includePropertiesSet; - if (includeProperties != null && includeProperties.length > 0) { + if (includeProperties.length > 0) { includePropertiesSet = new HashSet<>(); for (String p : includeProperties) includePropertiesSet.add(p); @@ -58,9 +54,11 @@ public JSONObject map2json(final Map 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); diff --git a/engine/src/main/java/com/arcadedb/database/MutableDocument.java b/engine/src/main/java/com/arcadedb/database/MutableDocument.java index acd9bf60d..132046f90 100644 --- a/engine/src/main/java/com/arcadedb/database/MutableDocument.java +++ b/engine/src/main/java/com/arcadedb/database/MutableDocument.java @@ -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());