Search in sources :

Example 6 with ODocumentSerializable

use of com.orientechnologies.orient.core.serialization.ODocumentSerializable in project orientdb by orientechnologies.

the class OStringSerializerEmbedded method toStream.

/**
   * Serialize the class name size + class name + object content
   * 
   * @param iValue
   */
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
    if (iValue != null) {
        if (iValue instanceof ODocumentSerializable)
            iValue = ((ODocumentSerializable) iValue).toDocument();
        if (!(iValue instanceof OSerializableStream))
            throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
        OSerializableStream stream = (OSerializableStream) iValue;
        iOutput.append(iValue.getClass().getName());
        iOutput.append(OStreamSerializerHelper.SEPARATOR);
        try {
            iOutput.append(new String(stream.toStream(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw OException.wrapException(new OSerializationException("Error serializing embedded object"), e);
        }
    }
    return iOutput;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable)

Example 7 with ODocumentSerializable

use of com.orientechnologies.orient.core.serialization.ODocumentSerializable in project orientdb by orientechnologies.

the class OStringSerializerEmbedded method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(final String iStream) {
    if (iStream == null || iStream.length() == 0)
        // NULL VALUE
        return null;
    final ODocument instance = new ODocument();
    try {
        ORecordSerializerSchemaAware2CSV.INSTANCE.fromStream(iStream.getBytes("UTF-8"), instance, null);
    } catch (UnsupportedEncodingException e) {
        throw OException.wrapException(new OSerializationException("Error decoding string"), e);
    }
    final String className = instance.field(ODocumentSerializable.CLASS_NAME);
    if (className == null)
        return instance;
    Class<?> clazz = null;
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        OLogManager.instance().debug(this, "Class name provided in embedded document " + className + " does not exist.");
    }
    if (clazz == null)
        return instance;
    if (ODocumentSerializable.class.isAssignableFrom(clazz)) {
        try {
            final ODocumentSerializable documentSerializable = (ODocumentSerializable) clazz.newInstance();
            final ODocument docClone = new ODocument();
            instance.copyTo(docClone);
            docClone.removeField(ODocumentSerializable.CLASS_NAME);
            documentSerializable.fromDocument(docClone);
            return documentSerializable;
        } catch (InstantiationException e) {
            throw OException.wrapException(new OSerializationException("Cannot serialize the object"), e);
        } catch (IllegalAccessException e) {
            throw OException.wrapException(new OSerializationException("Cannot serialize the object"), e);
        }
    }
    return instance;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 8 with ODocumentSerializable

use of com.orientechnologies.orient.core.serialization.ODocumentSerializable in project orientdb by orientechnologies.

the class ORecordSerializerBinaryV0 method serializeValue.

@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
    int pointer = 0;
    switch(type) {
        case INTEGER:
        case LONG:
        case SHORT:
            pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            break;
        case STRING:
            pointer = writeString(bytes, value.toString());
            break;
        case DOUBLE:
            long dg = Double.doubleToLongBits(((Number) value).doubleValue());
            pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
            OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
            break;
        case FLOAT:
            int fg = Float.floatToIntBits(((Number) value).floatValue());
            pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
            OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
            break;
        case BYTE:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Number) value).byteValue();
            break;
        case BOOLEAN:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
            break;
        case DATETIME:
            if (value instanceof Number) {
                pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            } else
                pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
            break;
        case DATE:
            long dateValue;
            if (value instanceof Number) {
                dateValue = ((Number) value).longValue();
            } else
                dateValue = ((Date) value).getTime();
            dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
            pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
            break;
        case EMBEDDED:
            pointer = bytes.offset;
            if (value instanceof ODocumentSerializable) {
                ODocument cur = ((ODocumentSerializable) value).toDocument();
                cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
                serialize(cur, bytes, false);
            } else {
                serialize((ODocument) value, bytes, false);
            }
            break;
        case EMBEDDEDSET:
        case EMBEDDEDLIST:
            if (value.getClass().isArray())
                pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
            else
                pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
            break;
        case DECIMAL:
            BigDecimal decimalValue = (BigDecimal) value;
            pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
            ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
            break;
        case BINARY:
            pointer = writeBinary(bytes, (byte[]) (value));
            break;
        case LINKSET:
        case LINKLIST:
            Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
            pointer = writeLinkCollection(bytes, ridCollection);
            break;
        case LINK:
            if (!(value instanceof OIdentifiable))
                throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
            pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
            break;
        case LINKMAP:
            pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
            break;
        case EMBEDDEDMAP:
            pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
            break;
        case LINKBAG:
            pointer = ((ORidBag) value).toStream(bytes);
            break;
        case CUSTOM:
            if (!(value instanceof OSerializableStream))
                value = new OSerializableWrapper((Serializable) value);
            pointer = writeString(bytes, value.getClass().getName());
            writeBinary(bytes, ((OSerializableStream) value).toStream());
            break;
        case TRANSIENT:
            break;
        case ANY:
            break;
    }
    return pointer;
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) BigDecimal(java.math.BigDecimal) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)5 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)5 OValidationException (com.orientechnologies.orient.core.exception.OValidationException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)3 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)3 OException (com.orientechnologies.common.exception.OException)2 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)2 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)2 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)2 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 BigDecimal (java.math.BigDecimal)2 Map (java.util.Map)2 OLazyIterator (com.orientechnologies.common.collection.OLazyIterator)1 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1