org.codehaus.jackson.type.JavaType Java Examples

The following examples show how to use org.codehaus.jackson.type.JavaType. 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: BackportedJacksonMappingIterator.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected BackportedJacksonMappingIterator(JavaType type, JsonParser jp, DeserializationContext ctxt, JsonDeserializer<?> deser) {
    _type = type;
    _parser = jp;
    _context = ctxt;
    _deserializer = (JsonDeserializer<T>) deser;

    /* One more thing: if we are at START_ARRAY (but NOT root-level
     * one!), advance to next token (to allow matching END_ARRAY)
     */
    if (jp != null && jp.getCurrentToken() == JsonToken.START_ARRAY) {
        JsonStreamContext sc = jp.getParsingContext();
        // safest way to skip current token is to clear it (so we'll advance soon)
        if (!sc.inRoot()) {
            jp.clearCurrentToken();
        }
    }
}
 
Example #2
Source File: BackportedObjectReader.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Method called to locate deserializer for the passed root-level value.
 */
protected JsonDeserializer<Object> _findRootDeserializer(DeserializationConfig cfg, JavaType valueType)
        throws JsonMappingException {

    // Sanity check: must have actual type...
    if (valueType == null) {
        throw new JsonMappingException("No value type configured for ObjectReader");
    }

    // First: have we already seen it?
    JsonDeserializer<Object> deser = _rootDeserializers.get(valueType);
    if (deser != null) {
        return deser;
    }

    // es-hadoop: findType with 2 args have been removed since 1.9 so this code compiles on 1.8 (which has the fallback method)
    // es-hadoop: on 1.5 only the 2 args method exists, since 1.9 only the one with 3 args hence the if

    // Nope: need to ask provider to resolve it
    deser = _provider.findTypedValueDeserializer(cfg, valueType);
    if (deser == null) { // can this happen?
        throw new JsonMappingException("Can not find a deserializer for type " + valueType);
    }
    _rootDeserializers.put(valueType, deser);
    return deser;
}
 
