org.bson.codecs.configuration.CodecConfigurationException Java Examples
The following examples show how to use
org.bson.codecs.configuration.CodecConfigurationException.
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: DiscriminatorLookup.java From morphia with Apache License 2.0 | 6 votes |
/** * Looks up a discriminator value * * @param discriminator the value to search witih * @return the mapped class */ public Class<?> lookup(final String discriminator) { if (discriminatorClassMap.containsKey(discriminator)) { return discriminatorClassMap.get(discriminator); } Class<?> clazz = getClassForName(discriminator); if (clazz == null) { clazz = searchPackages(discriminator); } if (clazz == null) { throw new CodecConfigurationException(format("A class could not be found for the discriminator: '%s'.", discriminator)); } else { discriminatorClassMap.put(discriminator, clazz); } return clazz; }
Example #2
Source File: MorphiaMapPropertyCodecProvider.java From morphia with Apache License 2.0 | 6 votes |
@Override public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) { if (Map.class.isAssignableFrom(type.getType())) { final List<? extends TypeWithTypeParameters<?>> typeParameters = type.getTypeParameters(); TypeWithTypeParameters<?> keyType = getType(typeParameters, 0); final TypeWithTypeParameters<?> valueType = getType(typeParameters, 1); try { return new MapCodec(type.getType(), keyType.getType(), registry.get(valueType)); } catch (CodecConfigurationException e) { if (valueType.getType().equals(Object.class)) { try { return (Codec<T>) registry.get(TypeData.builder(Map.class).build()); } catch (CodecConfigurationException e1) { // Ignore and return original exception } } throw e; } } else if (Enum.class.isAssignableFrom(type.getType())) { return new EnumCodec(type.getType()); } return null; }
Example #3
Source File: CollectionCodec.java From morphia with Apache License 2.0 | 6 votes |
private Collection<T> getInstance() { if (encoderClass.isInterface()) { if (encoderClass.isAssignableFrom(ArrayList.class)) { return new ArrayList<T>(); } else if (encoderClass.isAssignableFrom(HashSet.class)) { return new HashSet<T>(); } else { throw new CodecConfigurationException(format("Unsupported Collection interface of %s!", encoderClass.getName())); } } try { return encoderClass.getDeclaredConstructor().newInstance(); } catch (final Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } }
Example #4
Source File: MorphiaCollectionPropertyCodecProvider.java From morphia with Apache License 2.0 | 6 votes |
@Override public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) { if (Collection.class.isAssignableFrom(type.getType())) { final List<? extends TypeWithTypeParameters<?>> typeParameters = type.getTypeParameters(); TypeWithTypeParameters<?> valueType = getType(typeParameters, 0); try { return new MorphiaCollectionCodec(type, registry, valueType); } catch (CodecConfigurationException e) { if (valueType.getType().equals(Object.class)) { try { return (Codec<T>) registry.get(TypeData.builder(Collection.class).build()); } catch (CodecConfigurationException e1) { // Ignore and return original exception } } throw e; } } return null; }
Example #5
Source File: NativeCodecRegistry.java From immutables with Apache License 2.0 | 6 votes |
@Override public <T> Codec<T> get(Class<T> clazz) { final JavaType javaType = mapper.getTypeFactory().constructType(clazz); try { JsonSerializer<?> ser = mapper.getSerializerProviderInstance().findValueSerializer(javaType); if (ser instanceof Wrapper) { @SuppressWarnings("unchecked") Codec<T> codec = ((Wrapper<Codec<T>>) ser).unwrap(); return codec; } } catch (JsonMappingException e) { throw new CodecConfigurationException("Exception for " + javaType, e); } throw new CodecConfigurationException(javaType + " not supported"); }
Example #6
Source File: EntityEncoder.java From morphia with Apache License 2.0 | 6 votes |
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 #7
Source File: EntityDecoder.java From morphia with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") protected Codec<T> getCodecFromDocument(final BsonReader reader, final boolean useDiscriminator, final String discriminatorKey, final CodecRegistry registry, final DiscriminatorLookup discriminatorLookup, final Codec<T> defaultCodec) { Codec<T> codec = null; if (useDiscriminator) { BsonReaderMark mark = reader.getMark(); try { reader.readStartDocument(); while (codec == null && reader.readBsonType() != BsonType.END_OF_DOCUMENT) { if (discriminatorKey.equals(reader.readName())) { codec = (Codec<T>) registry.get(discriminatorLookup.lookup(reader.readString())); } else { reader.skipValue(); } } } catch (Exception e) { throw new CodecConfigurationException(String.format("Failed to decode '%s'. Decoding errored with: %s", morphiaCodec.getEntityModel().getName(), e.getMessage()), e); } finally { mark.reset(); } } return codec != null ? codec : defaultCodec; }
Example #8
Source File: MorphiaCollectionCodec.java From morphia with Apache License 2.0 | 5 votes |
private Collection<T> getInstance() { if (getEncoderClass().equals(Collection.class) || getEncoderClass().equals(List.class)) { return new ArrayList<>(); } else if (getEncoderClass().equals(Set.class)) { return new HashSet<>(); } try { final Constructor<Collection<T>> constructor = getEncoderClass().getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (final Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } }
Example #9
Source File: ObjectCodec.java From morphia with Apache License 2.0 | 5 votes |
@Override public Object decode(final BsonReader reader, final DecoderContext decoderContext) { BsonType bsonType = reader.getCurrentBsonType(); Class<?> clazz; if (bsonType == BsonType.DOCUMENT) { clazz = Document.class; String discriminatorField = mapper.getOptions().getDiscriminatorKey(); BsonReaderMark mark = reader.getMark(); reader.readStartDocument(); while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) { if (reader.readName().equals(discriminatorField)) { try { clazz = mapper.getClass(reader.readString()); } catch (CodecConfigurationException e) { throw new MappingException(e.getMessage(), e); } } else { reader.skipValue(); } } mark.reset(); } else { clazz = bsonTypeClassMap.get(bsonType); } return mapper.getCodecRegistry() .get(clazz) .decode(reader, decoderContext); }
Example #10
Source File: MorphiaMapPropertyCodecProvider.java From morphia with Apache License 2.0 | 5 votes |
private Map<K, V> getInstance() { if (encoderClass.isInterface()) { return new HashMap<>(); } try { final Constructor<Map<K, V>> constructor = encoderClass.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (final Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } }
Example #11
Source File: EnumPropertyCodecProvider.java From morphia with Apache License 2.0 | 5 votes |
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry propertyCodecRegistry) { Class<T> clazz = type.getType(); if (Enum.class.isAssignableFrom(clazz)) { try { return codecRegistry.get(clazz); } catch (CodecConfigurationException e) { return (Codec<T>) new EnumCodec(clazz); } } return null; }
Example #12
Source File: EntityCodec.java From mongo-mapper with Apache License 2.0 | 5 votes |
private <V> Codec<V> getCodecForType(Class<V> fieldType) { if (ignoredTypes.contains(fieldType)) { return null; } try { return registry.get(fieldType); } catch (CodecConfigurationException | MongoMapperException e) { // No other way to check without catching exception. // Cache types without codec to improve performance ignoredTypes.add(fieldType); } return null; }
Example #13
Source File: JacksonCodecRegistry.java From immutables with Apache License 2.0 | 5 votes |
@Override public <T> Codec<T> get(Class<T> clazz) { final JavaType javaType = mapper.getTypeFactory().constructType(clazz); if (!(mapper.canSerialize(clazz) && mapper.canDeserialize(javaType))) { throw new CodecConfigurationException(String.format("%s (javaType: %s) not supported by Jackson Mapper", clazz, javaType)); } return new JacksonCodec<>(clazz, mapper); }
Example #14
Source File: JacksonCodecs.java From immutables with Apache License 2.0 | 5 votes |
private static <T> Codec<T> findCodecOrNull(CodecRegistry registry, Class<T> type) { try { return registry.get(type); } catch (CodecConfigurationException e) { return null; } }
Example #15
Source File: BsonCodecRepoTest.java From immutables with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") // hopefully correct @Override public <T> Codec<T> get(Class<T> clazz) { if (!Somebody.class.isAssignableFrom(clazz)) { throw new CodecConfigurationException("Not supported " + clazz); } return (Codec<T>) this; }
Example #16
Source File: JacksonCodecs.java From immutables with Apache License 2.0 | 5 votes |
public static Serializers serializers(final CodecRegistry registry) { return new Serializers.Base() { @Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { try { Codec<?> codec = registry.get(type.getRawClass()); return serializer(codec); } catch (CodecConfigurationException e) { return null; } } }; }
Example #17
Source File: JacksonCodecs.java From immutables with Apache License 2.0 | 5 votes |
public static CodecRegistry registryFromMapper(final ObjectMapper mapper) { Preconditions.checkNotNull(mapper, "mapper"); return new CodecRegistry() { @Override public <T> Codec<T> get(final Class<T> clazz) { final JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); if (!mapper.canSerialize(clazz) || !mapper.canDeserialize(javaType)) { throw new CodecConfigurationException(String.format("%s (javaType: %s) not supported by Jackson Mapper", clazz, javaType)); } return new JacksonCodec<>(clazz, mapper); } }; }
Example #18
Source File: GsonCodecs.java From immutables with Apache License 2.0 | 5 votes |
/** * Gson Factory which gives preference to existing adapters from {@code gson} instance. However, * if type is not supported it will query {@link CodecRegistry} to create one (if possible). * * <p>This allows supporting Bson types by Gson natively (eg. for {@link org.bson.types.ObjectId}). * * @param registry existing registry which will be used if type is unknown to {@code gson}. * @return factory which delegates to {@code registry} for unknown types. */ public static TypeAdapterFactory delegatingTypeAdapterFactory(final CodecRegistry registry) { Preconditions.checkNotNull(registry, "registry"); return new TypeAdapterFactory() { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { boolean hasAdapter; try { TypeAdapter<T> adapter = gson.getDelegateAdapter(this, type); hasAdapter = !isReflectiveTypeAdapter(adapter); } catch (IllegalArgumentException e) { hasAdapter = false; } if (hasAdapter) { return null; } try { @SuppressWarnings("unchecked") Codec<T> codec = (Codec<T>) registry.get(type.getRawType()); return typeAdapterFromCodec(codec); } catch (CodecConfigurationException e1) { return null; } } }; }
Example #19
Source File: GsonCodecs.java From immutables with Apache License 2.0 | 3 votes |
/** * Build a codec from {@link TypeAdapter}. Opposite of {@link #typeAdapterFromCodec(Codec)}. * * @param type type handled by this adapter * @param adapter existing adapter * @param <T> codec value type * @throws CodecConfigurationException if adapter is not supported * @return new instance of the codec which handles {@code type}. */ public static <T> Codec<T> codecFromTypeAdapter(Class<T> type, TypeAdapter<T> adapter) { if (isReflectiveTypeAdapter(adapter)) { throw new CodecConfigurationException(String.format("%s can't be build from %s " + "(for type %s)", TypeAdapterCodec.class.getSimpleName(), adapter.getClass().getName(), type.getName())); } return new TypeAdapterCodec<>(type, adapter); }