<p> Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings). Very similar to {@link
Mapper#toDBObject} </p> <p> Used (mainly) by query/update operations </p>
518400
Object toMongoObject(final Object javaObj, final boolean includeClassName) {
        if (javaObj == null) {
            return null;
        }
        Class origClass = javaObj.getClass();

        if (origClass.isAnonymousClass() && origClass.getSuperclass().isEnum()) {
            origClass = origClass.getSuperclass();
        }

        final Object newObj = getConverters().encode(origClass, javaObj);
        if (newObj == null) {
            LOG.warn("converted " + javaObj + " to null");
            return null;
        }
        final Class type = newObj.getClass();
        final boolean bSameType = origClass.equals(type);

        //TODO: think about this logic a bit more.
        //Even if the converter changed it, should it still be processed?
        if (!bSameType && !(Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type))) {
            return newObj;
        } else { //The converter ran, and produced another type, or it is a list/map

            boolean isSingleValue = true;
            boolean isMap = false;
            Class subType = null;

            if (type.isArray() || Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type)) {
                isSingleValue = false;
                isMap = implementsInterface(type, Map.class);
                // subtype of Long[], List<Long> is Long
                subType = (type.isArray()) ? type.getComponentType() : getParameterizedClass(type, (isMap) ? 1 : 0);
            }

            if (isSingleValue && !isPropertyType(type)) {
                final DBObject dbObj = toDBObject(newObj);
                if (!includeClassName) {
                    dbObj.removeField(opts.getDiscriminatorField());
                }
                return dbObj;
            } else if (newObj instanceof DBObject) {
                return newObj;
            } else if (isMap) {
                if (isPropertyType(subType)) {
                    return toDBObject(newObj);
                } else {
                    final LinkedHashMap m = new LinkedHashMap();
                    for (final Map.Entry e : (Iterable<Map.Entry>) ((Map) newObj).entrySet()) {
                        m.put(e.getKey(), toMongoObject(e.getValue(), includeClassName));
                    }

                    return m;
                }
                //Set/List but needs elements converted
            } else if (!isSingleValue && !isPropertyType(subType)) {
                final List<Object> values = new BasicDBList();
                if (type.isArray()) {
                    for (final Object obj : (Object[]) newObj) {
                        values.add(toMongoObject(obj, includeClassName));
                    }
                } else {
                    for (final Object obj : (Iterable) newObj) {
                        values.add(toMongoObject(obj, includeClassName));
                    }
                }

                return values;
            } else {
                return newObj;
            }
        }
    }
1010558
public static <T> T convertObject(String json, Class<T> clazz) throws IOException {
        try {
            if (clazz.isAnnotationPresent(Model.class)) {
                return MODEL_MAPPER.readValue(json, clazz);
            }
            return MAPPER.readValue(json, clazz);
        } catch (IOException e) {
            String error = String.format("Unable to parse given json '%s' into class '%s'.", json, clazz.getName());
            LOGGER.error(error, e);
            throw new IOException(error, e);
        }
    }
267371
public MongoTemplate buildMongoTemplate(final BaseMongoDbProperties mongo) {
        val mongoDbFactory = mongoDbFactory(buildMongoDbClient(mongo), mongo);
        return new MongoTemplate(mongoDbFactory, mappingMongoConverter(mongoDbFactory));
    }
155167
public DBObject toDBObject() {
        DBObject dbObject = new BasicDBObject();
        if (granularity != null) {
            dbObject.put("granularity", granularity.getGranulality());
        }

        DBObject output = new BasicDBObject();
        for (Map.Entry<String, Accumulator> entry : accumulators.entrySet()) {
            output.put(entry.getKey(), entry.getValue().toDBObject());
        }
        if (!accumulators.isEmpty()) {
            dbObject.put("output", output);
        }

        return dbObject;
    }
533683
public <T> T convertValue(Object object, Class<T> clazz) {
		return this.mapper.convertValue(object, clazz);
	}
19285
public <T> JsonReaderI<T> getMapper(Class<T> type) {
		// look for cached Mapper
		@SuppressWarnings("unchecked")
		JsonReaderI<T> map = (JsonReaderI<T>) cache.get(type);
		if (map != null)
			return map;
		/*
		 * Special handle
		 */
		if (type instanceof Class) {
			if (Map.class.isAssignableFrom(type))
				map = new DefaultMapperCollection<T>(this, type);
			else if (List.class.isAssignableFrom(type))
				map = new DefaultMapperCollection<T>(this, type);
			if (map != null) {
				cache.put(type, map);
				return map;
			}
		}

		if (type.isArray())
			map = new ArraysMapper.GenericMapper<T>(this, type);
		else if (List.class.isAssignableFrom(type))
			map = new CollectionMapper.ListClass<T>(this, type);
		else if (Map.class.isAssignableFrom(type))
			map = new CollectionMapper.MapClass<T>(this, type);
		else
			// use bean class
			map = new BeansMapper.Bean<T>(this, type);
		cache.putIfAbsent(type, map);
		return map;
	}
571063
private <T> T convertObject(byte[] json, Class<T> claz) throws IOException {
        TypeReference<RestSuccessResult<Map<?, ?>>> typeReference = new TypeReference<RestSuccessResult<Map<?, ?>>>() {
        };
        RestSuccessResult<T> successResponse = MAPPER.readValue(json, typeReference);
        Map<?, ?> data = (Map<?, ?>) successResponse.getData();
        return MAPPER.convertValue(data, claz);
    }
