Java Code Examples for org.bson.BsonWriter#writeStartDocument()

The following examples show how to use org.bson.BsonWriter#writeStartDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MathExpression.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeName(getOperation());
    if (operands.size() > 1) {
        writer.writeStartArray();
    }
    for (final Expression operand : operands) {
        if (operand != null) {
            ExpressionCodec.writeUnnamedExpression(mapper, writer, operand, encoderContext);
        } else {
            writer.writeNull();
        }
    }
    if (operands.size() > 1) {
        writer.writeEndArray();
    }
    writer.writeEndDocument();
}
 
Example 2
Source File: EntityEncoder.java    From morphia with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void encodeEntity(final BsonWriter writer, final T value, final EncoderContext encoderContext) {
    if (areEquivalentTypes(value.getClass(), morphiaCodec.getEntityModel().getType())) {
        writer.writeStartDocument();

        FieldModel<?> idModel = morphiaCodec.getEntityModel().getIdModel();
        encodeIdProperty(writer, value, encoderContext, idModel);

        if (morphiaCodec.getEntityModel().useDiscriminator()) {
            writer.writeString(morphiaCodec.getEntityModel().getDiscriminatorKey(), morphiaCodec.getEntityModel().getDiscriminator());
        }

        for (FieldModel<?> fieldModel : morphiaCodec.getEntityModel().getFieldModels()) {
            if (fieldModel.equals(idModel)) {
                continue;
            }
            encodeProperty(writer, value, encoderContext, fieldModel);
        }
        writer.writeEndDocument();
    } else {
        morphiaCodec.getRegistry().get((Class<T>) value.getClass())
                    .encode(writer, value, encoderContext);
    }
}
 
Example 3
Source File: SwitchExpression.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartDocument(getOperation());
    writer.writeStartArray("branches");
    for (final Pair branch : branches) {
        writer.writeStartDocument();
        writer.writeName("case");
        branch.caseExpression.encode(mapper, writer, encoderContext);
        writer.writeName("then");
        branch.then.encode(mapper, writer, encoderContext);
        writer.writeEndDocument();
    }
    writer.writeEndArray();
    writeNamedExpression(mapper, writer, "default", defaultCase, encoderContext);
    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 4
Source File: NearFilter.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
    writer.writeStartDocument(field(mapper));
    if (isNot()) {
        writer.writeStartDocument("$not");
    }
    writer.writeStartDocument(getFilterName());
    writer.writeName("$geometry");
    writeUnnamedValue(getValue(mapper), mapper, writer, context);
    if (maxDistance != null) {
        writeNamedValue("$maxDistance", maxDistance, mapper, writer, context);
    }
    if (minDistance != null) {
        writeNamedValue("$minDistance", minDistance, mapper, writer, context);
    }
    if (crs != null) {
        writeNamedValue("crs", crs, mapper, writer, context);
    }
    writer.writeEndDocument();
    if (isNot()) {
        writer.writeEndDocument();
    }
    writer.writeEndDocument();
}
 
Example 5
Source File: CurrentOpCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
protected void encodeStage(final BsonWriter writer, final CurrentOp value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writeBoolean(writer, "allUsers", value.isAllUsers(), encoderContext);
    writeBoolean(writer, "idleConnections", value.isIdleConnections(), encoderContext);
    writeBoolean(writer, "idleCursors", value.isIdleCursors(), encoderContext);
    writeBoolean(writer, "idleSessions", value.isIdleSessions(), encoderContext);
    writeBoolean(writer, "localOps", value.isLocalOps(), encoderContext);
    writer.writeEndDocument();
}
 
Example 6
Source File: ConvertExpression.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartDocument(getOperation());
    ExpressionCodec.writeNamedExpression(mapper, writer, "input", input, encoderContext);
    ExpressionCodec.writeNamedValue(mapper, writer, "to", to.getName(), encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "onError", onError, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "onNull", onNull, encoderContext);
    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 7
Source File: SortCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
protected void encodeStage(final BsonWriter writer, final Sort value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    for (final SortType sort : value.getSorts()) {
        writer.writeName(sort.getField());
        sort.getDirection().encode(writer);
    }
    writer.writeEndDocument();
}
 
