List of usage examples for com.fasterxml.jackson.databind ObjectMapper convertValue
@SuppressWarnings("unchecked") public <T> T convertValue(Object fromValue, JavaType toValueType) throws IllegalArgumentException
From source file:com.sugaronrest.restapicalls.methodcalls.InsertLinkedEntry.java
/** * Formats and return selected fields./*w w w.j a v a 2 s. c o m*/ * * @param entity Java object to update. * @param selectFields Selected fields. * @return Formatted selected fields. */ private static Map<String, Object> EntityToNameValueList(Object entity, List<String> selectFields) { if (entity == null) { return null; } ObjectMapper mapper = JsonObjectMapper.getMapper(); Map<String, Object> tempEntity = mapper.convertValue(entity, Map.class); if (tempEntity == null) { return null; } boolean useSelectedFields = (selectFields != null) && (selectFields.size() > 0); Map<String, Object> mappedEntity = new HashMap<String, Object>(); for (Map.Entry<String, Object> mapEntry : tempEntity.entrySet()) { String key = mapEntry.getKey(); // The identifier must always be added. if (!key.equalsIgnoreCase("id")) { if (useSelectedFields) { if (!selectFields.contains(key)) { continue; } } } Map<String, Object> namevalueDic = new HashMap<String, Object>(); namevalueDic.put("name", key); namevalueDic.put("value", mapEntry.getValue()); mappedEntity.put(key, namevalueDic); } return mappedEntity; }
From source file:org.mule.modules.cookbook.CookbookConnector.java
@Transformer(sourceTypes = { Recipe.class }) public static Map<String, Object> recipeToMap(Recipe recipe) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> result = mapper.convertValue(recipe, new TypeReference<Map<String, Object>>() { });//from w w w.j a v a 2 s .c o m return result; }
From source file:org.mule.modules.cookbook.CookbookConnector.java
@Transformer(sourceTypes = { List.class }) public static List<Map<String, Object>> recipesToMaps(List<Recipe> list) { ObjectMapper mapper = new ObjectMapper(); List<Map<String, Object>> result = mapper.convertValue(list, new TypeReference<List<Map<String, Object>>>() { });/*from w ww . j a v a 2 s .co m*/ return result; }
From source file:com.sugaronrest.restapicalls.methodcalls.InsertEntries.java
/** * Formats and return selected fields.//from w w w.java 2 s . co m * * @param entities List of Java objects to update. * @param selectFields Selected fields. * @return Formatted selected fields. */ private static List<Map<String, Object>> EntitiesToNameValueList(List<Object> entities, List<String> selectFields) { if (entities == null) { return null; } List<Map<String, Object>> mappedEntities = new ArrayList<Map<String, Object>>(); boolean useSelectedFields = (selectFields != null) && (selectFields.size() > 0); ObjectMapper mapper = JsonObjectMapper.getMapper(); for (Object entity : entities) { Map<String, Object> tempEntity = mapper.convertValue(entity, Map.class); Map<String, Object> mappedEntity = new HashMap<String, Object>(); for (Map.Entry<String, Object> mapEntry : tempEntity.entrySet()) { String key = mapEntry.getKey(); if (useSelectedFields) { if (!selectFields.contains(key)) { continue; } } if (key.equalsIgnoreCase("id")) { continue; } Map<String, Object> namevalueDic = new HashMap<String, Object>(); namevalueDic.put("name", key); namevalueDic.put("value", mapEntry.getValue()); mappedEntity.put(key, namevalueDic); } mappedEntities.add(mappedEntity); } return mappedEntities; }
From source file:com.sugaronrest.restapicalls.methodcalls.UpdateEntries.java
/** * Formats and return selected fields./*from w w w . j a v a 2s . c o m*/ * * @param entities List of Java objects to update. * @param selectFields Selected fields. * @return Formatted selected fields. */ private static List<Map<String, Object>> EntitiesToNameValueList(List<Object> entities, List<String> selectFields) { if (entities == null) { return null; } List<Map<String, Object>> mappedEntities = new ArrayList<Map<String, Object>>(); boolean useSelectedFields = (selectFields != null) && (selectFields.size() > 0); ObjectMapper mapper = JsonObjectMapper.getMapper(); for (Object entity : entities) { Map<String, Object> tempEntity = mapper.convertValue(entity, Map.class); Map<String, Object> mappedEntity = new HashMap<String, Object>(); for (Map.Entry<String, Object> mapEntry : tempEntity.entrySet()) { String key = mapEntry.getKey(); // The identifier must always be added. if (!key.equalsIgnoreCase("id")) { if (useSelectedFields) { if (!selectFields.contains(key)) { continue; } } } Map<String, Object> namevalueDic = new HashMap<String, Object>(); namevalueDic.put("name", key); namevalueDic.put("value", mapEntry.getValue()); mappedEntity.put(key, namevalueDic); } mappedEntities.add(mappedEntity); } return mappedEntities; }
From source file:com.almende.util.TypeUtil.java
/** * Inject./*from www .j a v a 2 s . c o m*/ * * @param <T> the generic type * @param value the value * @param fullType the full type * @return the t */ @SuppressWarnings("unchecked") public static <T> T inject(final Object value, final JavaType fullType) { if (value == null) { return null; } if (fullType.hasRawClass(Void.class)) { return null; } final ObjectMapper mapper = JOM.getInstance(); if (value instanceof JsonNode) { if (((JsonNode) value).isNull()) { return null; } try { return mapper.convertValue(value, fullType); } catch (final Exception e) { ClassCastException cce = new ClassCastException( "Failed to convert value:" + value + " -----> " + fullType); cce.initCause(e); throw cce; } } if (fullType.getRawClass().isAssignableFrom(value.getClass())) { return (T) value; } else { throw new ClassCastException(value.getClass().getCanonicalName() + " can't be converted to: " + fullType.getRawClass().getCanonicalName()); } }
From source file:org.smartdeveloperhub.vocabulary.config.ConfigurationFactory.java
static <T> T convert(final Object source, final Type type) { final ObjectMapper mapper = parsingMapper(); final JavaType constructType = mapper.getTypeFactory().constructType(type); checkArgument(mapper.canDeserialize(constructType), "%s is not a valid configuration class", constructType.toCanonical()); return mapper.convertValue(source, constructType); }
From source file:com.netflix.spinnaker.halyard.core.GlobalApplicationOptions.java
public static GlobalApplicationOptions getInstance() { if (GlobalApplicationOptions.options == null) { Yaml yamlParser = new Yaml(new SafeConstructor()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, false); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); try {// www . j av a 2s .c o m GlobalApplicationOptions.options = objectMapper.convertValue( yamlParser.load(FileUtils.openInputStream(new File(CONFIG_PATH))), GlobalApplicationOptions.class); } catch (IOException e) { GlobalApplicationOptions.options = new GlobalApplicationOptions(); } } return GlobalApplicationOptions.options; }
From source file:org.apache.streams.converter.TypeConverterUtil.java
public static Object convert(Object object, Class outClass, ObjectMapper mapper) { ObjectNode node = null;/*from ww w . ja v a 2 s . c om*/ Object outDoc = null; if (object instanceof String) { try { node = mapper.readValue((String) object, ObjectNode.class); } catch (IOException e) { LOGGER.warn(e.getMessage()); LOGGER.warn(object.toString()); } } else { node = mapper.convertValue(object, ObjectNode.class); } if (node != null) { try { if (outClass == String.class) outDoc = mapper.writeValueAsString(node); else outDoc = mapper.convertValue(node, outClass); } catch (Throwable e) { LOGGER.warn(e.getMessage()); LOGGER.warn(node.toString()); } } return outDoc; }
From source file:io.github.robwin.swagger.loader.Swagger20Parser.java
private static Swagger convertToSwagger(String data) throws IOException { ObjectMapper mapper; if (data.trim().startsWith("{")) { mapper = Json.mapper();//from ww w . jav a2 s . c o m } else { mapper = Yaml.mapper(); } JsonNode rootNode = mapper.readTree(data); // must have swagger node set JsonNode swaggerNode = rootNode.get("swagger"); if (swaggerNode == null) { throw new IllegalArgumentException("Swagger String has an invalid format."); } else { return mapper.convertValue(rootNode, Swagger.class); } }