332071
@Override
    public synchronized T read(T dto) throws Exception {
        Cursor cursor = db.query(transformer.getTableName(), transformer.getFields(), transformer.getWhereClause(dto),null, null,null,null);
        T object = null;
        if (cursor.moveToFirst()){
            object = transformer.transform(cursor);
        }
        return object;
    }
1200080
<T> Map<String, HColumn<String, byte[]>> createColumnMap(T obj) {
    if (null == obj) {
      throw new IllegalArgumentException("Class type cannot be null");
    }

    @SuppressWarnings("unchecked")
    CFMappingDef<T> cfMapDef = (CFMappingDef<T>) cacheMgr.getCfMapDef((Class<T>) obj.getClass(),
        true);
    try {
      Map<String, HColumn<String, byte[]>> colSet = new HashMap<String, HColumn<String, byte[]>>();
      Collection<PropertyMappingDefinition> coll = cfMapDef.getAllProperties();
      for (PropertyMappingDefinition md : coll) {
        Collection<HColumn<String, byte[]>> colColl = createColumnsFromProperty(obj, md);
        if (null != colColl) {
          for (HColumn<String, byte[]> col : colColl) {
            colSet.put(col.getName(), col);
          }
        }
      }

      if (null != cfMapDef.getCfBaseMapDef()) {
        CFMappingDef<?> cfSuperMapDef = cfMapDef.getCfBaseMapDef();
        String discColName = cfSuperMapDef.getDiscColumn();
        DiscriminatorType discType = cfSuperMapDef.getDiscType();

        colSet.put(
            discColName,
            createHColumn(discColName, convertDiscTypeToColValue(discType, cfMapDef.getDiscValue())));
      }

      addAnonymousProperties(obj, cfMapDef, colSet);
      return colSet;
    } catch (SecurityException e) {
      throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
      throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(e);
    }
  }
289826
@SuppressWarnings("unchecked")
	public <T> T toObject(String jsonString, JavaType javaType) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}

		try {
			return (T) mapper.readValue(jsonString, javaType);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
1170789
public <S, T> T convertTo(ConverterKey<S,T> key, Object object) {

		if (object == null) {
			return null;
		}
		
		Converter<S, T> conv = findConverter(key.getInputClass(), key.getOutputClass(), key.getQualifierAnnotation() == null ? DefaultBinding.class : key.getQualifierAnnotation());
		
		if (conv == null) {
			throw new NoConverterFoundException(key);
		}
		
		@SuppressWarnings("unchecked")
		S myObject = (S)object;
		return conv.convert(myObject);
	}
487980
public T findOneById(K id, T fields) throws MongoException {
        return findOneById(id, convertToBasicDbObject(fields));
    }
431478
public DBObject toDBObject() {
        final BasicDBList list = new BasicDBList();
        for (final Point point : points) {
            list.add(point.toDBObject());
        }

        return new BasicDBObject(geometry, list);
    }
864982
public static <T> T objectify(ObjectMapper mapper, Object source, JavaType targetType) {
        try {
            return mapper.readValue(mapper.writeValueAsBytes(source), targetType);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
1306252
private static <T> T mapByIndex(byte[] content, Class<T> genericType, ObjectMetaData objectMap, Charset charset)
            throws InstantiationException, IllegalAccessException, InvocationTargetException {

        List<FieldMetaData> mappingFields = objectMap.getFieldList();
        T obj = genericType.newInstance();
        for (int i = 0; i < mappingFields.size(); i++) {
            FieldMetaData field = mappingFields.get(i);
            // 设置属性值
            LOGGER.debug("设置值是 " + field + field.getValue(content, charset));
            BeanUtils.setProperty(obj, field.getFieldName(), field.getValue(content, charset));
        }

        return obj;
    }
596438
public static Mapper beanMapper(final String property){
        return  new Mapper() {
        public Object map(Object a) {
            try {
                return BeanUtils.getProperty(a, property);
            } catch (Exception e) {
                throw new ClassCastException("Object was not the expected class: " + a.getClass());
            }
        }
        };
    }
96062
protected ByteBuffer objToBB(Object o)
    {
        if (o == null)
            return nullToBB();
        if (o instanceof java.lang.String)
            return ByteBuffer.wrap(new DataByteArray((String)o).get());
        if (o instanceof Integer)
            return Int32Type.instance.decompose((Integer)o);
        if (o instanceof Long)
            return LongType.instance.decompose((Long)o);
        if (o instanceof Float)
            return FloatType.instance.decompose((Float)o);
        if (o instanceof Double)
            return DoubleType.instance.decompose((Double)o);
        if (o instanceof UUID)
            return ByteBuffer.wrap(UUIDGen.decompose((UUID) o));
        if(o instanceof Tuple) {
            List<Object> objects = ((Tuple)o).getAll();
            //collections
            if (objects.size() > 0 && objects.get(0) instanceof String)
            {
                String collectionType = (String) objects.get(0);
                if ("set".equalsIgnoreCase(collectionType) ||
                        "list".equalsIgnoreCase(collectionType))
                    return objToListOrSetBB(objects.subList(1, objects.size()));
                else if ("map".equalsIgnoreCase(collectionType))
                    return objToMapBB(objects.subList(1, objects.size()));
                   
            }
            return objToCompositeBB(objects);
        }

        return ByteBuffer.wrap(((DataByteArray) o).get());
    }