Search in sources :

Example 1 with ORecordStringable

use of com.orientechnologies.orient.core.record.ORecordStringable 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 2 with ORecordStringable

use of com.orientechnologies.orient.core.record.ORecordStringable in project orientdb by orientechnologies.

the class ORecordSerializerJSON method fromString.

public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, final String iOptions, boolean needReload) {
    iSource = unwrapSource(iSource);
    boolean noMap = false;
    if (iOptions != null) {
        final String[] format = iOptions.split(",");
        for (String f : format) if (f.equalsIgnoreCase("noMap"))
            noMap = true;
    }
    if (iRecord != null)
        // RESET ALL THE FIELDS
        iRecord.clear();
    final List<String> fields = OStringSerializerHelper.smartSplit(iSource, PARAMETER_SEPARATOR, 0, -1, true, true, false, false, ' ', '\n', '\r', '\t');
    if (fields.size() % 2 != 0)
        throw new OSerializationException("Error on unmarshalling JSON content: wrong format \"" + iSource + "\". Use <field> : <value>");
    Map<String, Character> fieldTypes = null;
    if (fields != null && fields.size() > 0) {
        // SEARCH FOR FIELD TYPES IF ANY
        for (int i = 0; i < fields.size(); i += 2) {
            final String fieldName = OIOUtils.getStringContent(fields.get(i));
            final String fieldValue = fields.get(i + 1);
            final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
            if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
                fieldTypes = loadFieldTypes(fieldTypes, fieldValueAsString);
            } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
                if (iRecord == null || ORecordInternal.getRecordType(iRecord) != fieldValueAsString.charAt(0)) {
                    // CREATE THE RIGHT RECORD INSTANCE
                    iRecord = Orient.instance().getRecordFactoryManager().newInstance((byte) fieldValueAsString.charAt(0));
                }
            } else if (needReload && fieldName.equals(ODocumentHelper.ATTRIBUTE_RID) && iRecord instanceof ODocument) {
                if (fieldValue != null && fieldValue.length() > 0) {
                    ORecord localRecord = ODatabaseRecordThreadLocal.INSTANCE.get().load(new ORecordId(fieldValueAsString));
                    if (localRecord != null)
                        iRecord = localRecord;
                }
            } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS) && iRecord instanceof ODocument) {
                ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord), "null".equals(fieldValueAsString) ? null : fieldValueAsString);
            }
        }
        if (iRecord == null)
            iRecord = new ODocument();
        try {
            for (int i = 0; i < fields.size(); i += 2) {
                final String fieldName = OIOUtils.getStringContent(fields.get(i));
                final String fieldValue = fields.get(i + 1);
                final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
                // RECORD ATTRIBUTES
                if (fieldName.equals(ODocumentHelper.ATTRIBUTE_RID))
                    ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString));
                else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION))
                    ORecordInternal.setVersion(iRecord, Integer.parseInt(fieldValue));
                else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
                    continue;
                } else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
                    continue;
                } else if (fieldName.equals("value") && !(iRecord instanceof ODocument)) {
                    // RECORD VALUE(S)
                    if ("null".equals(fieldValue))
                        iRecord.fromStream(OCommonConst.EMPTY_BYTE_ARRAY);
                    else if (iRecord instanceof OBlob) {
                        // BYTES
                        iRecord.fromStream(OBase64Utils.decode(fieldValueAsString));
                    } else if (iRecord instanceof ORecordStringable) {
                        ((ORecordStringable) iRecord).value(fieldValueAsString);
                    } else
                        throw new IllegalArgumentException("unsupported type of record");
                } else if (iRecord instanceof ODocument) {
                    final ODocument doc = ((ODocument) iRecord);
                    // DETERMINE THE TYPE FROM THE SCHEMA
                    OType type = determineType(doc, fieldName);
                    final Object v = getValue(doc, fieldName, fieldValue, fieldValueAsString, type, null, fieldTypes, noMap, iOptions);
                    if (v != null)
                        if (v instanceof Collection<?> && !((Collection<?>) v).isEmpty()) {
                            if (v instanceof ORecordLazyMultiValue)
                                ((ORecordLazyMultiValue) v).setAutoConvertToRecord(false);
                            // CHECK IF THE COLLECTION IS EMBEDDED
                            if (type == null) {
                                // TRY TO UNDERSTAND BY FIRST ITEM
                                Object first = ((Collection<?>) v).iterator().next();
                                if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid())
                                    type = v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST;
                            }
                            if (type != null) {
                                // TREAT IT AS EMBEDDED
                                doc.field(fieldName, v, type);
                                continue;
                            }
                        } else if (v instanceof Map<?, ?> && !((Map<?, ?>) v).isEmpty()) {
                            // CHECK IF THE MAP IS EMBEDDED
                            Object first = ((Map<?, ?>) v).values().iterator().next();
                            if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid()) {
                                doc.field(fieldName, v, OType.EMBEDDEDMAP);
                                continue;
                            }
                        } else if (v instanceof ODocument && type != null && type.isLink()) {
                            String className = ((ODocument) v).getClassName();
                            if (className != null && className.length() > 0)
                                ((ODocument) v).save();
                        }
                    if (type == null && fieldTypes != null && fieldTypes.containsKey(fieldName))
                        type = ORecordSerializerStringAbstract.getType(fieldValue, fieldTypes.get(fieldName));
                    if (v instanceof OTrackedSet<?>) {
                        if (OMultiValue.getFirstValue((Set<?>) v) instanceof OIdentifiable)
                            type = OType.LINKSET;
                    } else if (v instanceof OTrackedList<?>) {
                        if (OMultiValue.getFirstValue((List<?>) v) instanceof OIdentifiable)
                            type = OType.LINKLIST;
                    }
                    if (type != null)
                        doc.field(fieldName, v, type);
                    else
                        doc.field(fieldName, v);
                }
            }
        } catch (Exception e) {
            if (iRecord.getIdentity().isValid())
                throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record " + iRecord.getIdentity()), e);
            else
                throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record: " + iSource), e);
        }
    }
    return iRecord;
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) List(java.util.List) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordStringable(com.orientechnologies.orient.core.record.ORecordStringable) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) ParseException(java.text.ParseException) IOException(java.io.IOException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) ORecord(com.orientechnologies.orient.core.record.ORecord) Collection(java.util.Collection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)2 ORecordStringable (com.orientechnologies.orient.core.record.ORecordStringable)2 IOException (java.io.IOException)2 OException (com.orientechnologies.common.exception.OException)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)1 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)1 OFetchPlan (com.orientechnologies.orient.core.fetch.OFetchPlan)1 OJSONFetchContext (com.orientechnologies.orient.core.fetch.json.OJSONFetchContext)1 OJSONFetchListener (com.orientechnologies.orient.core.fetch.json.OJSONFetchListener)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OJSONWriter (com.orientechnologies.orient.core.serialization.serializer.OJSONWriter)1 StringWriter (java.io.StringWriter)1 ParseException (java.text.ParseException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1