Example 8
Source File: GeoWithinFilter.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public final void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
    writer.writeStartDocument(field(mapper));
    writer.writeStartDocument(getFilterName());
    writer.writeName("$geometry");

    Object shape = getValue();
    Codec codec = mapper.getCodecRegistry().get(shape.getClass());
    codec.encode(writer, shape, context);

    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 9
Source File: ArrayFilterExpression.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartDocument(getOperation());
    ExpressionCodec.writeNamedExpression(mapper, writer, "input", array, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "cond", conditional, encoderContext);
    ExpressionCodec.writeNamedValue(mapper, writer, "as", as, encoderContext);
    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 10
Source File: IsoDates.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeName(getOperation());

    writer.writeStartDocument();
    ExpressionCodec.writeNamedExpression(mapper, writer, "date", date, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "timezone", timezone, encoderContext);
    writer.writeEndDocument();

    writer.writeEndDocument();
}
 
Example 11
Source File: RegexFilter.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
    writer.writeStartDocument(field(mapper));
    if (isNot()) {
        writer.writeStartDocument("$not");
    }
    ExpressionCodec.writeNamedValue(mapper, writer, "$regex", new BsonRegularExpression(regex), context);
    ExpressionCodec.writeNamedValue(mapper, writer, "$options", options, context);
    if (isNot()) {
        writer.writeEndDocument();
    }
    writer.writeEndDocument();
}
 
Example 12
Source File: Filters.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a modulo operation on the value of a field and selects documents with a specified result.
 *
 * @param field     the field to check
 * @param divisor   the value to divide by
 * @param remainder the remainder to check for
 * @return the filter
 * @query.filter $mod
 */
public static Filter mod(final String field, final long divisor, final long remainder) {
    return new Filter("$mod", field, null) {
        @Override
        public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
            writer.writeStartDocument(field(mapper));
            writer.writeName(getFilterName());
            writer.writeStartArray();
            writeUnnamedValue(divisor, mapper, writer, context);
            writeUnnamedValue(remainder, mapper, writer, context);
            writer.writeEndArray();
            writer.writeEndDocument();
        }
    };
}
 
Example 13
Source File: StageCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public final void encode(final BsonWriter writer, final T value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeName(value.getStageName());
    encodeStage(writer, value, encoderContext);
    writer.writeEndDocument();
}
 
Example 14
Source File: IamCodec.java    From Shadbot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void encode(BsonWriter writer, IamBean value, EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeString("message_id", value.getMessageId());
    writer.writeString("role_id", value.getRoleId());
    writer.writeEndDocument();
}
 
Example 15
Source File: IndexExpression.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartArray(getOperation());
    writeUnnamedExpression(mapper, writer, string, encoderContext);
    writeUnnamedExpression(mapper, writer, substring, encoderContext);
    writeUnnamedValue(mapper, writer, start, encoderContext);
    writeUnnamedValue(mapper, writer, end, encoderContext);
    writer.writeEndArray();
    writer.writeEndDocument();
}
 
Example 16
Source File: CenterCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final dev.morphia.query.Shape.Center value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartArray(value.getGeometry());

    encodePosition(writer, value.getCenter().getPosition());
    writer.writeDouble(value.getRadius());

    writer.writeEndArray();
    writer.writeEndDocument();
}
 
Example 17
Source File: ShapeCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final dev.morphia.query.Shape value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartArray(value.getGeometry());
    for (final Point point : value.getPoints()) {
        encodePosition(writer, point.getCoordinates());
    }
    writer.writeEndArray();
    writer.writeEndDocument();
}
 
Example 18
Source File: DateToParts.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeName(getOperation());

    writer.writeStartDocument();
    ExpressionCodec.writeNamedExpression(mapper, writer, "date", date, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "timezone", timeZone, encoderContext);
    ExpressionCodec.writeNamedValue(mapper, writer, "iso8601", iso8601, encoderContext);
    writer.writeEndDocument();

    writer.writeEndDocument();
}
 
Example 19
Source File: ReduceExpression.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeStartDocument(getOperation());
    ExpressionCodec.writeNamedExpression(mapper, writer, "input", input, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "initialValue", initial, encoderContext);
    ExpressionCodec.writeNamedExpression(mapper, writer, "in", in, encoderContext);
    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 20
Source File: DocumentExpression.java    From morphia with Apache License 2.0 4 votes vote down vote up
public void encode(final String name, final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument(name);
    fields.encode(mapper, writer, encoderContext);
    writer.writeEndDocument();
}