use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class ORecordSerializerJSON method getValueAsMap.
private Object getValueAsMap(ODocument iRecord, String iFieldValue, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions, String[] fields) {
if (fields.length % 2 == 1)
throw new OSerializationException("Bad JSON format on map. Expected pairs of field:value but received '" + iFieldValue + "'");
final Map<String, Object> embeddedMap = new LinkedHashMap<String, Object>();
for (int i = 0; i < fields.length; i += 2) {
String iFieldName = fields[i];
if (iFieldName.length() >= 2)
iFieldName = iFieldName.substring(1, iFieldName.length() - 1);
iFieldValue = fields[i + 1];
final String valueAsString = OIOUtils.getStringContent(iFieldValue);
embeddedMap.put(iFieldName, getValue(iRecord, null, iFieldValue, valueAsString, iLinkedType, null, iFieldTypes, iNoMap, iOptions));
}
return embeddedMap;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class ORecordSerializerJSON method toString.
@Override
public StringBuilder toString(final ORecord iRecord, final StringBuilder iOutput, final String iFormat, final OUserObject2RecordHandler iObjHandler, boolean iOnlyDelta, boolean autoDetectCollectionType) {
try {
final StringWriter buffer = new StringWriter(INITIAL_SIZE);
final OJSONWriter json = new OJSONWriter(buffer, iFormat);
final FormatSettings settings = new FormatSettings(iFormat);
json.beginObject();
OJSONFetchContext context = new OJSONFetchContext(json, settings);
context.writeSignature(json, iRecord);
if (iRecord instanceof ODocument) {
final OFetchPlan fp = OFetchHelper.buildFetchPlan(settings.fetchPlan);
OFetchHelper.fetch(iRecord, null, fp, new OJSONFetchListener(), context, iFormat);
} else if (iRecord instanceof ORecordStringable) {
// STRINGABLE
final ORecordStringable record = (ORecordStringable) iRecord;
json.writeAttribute(settings.indentLevel, true, "value", record.value());
} else if (iRecord instanceof OBlob) {
// BYTES
final OBlob record = (OBlob) iRecord;
json.writeAttribute(settings.indentLevel, true, "value", OBase64Utils.encodeBytes(record.toStream()));
} else
throw new OSerializationException("Error on marshalling record of type '" + iRecord.getClass() + "' to JSON. The record type cannot be exported to JSON");
json.endObject(settings.indentLevel, true);
iOutput.append(buffer);
return iOutput;
} catch (IOException e) {
throw OException.wrapException(new OSerializationException("Error on marshalling of record to JSON"), e);
}
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class ORecordSerializerJSON method unwrapSource.
private String unwrapSource(String iSource) {
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 '" + iSource + "': content must be between { }");
iSource = iSource.substring(1, iSource.length() - 1).trim();
return iSource;
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class ORecordSerializerSchemaAware2CSV method writeClassOnly.
public byte[] writeClassOnly(ORecord iSource) {
final ODocument record = (ODocument) iSource;
StringBuilder iOutput = new StringBuilder();
if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
iOutput.append(ODocumentInternal.getImmutableSchemaClass(record).getStreamableName());
iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR);
}
try {
return iOutput.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw OException.wrapException(new OSerializationException("error writing class name"), e);
}
}
use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.
the class OStreamSerializerAnyRecord method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(byte[] iStream) throws IOException {
if (iStream == null || iStream.length == 0)
// NULL VALUE
return null;
final String stream = new String(iStream, "UTF-8");
Class<?> cls = null;
try {
final StringBuilder content = new StringBuilder(1024);
cls = OStreamSerializerHelper.readRecordType(stream, content);
// TRY WITH THE DATABASE CONSTRUCTOR
for (Constructor<?> c : cls.getDeclaredConstructors()) {
Class<?>[] params = c.getParameterTypes();
if (params.length == 2 && params[1].equals(ORID.class)) {
ORecord rec = (ORecord) c.newInstance(new ORecordId(content.toString()));
// rec.load();
return rec;
}
}
} catch (Exception e) {
throw OException.wrapException(new OSerializationException("Error on unmarshalling content. Class " + (cls != null ? cls.getName() : "?")), e);
}
throw new OSerializationException("Cannot unmarshall the record since the serialized object of class " + (cls != null ? cls.getSimpleName() : "?") + " has no constructor with suitable parameters: (ORID)");
}
Aggregations