Example #3
Source File: Jackson1Parser.java    From typescript-generator with MIT License 6 votes vote down vote up
private BeanHelper getBeanHelper(Class<?> beanClass) {
    if (beanClass == null) {
        return null;
    }
    try {
        final SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
        final JavaType simpleType = objectMapper.constructType(beanClass);
        final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializationConfig, simpleType, null);
        if (jsonSerializer == null) {
            return null;
        }
        if (jsonSerializer instanceof BeanSerializer) {
            return new BeanHelper((BeanSerializer) jsonSerializer);
        } else {
            final String jsonSerializerName = jsonSerializer.getClass().getName();
            throw new RuntimeException(String.format("Unknown serializer '%s' for class '%s'", jsonSerializerName, beanClass));
        }
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: JsonUtils.java    From fountain with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
   public static Object json2Object(String jsonString, JavaType type) {

	if (jsonString == null || "".equals(jsonString)) {
		return "";
	} else {
		try {
			objectMapper.getDeserializationConfig().set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			return objectMapper.readValue(jsonString, type);
		} catch (Exception e) {
			log.warn("json error:" + e.getMessage());
		}

	}
	return "";
}
 
Example #5
Source File: JsonUtils.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> string2Map(String json) {
	JavaType type = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #6
Source File: BackportedObjectReader.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor used by {@link ObjectMapper} for initial instantiation
 */
protected BackportedObjectReader(ObjectMapper mapper, JavaType valueType, Object valueToUpdate) {
    _rootDeserializers = ReflectionUtils.getField(ROOT_DESERIALIZERS, mapper);
    _provider = mapper.getDeserializerProvider();
    _jsonFactory = mapper.getJsonFactory();

    // must make a copy at this point, to prevent further changes from trickling down
    _config = mapper.copyDeserializationConfig();

    _valueType = valueType;
    _valueToUpdate = valueToUpdate;
    if (valueToUpdate != null && valueType.isArrayType()) {
        throw new IllegalArgumentException("Can not update an array value");
    }
}
 
Example #7
Source File: LogicalPlanSerializer.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public boolean useForType(JavaType t)
{
  if (t.getRawClass() == Object.class) {
    return true;
  }
  if (t.getRawClass().getName().startsWith("java.")) {
    return false;
  }
  if (t.isArrayType()) {
    return false;
  }
  return super.useForType(t);
}
 
Example #8
Source File: JsonUtils.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static <C extends Collection<E>, E> C string2Collection(String json, Class<C> collectionType,
		Class<E> elemType) {
	JavaType type = typeFactory.constructCollectionType(collectionType, elemType);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}													
}
 
Example #9
Source File: JsonUtils.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T[] string2Array(String json, Class<T> clazz) {
	JavaType type = ArrayType.construct(typeFactory.constructType(clazz));
	try {
		return (T[]) MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #10
Source File: JsonUtils.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static <K, V> Map<K, V> string2Map(String json, Class<K> keyClazz, Class<V> valueClazz) {
	JavaType type = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #11
Source File: LogicalPlanSerializer.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public boolean useForType(JavaType t)
{
  if (t.getRawClass() == Object.class) {
    return true;
  }
  if (t.getRawClass().getName().startsWith("java.")) {
    return false;
  }
  if (t.isArrayType()) {
    return false;
  }
  return super.useForType(t);
}
 
Example #12
Source File: JsonUtils.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T string2Object(String json, Class<T> clazz) {
	JavaType type = typeFactory.constructType(clazz);
	try {
		return (T) MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #13
Source File: CrossJsonUtil.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static <C extends Collection<E>, E> C string2Collection(String json, Class<C> collectionType,
		Class<E> elemType) {
	JavaType type = typeFactory.constructCollectionType(collectionType, elemType);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}													
}
 
Example #14
Source File: CrossJsonUtil.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T[] string2Array(String json, Class<T> clazz) {
	JavaType type = ArrayType.construct(typeFactory.constructType(clazz));
	try {
		return (T[]) MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #15
Source File: CrossJsonUtil.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static <K, V> Map<K, V> string2Map(String json, Class<K> keyClazz, Class<V> valueClazz) {
	JavaType type = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #16
Source File: CrossJsonUtil.java    From jforgame with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> string2Map(String json) {
	JavaType type = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
	try {
		return MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #17
Source File: CrossJsonUtil.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T string2Object(String json, Class<T> clazz) {
	JavaType type = typeFactory.constructType(clazz);
	try {
		return (T) MAPPER.readValue(json, type);
	} catch(Exception e) {
		return null;
	}
}
 
Example #18
Source File: JsonUtil.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
/**
 * 通过传入多个class对象来进行类型转换
 * @param str
 * @param collectionClass
 * @param classes
 * @param <T>
 * @return
 */
public static <T> T string2obj(String str, Class<?> collectionClass,Class<?>... classes){
    if (StringUtils.isEmpty(str) || collectionClass == null || classes==null) {
        return null;
    }
    JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, classes);
    try {
        return objectMapper.readValue(str, javaType);
    } catch (Exception e) {
        log.warn("parse string2obj warn",e);
        return null;
    }
}
 
Example #19
Source File: JsonUtil.java    From mmall20180107 with Apache License 2.0 5 votes vote down vote up
public static<T> T str2Obj(String str,Class<?> collectionClass,Class<?>... elementClasses ){

        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);

        try {
            return objectMapper.readValue(str,javaType);
        } catch (IOException e) {
            log.warn("Parse String to Object error",e);
            e.printStackTrace();
            return null;
        }
    }
 
Example #20
Source File: JsonUtil.java    From ml-blog with MIT License 5 votes vote down vote up
public static <T> T string2Obj(String json, Class<?> collectionClass, Class<?> ... elementClasses) {
    if (json == null || "".equals(json) || collectionClass == null) {
        return null;
    }

    JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);

    try {
        return objectMapper.readValue(json, javaType);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #21
Source File: JsonUtils.java    From fountain with Apache License 2.0 4 votes vote down vote up
public static Object convertValue(Object val,JavaType type){
	return objectMapper.convertValue(val, type);
}
 
Example #22
Source File: GenericServiceAPIResponseEntityDeserializer.java    From Eagle with Apache License 2.0 4 votes vote down vote up
@Override
public GenericServiceAPIResponseEntity deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    GenericServiceAPIResponseEntity entity = new GenericServiceAPIResponseEntity();
    ObjectCodec objectCodec = jp.getCodec();

    JsonNode rootNode = jp.getCodec().readTree(jp);
    if(rootNode.isObject()){
        Iterator<Map.Entry<String,JsonNode>> fields = rootNode.getFields();
        JsonNode objNode = null;
        while(fields.hasNext()){
            Map.Entry<String,JsonNode> field = fields.next();
            if (META_FIELD.equals(field.getKey()) && field.getValue() != null)
                entity.setMeta(objectCodec.readValue(field.getValue().traverse(), Map.class));
            else if(SUCCESS_FIELD.equals(field.getKey()) && field.getValue() != null){
                entity.setSuccess(field.getValue().getValueAsBoolean(false));
            }else if(EXCEPTION_FIELD.equals(field.getKey()) && field.getValue() != null){
                entity.setException(field.getValue().getTextValue());
            }else if(TYPE_FIELD.endsWith(field.getKey())  && field.getValue() != null){
                try {
                    entity.setType(Class.forName(field.getValue().getTextValue()));
                } catch (ClassNotFoundException e) {
                    throw new IOException(e);
                }
            }else if(OBJ_FIELD.equals(field.getKey()) && field.getValue() != null){
                objNode = field.getValue();
            }
        }

        if(objNode!=null) {
            JavaType collectionType=null;
            if (entity.getType() != null) {
                collectionType = TypeFactory.defaultInstance().constructCollectionType(LinkedList.class, entity.getType());
            }else{
                collectionType = TypeFactory.defaultInstance().constructCollectionType(LinkedList.class, Map.class);
            }
            List obj = objectCodec.readValue(objNode.traverse(), collectionType);
            entity.setObj(obj);
        }
    }else{
        throw new IOException("root node is not object");
    }
    return entity;
}
 
Example #23
Source File: AndroidJSONMappers.java    From squidb with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJSON(String jsonString, Type javaType) throws Exception {
    JavaType type = MAPPER.getTypeFactory().constructType(javaType);
    return MAPPER.readValue(jsonString, type);
}
 
Example #24
Source File: JacksonUtil.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * json数据转化为对象(JavaType)
 *
 * @param <T>       the type parameter
 * @param jsonValue the json value
 * @param valueType the value type
 *
 * @return t t
 *
 * @throws IOException the io exception
 */
public static <T> T parseJsonWithFormat(String jsonValue, JavaType valueType) throws IOException {
	Preconditions.checkArgument(StringUtils.isNotEmpty(jsonValue), "this argument is required; it must not be null");
	return (T) formatedMapper.readValue(jsonValue, valueType);
}
 
Example #25
Source File: JacksonUtil.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * json数据转化为对象(JavaType)
 *
 * @param <T>       the type parameter
 * @param jsonValue the json value
 * @param valueType the value type
 *
 * @return t t
 *
 * @throws IOException the io exception
 */
@SuppressWarnings("unchecked")
public static <T> T parseJson(String jsonValue, JavaType valueType) throws IOException {
	Preconditions.checkArgument(StringUtils.isNotEmpty(jsonValue), "this argument is required; it must not be null");
	return (T) defaultMapper.readValue(jsonValue, valueType);
}