Search in sources :

Example 1 with OJSONFetchListener

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

Aggregations

OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)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 ORecordStringable (com.orientechnologies.orient.core.record.ORecordStringable)1 OJSONWriter (com.orientechnologies.orient.core.serialization.serializer.OJSONWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1