Search in sources :

Example 1 with OJSONFetchContext

use of com.orientechnologies.orient.core.fetch.json.OJSONFetchContext 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 OJSONFetchContext

use of com.orientechnologies.orient.core.fetch.json.OJSONFetchContext in project orientdb by orientechnologies.

the class OFetchHelper method fetchCollection.

@SuppressWarnings("unchecked")
private static void fetchCollection(final ODocument iRootRecord, final Object iUserObject, final OFetchPlan iFetchPlan, final Object fieldValue, final String fieldName, final int iCurrentLevel, final int iLevelFromRoot, final int iFieldDepthLevel, final Map<ORID, Integer> parsedRecords, final String iFieldPathFromRoot, final OFetchListener iListener, final OFetchContext iContext) throws IOException {
    final Iterable<?> linked;
    if (fieldValue instanceof Iterable<?> || fieldValue instanceof ORidBag) {
        linked = (Iterable<OIdentifiable>) fieldValue;
        iContext.onBeforeCollection(iRootRecord, fieldName, iUserObject, (Iterable) linked);
    } else if (fieldValue.getClass().isArray()) {
        linked = OMultiValue.getMultiValueIterable(fieldValue, false);
        iContext.onBeforeCollection(iRootRecord, fieldName, iUserObject, (Iterable) linked);
    } else if (fieldValue instanceof Map<?, ?>) {
        linked = (Collection<?>) ((Map<?, ?>) fieldValue).values();
        iContext.onBeforeMap(iRootRecord, fieldName, iUserObject);
    } else
        throw new IllegalStateException("Unrecognized type: " + fieldValue.getClass());
    final Iterator<?> iter;
    if (linked instanceof ORecordLazyMultiValue)
        iter = ((ORecordLazyMultiValue) linked).rawIterator();
    else
        iter = linked.iterator();
    try {
        while (iter.hasNext()) {
            final Object o = iter.next();
            if (o == null)
                continue;
            if (o instanceof OIdentifiable) {
                OIdentifiable d = (OIdentifiable) o;
                // GO RECURSIVELY
                final Integer fieldDepthLevel = parsedRecords.get(d.getIdentity());
                if (!d.getIdentity().isPersistent() || (fieldDepthLevel != null && fieldDepthLevel.intValue() == iLevelFromRoot)) {
                    removeParsedFromMap(parsedRecords, d);
                    d = d.getRecord();
                    if (d == null)
                        iListener.processStandardField(null, d, null, iContext, iUserObject, "", null);
                    else if (!(d instanceof ODocument)) {
                        iListener.processStandardField(null, d, fieldName, iContext, iUserObject, "", null);
                    } else {
                        iContext.onBeforeDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
                        final Object userObject = iListener.fetchLinkedCollectionValue(iRootRecord, iUserObject, fieldName, (ODocument) d, iContext);
                        processRecord((ODocument) d, userObject, iFetchPlan, iCurrentLevel, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext, "");
                        iContext.onAfterDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
                    }
                } else {
                    iListener.parseLinkedCollectionValue(iRootRecord, d, iUserObject, fieldName, iContext);
                }
            } else if (o instanceof Map<?, ?>) {
                fetchMap(iRootRecord, iUserObject, iFetchPlan, o, null, iCurrentLevel + 1, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext);
            } else if (OMultiValue.isMultiValue(o)) {
                fetchCollection(iRootRecord, iUserObject, iFetchPlan, o, null, iCurrentLevel + 1, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext);
            } else if (o instanceof String || o instanceof Number || o instanceof Boolean) {
                ((OJSONFetchContext) iContext).getJsonWriter().writeValue(0, false, o);
            }
        }
    } finally {
        if (fieldValue instanceof Iterable<?> || fieldValue instanceof ORidBag)
            iContext.onAfterCollection(iRootRecord, fieldName, iUserObject);
        else if (fieldValue.getClass().isArray())
            iContext.onAfterCollection(iRootRecord, fieldName, iUserObject);
        else if (fieldValue instanceof Map<?, ?>)
            iContext.onAfterMap(iRootRecord, fieldName, iUserObject);
    }
}
Also used : OJSONFetchContext(com.orientechnologies.orient.core.fetch.json.OJSONFetchContext) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OJSONFetchContext (com.orientechnologies.orient.core.fetch.json.OJSONFetchContext)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 OFetchPlan (com.orientechnologies.orient.core.fetch.OFetchPlan)1 OJSONFetchListener (com.orientechnologies.orient.core.fetch.json.OJSONFetchListener)1 ORecordStringable (com.orientechnologies.orient.core.record.ORecordStringable)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OJSONWriter (com.orientechnologies.orient.core.serialization.serializer.OJSONWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1