Java Code Examples for org.bson.codecs.EncoderContext#encodeWithChildContext()

The following examples show how to use org.bson.codecs.EncoderContext#encodeWithChildContext() . 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: EntityEncoder.java    From morphia with Apache License 2.0 6 votes vote down vote up
private <S> void encodeValue(final BsonWriter writer, final EncoderContext encoderContext, final FieldModel<S> model,
                             final S propertyValue) {
    if (model.shouldSerialize(propertyValue)) {
        writer.writeName(model.getMappedName());
        if (propertyValue == null) {
            writer.writeNull();
        } else {
            try {
                encoderContext.encodeWithChildContext(model.getCachedCodec(), writer, propertyValue);
            } catch (CodecConfigurationException e) {
                throw new CodecConfigurationException(String.format("Failed to encode '%s'. Encoding '%s' errored with: %s",
                    morphiaCodec.getEntityModel().getName(), model.getMappedName(), e.getMessage()), e);
            }
        }
    }
}
 
Example 2
Source File: Filter.java    From morphia with Apache License 2.0 5 votes vote down vote up
protected void writeNamedValue(final String name, final Object named, final Mapper mapper, final BsonWriter writer,
                               final EncoderContext encoderContext) {
    writer.writeName(name);
    if (named != null) {
        Codec codec = mapper.getCodecRegistry().get(named.getClass());
        encoderContext.encodeWithChildContext(codec, writer, named);
    } else {
        writer.writeNull();
    }
}
 
Example 3
Source File: Filter.java    From morphia with Apache License 2.0 5 votes vote down vote up
protected void writeUnnamedValue(final Object value, final Mapper mapper, final BsonWriter writer,
                                 final EncoderContext encoderContext) {
    if (value != null) {
        Codec codec = mapper.getCodecRegistry().get(value.getClass());
        encoderContext.encodeWithChildContext(codec, writer, value);
    } else {
        writer.writeNull();
    }
}
 
Example 4
Source File: StageCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
protected void writeNamedValue(final BsonWriter writer, final String name, final Object value, final EncoderContext encoderContext) {
    if (value != null) {
        writer.writeName(name);
        Codec codec = getCodecRegistry().get(value.getClass());
        encoderContext.encodeWithChildContext(codec, writer, value);
    }
}
 
Example 5
Source File: UnsetCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
protected void encodeStage(final BsonWriter writer, final Unset value, final EncoderContext encoderContext) {
    List<Expression> fields = value.getFields();
    if (fields.size() == 1) {
        fields.get(0).encode(getMapper(), writer, encoderContext);
    } else if(fields.size()> 1) {
        Codec codec = getCodecRegistry().get(fields.getClass());
        encoderContext.encodeWithChildContext(codec, writer, fields);
    }
}
 
Example 6
Source File: ExpressionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * @param mapper
 * @param writer
 * @param name
 * @param value
 * @param encoderContext
 * @morphia.internal
 */
public static void writeNamedValue(final Mapper mapper, final BsonWriter writer, final String name, final Object value,
                                   final EncoderContext encoderContext) {
    if (value != null) {
        writer.writeName(name);
        Codec codec = mapper.getCodecRegistry().get(value.getClass());
        encoderContext.encodeWithChildContext(codec, writer, value);
    }
}
 
Example 7
Source File: ExpressionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * @param mapper
 * @param writer
 * @param value
 * @param encoderContext
 * @morphia.internal
 */
public static void writeUnnamedValue(final Mapper mapper, final BsonWriter writer, final Object value,
                                     final EncoderContext encoderContext) {
    if (value != null) {
        Codec codec = mapper.getCodecRegistry().get(value.getClass());
        encoderContext.encodeWithChildContext(codec, writer, value);
    }
}
 
Example 8
Source File: LiteralExpression.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());
    Codec codec = mapper.getCodecRegistry().get(getValue().getClass());
    encoderContext.encodeWithChildContext(codec, writer, getValue());
    writer.writeEndDocument();
}
 
Example 9
Source File: Expression.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * @param mapper         the mapper
 * @param writer         the writer
 * @param encoderContext the context
 * @morphia.internal
 */
@SuppressWarnings("rawtypes")
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    writer.writeName(operation);
    Codec codec = mapper.getCodecRegistry().get(value.getClass());
    encoderContext.encodeWithChildContext(codec, writer, value);
    writer.writeEndDocument();
}
 
Example 10
Source File: ValueExpression.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) {
    if (getValue() != null) {
        Codec codec = mapper.getCodecRegistry().get(getValue().getClass());
        encoderContext.encodeWithChildContext(codec, writer, getValue());
    } else {
        writer.writeNull();
    }
}
 
Example 11
Source File: MergeCodec.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
protected void encodeStage(final BsonWriter writer, final Merge value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    String collection;
    String database = null;
    if(value.getType() != null) {
        collection = getMapper().getMappedClass(value.getType()).getCollectionName();
    } else {
        collection = value.getCollection();
        database = value.getDatabase();
    }

    if(database == null) {
        writer.writeString("into", collection);
    } else {
        writer.writeStartDocument("into");
        writer.writeString("db", database);
        writer.writeString("coll", collection);
        writer.writeEndDocument();
    }

    List<String> on = value.getOn();
    if(on != null) {
        if(on.size() == 1) {
            writer.writeString("on", on.get(0));
        } else {
            writer.writeStartArray("on");
            for (final String name : on) {
                writer.writeString(name);
            }
            writer.writeEndArray();
        }
    }
    Map<String, Expression> variables = value.getVariables();
    if(variables != null) {
        writer.writeStartDocument("let");
        for (final Entry<String, Expression> entry : variables.entrySet()) {
            writer.writeName(entry.getKey());
            entry.getValue().encode(getMapper(), writer, encoderContext);
        }
        writer.writeEndDocument();
    }
    writeEnum(writer, "whenMatched", value.getWhenMatched(), encoderContext);
    List<Stage> pipeline = value.getWhenMatchedPipeline();
    if(pipeline != null) {
        writer.writeName("whenMatched");
        Codec codec = getCodecRegistry().get(pipeline.getClass());
        encoderContext.encodeWithChildContext(codec, writer, pipeline);
    }
    writeEnum(writer, "whenNotMatched", value.getWhenNotMatched(), encoderContext);
    writer.writeEndDocument();
}
 
Example 12
Source File: ProjectionCodec.java    From morphia with Apache License 2.0 4 votes vote down vote up
private void write(final BsonWriter writer, final PipelineField field, final EncoderContext encoderContext) {
    writer.writeName(field.getName());
    Class aClass = field.getValue().getClass();
    Codec codec = getCodecRegistry().get(aClass);
    encoderContext.encodeWithChildContext(codec, writer, field.getValue());
}
 
Example 13
Source File: ArrayLiteral.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext encoderContext) {
    Codec codec = mapper.getCodecRegistry().get(values.getClass());
    encoderContext.encodeWithChildContext(codec, writer, values);
}