Search in sources :

Example 6 with OSerializationException

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;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with OSerializationException

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);
    }
}
Also used : OJSONFetchContext(com.orientechnologies.orient.core.fetch.json.OJSONFetchContext) StringWriter(java.io.StringWriter) OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecordStringable(com.orientechnologies.orient.core.record.ORecordStringable) IOException(java.io.IOException) OFetchPlan(com.orientechnologies.orient.core.fetch.OFetchPlan) OJSONFetchListener(com.orientechnologies.orient.core.fetch.json.OJSONFetchListener)

Example 8 with OSerializationException

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;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 9 with OSerializationException

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);
    }
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 10 with OSerializationException

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)");
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Aggregations

OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)34 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)11 OException (com.orientechnologies.common.exception.OException)10 IOException (java.io.IOException)9 OType (com.orientechnologies.orient.core.metadata.schema.OType)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 ORecord (com.orientechnologies.orient.core.record.ORecord)6 ORID (com.orientechnologies.orient.core.id.ORID)5 ORecordId (com.orientechnologies.orient.core.id.ORecordId)5 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ParseException (java.text.ParseException)3