/** * Adds custom type information to the given {@link Document} if necessary. That is if the value is not the same as * the one given. This is usually the case if you store a subtype of the actual declared type of the property. * * @param type * @param value must not be {@literal null}. * @param bson must not be {@literal null}. */ protected void addCustomTypeKeyIfNecessary(@Nullable TypeInformation<?> type, Object value, Bson bson) { Class<?> reference = type != null ? type.getActualType().getType() : Object.class; Class<?> valueType = ClassUtils.getUserClass(value.getClass()); boolean notTheSameClass = !valueType.equals(reference); if (notTheSameClass) { typeMapper.writeType(valueType, bson); } }
private Document updateTypeRestrictions(Document query, Example example) { Document result = new Document(); if (isTypeRestricting(example)) { result.putAll(query); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; } for (Map.Entry<String, Object> entry : query.entrySet()) { if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; }
/** * Returns whether the given {@link String} is the type key. * * @param key * @return * @see MongoTypeMapper#isTypeKey(String) * @since 2.2 */ protected boolean isTypeKey(String key) { return converter.getTypeMapper().isTypeKey(key); }
TypeInformation<? extends S> typeToUse = typeMapper.readType(bson, type); Class<? extends S> rawType = typeToUse.getType();
this.converter.getTypeMapper().writeTypeRestrictions(result, restrictedTypes);
private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
TypeInformation<? extends S> typeToUse = typeMapper.readType(bson, type); Class<? extends S> rawType = typeToUse.getType();
this.converter.getTypeMapper().writeTypeRestrictions(result, restrictedTypes);
if (typeMapper.isTypeKey(key)) {
private Document updateTypeRestrictions(Document query, Example example) { Document result = new Document(); if (isTypeRestricting(example)) { result.putAll(query); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; } for (Map.Entry<String, Object> entry : query.entrySet()) { if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; }
private Serializable readObject(DBObject dbObject) throws ClassNotFoundException { TypeInformation<?> information = mapper.getTypeMapper().readType(dbObject); if (information == null) { return null; } return (Serializable) mapper.read(information.getType(), dbObject); }
/** * Root entry method into write conversion. Adds a type discriminator to the {@link Document}. Shouldn't be called for * nested conversions. * * @see org.springframework.data.mongodb.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.Document) */ public void write(Object obj, Bson bson) { if (null == obj) { return; } Class<?> entityType = ClassUtils.getUserClass(obj.getClass()); TypeInformation<? extends Object> type = ClassTypeInformation.from(entityType); Object target = obj instanceof LazyLoadingProxy ? ((LazyLoadingProxy) obj).getTarget() : obj; writeInternal(target, bson, type); if (asMap(bson).containsKey("_id") && asMap(bson).get("_id") == null) { removeFromMap(bson, "_id"); } if (requiresTypeHint(entityType)) { typeMapper.writeType(type, bson); } }
/** * Returns whether the given {@link String} is the type key. * * @param key * @return * @see MongoTypeMapper#isTypeKey(String) * @since 2.2 */ protected boolean isTypeKey(String key) { return converter.getTypeMapper().isTypeKey(key); }
/** * Adds custom type information to the given {@link Document} if necessary. That is if the value is not the same as * the one given. This is usually the case if you store a subtype of the actual declared type of the property. * * @param type * @param value must not be {@literal null}. * @param bson must not be {@literal null}. */ protected void addCustomTypeKeyIfNecessary(@Nullable TypeInformation<?> type, Object value, Bson bson) { Class<?> reference = type != null ? type.getActualType().getType() : Object.class; Class<?> valueType = ClassUtils.getUserClass(value.getClass()); boolean notTheSameClass = !valueType.equals(reference); if (notTheSameClass) { typeMapper.writeType(valueType, bson); } }
private boolean isTypeRestricting(Example example) { if (example.getMatcher() instanceof UntypedExampleMatcher) { return false; } if (example.getMatcher().getIgnoredPaths().isEmpty()) { return true; } for (String path : example.getMatcher().getIgnoredPaths()) { if (this.converter.getTypeMapper().isTypeKey(path)) { return false; } } return true; }
/** * Root entry method into write conversion. Adds a type discriminator to the {@link Document}. Shouldn't be called for * nested conversions. * * @see org.springframework.data.mongodb.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.Document) */ public void write(Object obj, Bson bson) { if (null == obj) { return; } Class<?> entityType = ClassUtils.getUserClass(obj.getClass()); TypeInformation<? extends Object> type = ClassTypeInformation.from(entityType); Object target = obj instanceof LazyLoadingProxy ? ((LazyLoadingProxy) obj).getTarget() : obj; writeInternal(target, bson, type); if (asMap(bson).containsKey("_id") && asMap(bson).get("_id") == null) { removeFromMap(bson, "_id"); } if (requiresTypeHint(entityType)) { typeMapper.writeType(type, bson); } }
if (typeMapper.isTypeKey(key)) {
public List<YAttribute> convert(BasicDBObject source) { List<YAttribute> attributes = new ArrayList<>(); for (String key : source.keySet()) { if(getConverter().getTypeMapper().isTypeKey(key)){ continue; } Object value = source.get(key); key = getConverter().potentiallyUnescapeMapKey(key); attributes.addAll(processAttributes(key, value)); } return attributes; }