Skip to content

Commit

Permalink
chore: added detailed exception in case elements in map or array are …
Browse files Browse the repository at this point in the history
…not serializable
  • Loading branch information
lvca committed Jun 18, 2024
1 parent 7b8a37a commit c84ce50
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

import java.util.*;

/**
* Detached document instances are generated from a document and can be accessed outside a transaction.
*
* @author Luca Garulli ([email protected])
*/
public class DetachedDocument extends ImmutableDocument {
private Map<String, Object> map;

Expand Down
60 changes: 37 additions & 23 deletions engine/src/main/java/com/arcadedb/serializer/BinarySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,43 +452,57 @@ else if (value instanceof LocalDate)
content.putUnsignedNumber(length);
for (int i = 0; i < length; ++i) {
final Object entryValue = Array.get(value, i);
final byte entryType = BinaryTypes.getTypeFromValue(entryValue);
content.putByte(entryType);
serializeValue(database, content, entryType, entryValue);
try {
final byte entryType = BinaryTypes.getTypeFromValue(entryValue);
content.putByte(entryType);
serializeValue(database, content, entryType, entryValue);
} catch (Exception e) {
LogManager.instance().log(this, Level.SEVERE, "Error on serializing array value for element %d = '%s'",
i, entryValue);
throw new SerializationException(
"Error on serializing array value for element " + i + " = '" + entryValue + "'");
}
}
}
break;
}
case BinaryTypes.TYPE_MAP: {
final Dictionary dictionary = database.getSchema().getDictionary();

if( value instanceof JSONObject)
if (value instanceof JSONObject)
value = ((JSONObject) value).toMap();

final Map<Object, Object> map = (Map<Object, Object>) value;
content.putUnsignedNumber(map.size());
for (final Map.Entry<Object, Object> entry : map.entrySet()) {
// WRITE THE KEY
Object entryKey = entry.getKey();
byte entryKeyType = BinaryTypes.getTypeFromValue(entryKey);

if (entryKey != null && entryKeyType == BinaryTypes.TYPE_STRING) {
final int id = dictionary.getIdByName((String) entryKey, false);
if (id > -1) {
// WRITE THE COMPRESSED STRING AS MAP KEY
entryKeyType = BinaryTypes.TYPE_COMPRESSED_STRING;
entryKey = id;
try {
// WRITE THE KEY
Object entryKey = entry.getKey();
byte entryKeyType = BinaryTypes.getTypeFromValue(entryKey);

if (entryKey != null && entryKeyType == BinaryTypes.TYPE_STRING) {
final int id = dictionary.getIdByName((String) entryKey, false);
if (id > -1) {
// WRITE THE COMPRESSED STRING AS MAP KEY
entryKeyType = BinaryTypes.TYPE_COMPRESSED_STRING;
entryKey = id;
}
}
}

content.putByte(entryKeyType);
serializeValue(database, content, entryKeyType, entryKey);

// WRITE THE VALUE
final Object entryValue = entry.getValue();
final byte entryValueType = BinaryTypes.getTypeFromValue(entryValue);
content.putByte(entryValueType);
serializeValue(database, content, entryValueType, entryValue);
content.putByte(entryKeyType);
serializeValue(database, content, entryKeyType, entryKey);

// WRITE THE VALUE
final Object entryValue = entry.getValue();
final byte entryValueType = BinaryTypes.getTypeFromValue(entryValue);
content.putByte(entryValueType);
serializeValue(database, content, entryValueType, entryValue);
} catch (Exception e) {
LogManager.instance().log(this, Level.SEVERE, "Error on serializing map value for key '%s' = '%s'",
entry.getKey(), entry.getValue());
throw new SerializationException(
"Error on serializing map value for key '" + entry.getKey() + "' = '" + entry.getValue() + "'");
}
}
break;
}
Expand Down

0 comments on commit c84ce50

Please sign in to comment.