Refine search
/** * 将json数据转换成pojo对象list */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; }
public static <type> type jsonToObject(String json, Class<?> cls) { type obj = null; JavaType javaType = objMapper.getTypeFactory().constructType(cls); try { obj = objMapper.readValue(json, javaType); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } return obj; }
/** * Deserializes a JSON object list from a JSON string. * * @param <T> child type of {@link JsonTemplate} * @param jsonString a JSON string * @param templateClass the template to deserialize the string to * @return the template filled with the values parsed from {@code jsonString} * @throws IOException if an error occurred during parsing the JSON */ public static <T extends JsonTemplate> List<T> readListOfJson( String jsonString, Class<T> templateClass) throws IOException { CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, templateClass); return objectMapper.readValue(jsonString, listType); }
@Override public <T> T parse(String json, Type type) { try { return mapper.readValue(json, mapper.getTypeFactory().constructType(type)); } catch (IOException e) { throw new JSONException(e); } }
@Override public <T> T parse(String json, Type type) { try { return mapper.readValue(json, mapper.getTypeFactory().constructType(type)); } catch (IOException e) { throw new JSONException(e); } }
@Override public Object deserialize(byte[] bytes, Type returnType) throws Exception { if (null == bytes || bytes.length == 0) { return null; } Type[] agsType = new Type[]{returnType}; JavaType javaType = MAPPER.getTypeFactory() .constructType(ParameterizedTypeImpl.make(CacheWrapper.class, agsType, null)); return MAPPER.readValue(bytes, javaType); }
@Override public Object deserialize(final byte[] bytes, final Type returnType) throws Exception { if (null == bytes || bytes.length == 0) { return null; } Type[] agsType = new Type[]{returnType}; JavaType javaType = MAPPER.getTypeFactory() .constructType(ParameterizedTypeImpl.make(CacheWrapper.class, agsType, null)); return MAPPER.readValue(bytes, javaType); }
public static <type> type jsonToObjectHashMap(String json, Class<?> keyClass, Class<?> valueClass) { type obj = null; JavaType javaType = objMapper.getTypeFactory().constructParametricType(HashMap.class, keyClass, valueClass); try { obj = objMapper.readValue(json, javaType); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } return obj; }
public static <type> type jsonToObjectList(String json, Class<?> collectionClass, Class<?>... elementClass) { type obj = null; JavaType javaType = objMapper.getTypeFactory().constructParametricType( collectionClass, elementClass); try { obj = objMapper.readValue(json, javaType); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } return obj; }
public List<RouteMethod> read(String classname) { String filename = "/" + classname.replace(".", "/") + ".json"; URL resource = getClass().getResource(filename); if (resource != null) { try { return mapper.readValue(resource, mapper.getTypeFactory().constructCollectionType(ArrayList.class, RouteMethod.class)); } catch (IOException e) { log.error("read of {} resulted in exception", filename, e); } } return null; }
@RequestMapping("customize") @ResponseBody public String customize() throws JsonParseException, JsonMappingException, IOException { String jsonStr = "[{\"userName\":\"mrbird\",\"age\":26},{\"userName\":\"scott\",\"age\":27}]"; JavaType type = mapper.getTypeFactory().constructParametricType(List.class, User.class); List<User> list = mapper.readValue(jsonStr, type); String msg = ""; for (User user : list) { msg += user.getUserName(); } return msg; } }
@Override public Object[] deepCloneMethodArgs(Method method, Object[] args) throws Exception { if (null == args || args.length == 0) { return args; } Type[] genericParameterTypes = method.getGenericParameterTypes(); if (args.length != genericParameterTypes.length) { throw new Exception("the length of " + method.getDeclaringClass().getName() + "." + method.getName() + " must " + genericParameterTypes.length); } Class<?> clazz = args.getClass(); Object[] res = ((Object) clazz == (Object) Object[].class) ? (Object[]) new Object[args.length] : (Object[]) Array.newInstance(clazz.getComponentType(), args.length); int len = genericParameterTypes.length; for (int i = 0; i < len; i++) { Type genericParameterType = genericParameterTypes[i]; Object obj = args[i]; if (genericParameterType instanceof ParameterizedType) { String json = MAPPER.writeValueAsString(obj); JavaType javaType = MAPPER.getTypeFactory().constructType(genericParameterType); res[i] = MAPPER.readValue(json, javaType); } else { res[i] = deepClone(obj, null); } } return res; }
@Override public Object[] deepCloneMethodArgs(Method method, Object[] args) throws Exception { if (null == args || args.length == 0) { return args; } Type[] genericParameterTypes = method.getGenericParameterTypes(); if (args.length != genericParameterTypes.length) { throw new Exception("the length of " + method.getDeclaringClass().getName() + "." + method.getName() + " must " + genericParameterTypes.length); } Class<?> clazz = args.getClass(); Object[] res = ((Object) clazz == (Object) Object[].class) ? (Object[]) new Object[args.length] : (Object[]) Array.newInstance(clazz.getComponentType(), args.length); int len = genericParameterTypes.length; for (int i = 0; i < len; i++) { Type genericParameterType = genericParameterTypes[i]; Object obj = args[i]; if (genericParameterType instanceof ParameterizedType) { byte[] tmp = MAPPER.writeValueAsBytes(obj); JavaType javaType = MAPPER.getTypeFactory().constructType(genericParameterType); res[i] = MAPPER.readValue(tmp, javaType); } else { res[i] = deepClone(obj, null); } } return res; }
/** * string --> List<Bean>... * * @param jsonStr * @param parametrized * @param parameterClasses * @param <T> * @return */ public static <T> T readValue(String jsonStr, Class<?> parametrized, Class<?>... parameterClasses) { try { JavaType javaType = getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses); return getInstance().readValue(jsonStr, javaType); } catch (JsonParseException e) { logger.error(e.getMessage(), e); } catch (JsonMappingException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } return null; }
public StateTestSuite(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, StateTestCase.class); testCases = new ObjectMapper().readValue(json, type); }
public StateTestData(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, StateTestDataEntry.class); testData = new ObjectMapper().readValue(json, type); }
public BlockTestSuite(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, BlockTestCase.class); testCases = new ObjectMapper().readValue(json, type); }
public TrieTestSuite(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, TrieTestCase.class); Map<String, TrieTestCase> caseMap = new ObjectMapper().readValue(json, type); for (Map.Entry<String, TrieTestCase> e : caseMap.entrySet()) { e.getValue().setName(e.getKey()); testCases.add(e.getValue()); } testCases.sort(Comparator.comparing(TrieTestCase::getName)); }
public DifficultyTestSuite(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, DifficultyTestCase.class); Map<String, DifficultyTestCase> caseMap = new ObjectMapper().readValue(json, type); for (Map.Entry<String, DifficultyTestCase> e : caseMap.entrySet()) { e.getValue().setName(e.getKey()); testCases.add(e.getValue()); } testCases.sort(Comparator.comparing(DifficultyTestCase::getName)); }
public ABITestSuite(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory(). constructMapType(HashMap.class, String.class, ABITestCase.class); Map<String, ABITestCase> caseMap = new ObjectMapper().readValue(json, type); for (Map.Entry<String, ABITestCase> e : caseMap.entrySet()) { e.getValue().setName(e.getKey()); testCases.add(e.getValue()); } testCases.sort(Comparator.comparing(ABITestCase::getName)); }