@Override public T decode(final BsonReader reader, final DecoderContext decoderContext) { try { return pojoCodec.decode(reader, decoderContext); } catch (CodecConfigurationException e) { throw new CodecConfigurationException( format("An exception occurred when decoding using the AutomaticPojoCodec.%n" + "Decoding into a '%s' failed with the following exception:%n%n%s%n%n" + "A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.", pojoCodec.getEncoderClass().getSimpleName(), e.getMessage()), e); } }
@Override public void encode(final BsonWriter writer, final T value, final EncoderContext encoderContext) { try { pojoCodec.encode(writer, value, encoderContext); } catch (CodecConfigurationException e) { throw new CodecConfigurationException( format("An exception occurred when encoding using the AutomaticPojoCodec.%n" + "Encoding a %s: '%s' failed with the following exception:%n%n%s%n%n" + "A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.", getEncoderClass().getSimpleName(), value, e.getMessage()), e); } }
private <S> void encodeValue(final BsonWriter writer, final EncoderContext encoderContext, final PropertyModel<S> propertyModel, final S propertyValue) { if (propertyModel.shouldSerialize(propertyValue)) { writer.writeName(propertyModel.getReadName()); if (propertyValue == null) { writer.writeNull(); } else { try { encoderContext.encodeWithChildContext(propertyModel.getCachedCodec(), writer, propertyValue); } catch (CodecConfigurationException e) { throw new CodecConfigurationException(format("Failed to encode '%s'. Encoding '%s' errored with: %s", classModel.getName(), propertyModel.getReadName(), e.getMessage()), e); } } } }
@SuppressWarnings("unchecked") private <S> void decodePropertyModel(final BsonReader reader, final DecoderContext decoderContext, final InstanceCreator<T> instanceCreator, final String name, final PropertyModel<S> propertyModel) { if (propertyModel != null) { try { S value = null; if (reader.getCurrentBsonType() == BsonType.NULL) { reader.readNull(); } else { value = decoderContext.decodeWithChildContext(propertyModel.getCachedCodec(), reader); } if (propertyModel.isWritable()) { instanceCreator.set(value, propertyModel); } } catch (BsonInvalidOperationException e) { throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding '%s' errored with: %s", classModel.getName(), name, e.getMessage()), e); } catch (CodecConfigurationException e) { throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding '%s' errored with: %s", classModel.getName(), name, e.getMessage()), e); } } else { if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("Found property not present in the ClassModel: %s", name)); } reader.skipValue(); } }