}
@Override
public ORecordInternal<?> fromString(final ODatabaseRecord iDatabase, String iSource, ORecordInternal<?> iRecord) {
if (iSource == null)
throw new OSerializationException("Error on unmarshalling JSON content: content is null");
iSource = iSource.trim();
if (!iSource.startsWith("{") || !iSource.endsWith("}"))
throw new OSerializationException("Error on unmarshalling JSON content: content must be between { }");
if (iRecord != null)
// RESET ALL THE FIELDS
iRecord.reset();
iSource = iSource.substring(1, iSource.length() - 1).trim();
String[] fields = OStringParser.getWords(iSource, ":,", true);
String fieldName;
String fieldValue;
String fieldValueAsString;
Map<String, Character> fieldTypes = null;
// SEARCH FOR FIELD TYPES IF ANY
if (fields != null && fields.length > 0) {
for (int i = 0; i < fields.length; i += 2) {
fieldName = fields[i];
fieldName = fieldName.substring(1, fieldName.length() - 1);
fieldValue = fields[i + 1];
fieldValueAsString = fieldValue.length() >= 2 ? fieldValue.substring(1, fieldValue.length() - 1) : fieldValue;
if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
// LOAD THE FIELD TYPE MAP
final String[] fieldTypesParts = fieldValueAsString.split(",");
if (fieldTypesParts.length > 0) {
fieldTypes = new HashMap<String, Character>();
String[] part;
for (String f : fieldTypesParts) {
part = f.split("=");
if (part.length == 2)
fieldTypes.put(part[0], part[1].charAt(0));
}
}
} else if (fieldName.equals(ATTRIBUTE_TYPE)) {
if (iRecord == null || iRecord.getRecordType() != fieldValueAsString.charAt(0)) {
// CREATE THE RIGHT RECORD INSTANCE
iRecord = ORecordFactory.newInstance((byte) fieldValueAsString.charAt(0));
iRecord.setDatabase(iDatabase);
}
}
}
try {
for (int i = 0; i < fields.length; i += 2) {
fieldName = fields[i];
fieldName = fieldName.substring(1, fieldName.length() - 1);
fieldValue = fields[i + 1].trim();
fieldValueAsString = fieldValue.length() >= 2 ? fieldValue.substring(1, fieldValue.length() - 1) : fieldValue;
// RECORD ATTRIBUTES
if (fieldName.equals(ATTRIBUTE_ID))
iRecord.setIdentity(new ORecordId(fieldValueAsString));
else if (fieldName.equals(ATTRIBUTE_VERSION))
iRecord.setVersion(Integer.parseInt(fieldValue));
else if (fieldName.equals(ATTRIBUTE_TYPE)) {
continue;
} else if (fieldName.equals(ATTRIBUTE_CLASS) && iRecord instanceof ODocument)
((ODocument) iRecord).setClassNameIfExists("null".equals(fieldValueAsString) ? null : fieldValueAsString);
else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument)
// JUMP IT
continue;
// RECORD VALUE(S)
else if (fieldName.equals("value") && !(iRecord instanceof ODocument)) {
if ("null".equals(fieldValue))
iRecord.fromStream(new byte[] {});
else if (iRecord instanceof ORecordBytes) {
// BYTES
iRecord.fromStream(OBase64Utils.decode(fieldValueAsString));
} else if (iRecord instanceof ORecordStringable) {
((ORecordStringable) iRecord).value(fieldValueAsString);
}
} else {
if (iRecord instanceof ODocument) {
final Object v = getValue((ODocument) iRecord, fieldName, fieldValue, fieldValueAsString, null, null, fieldTypes);
if (v != null)
if (v instanceof Collection<?> && ((Collection<?>) v).size() > 0) {
if (v instanceof ORecordLazySet)
((ORecordLazySet) v).setAutoConvertToRecord(false);
else if (v instanceof ORecordLazyList)
((ORecordLazyList) v).setAutoConvertToRecord(false);
// CHECK IF THE COLLECTION IS EMBEDDED
Object first = ((Collection<?>) v).iterator().next();
if (first != null && first instanceof ORecord<?> && !((ORecord<?>) first).getIdentity().isValid()) {
((ODocument) iRecord).field(fieldName, v, v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST);
continue;
}
} else if (v instanceof Map<?, ?> && ((Map<?, ?>) v).size() > 0) {
// CHECK IF THE MAP IS EMBEDDED
Object first = ((Map<?, ?>) v).values().iterator().next();
if (first != null && first instanceof ORecord<?> && !((ORecord<?>) first).getIdentity().isValid()) {
((ODocument) iRecord).field(fieldName, v, OType.EMBEDDEDMAP);
continue;
}
}
((ODocument) iRecord).field(fieldName, v);
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new OSerializationException("Error on unmarshalling JSON content for record " + iRecord.getIdentity(), e);
}
}
return iRecord;
}