当前位置: 首页>>代码示例>>Java>>正文


Java ObjectMapper.treeToValue方法代码示例

本文整理汇总了Java中com.fasterxml.jackson.databind.ObjectMapper.treeToValue方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectMapper.treeToValue方法的具体用法?Java ObjectMapper.treeToValue怎么用?Java ObjectMapper.treeToValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.fasterxml.jackson.databind.ObjectMapper的用法示例。


在下文中一共展示了ObjectMapper.treeToValue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: deserialize

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public Selector deserialize(JsonParser p, DeserializationContext ctxt)
    throws IOException {
  ObjectMapper mapper = (ObjectMapper) p.getCodec();
  ObjectNode obj = mapper.readTree(p);
  String typeName;
  if (obj.get("@type").isArray()) {
    // Find the actual selector type
    typeName = StreamSupport.stream(obj.get("@type").spliterator(), false)
        .filter(v -> !v.textValue().equals("cnt:ContentAsText"))
        .findFirst().orElse(new TextNode("UNKNOWN")).textValue();
    // Make @type a text value so that Jackson doesn't bail out further down the line
    obj.set("@type", new TextNode(typeName));
  } else {
    typeName = obj.get("@type").textValue();
  }
  if (MAPPING.containsKey(typeName)) {
    return mapper.treeToValue(obj, MAPPING.get(typeName));
  } else {
    throw new IllegalArgumentException("Cannot deserialize Selector.");
  }
}
 
开发者ID:dbmdz,项目名称:iiif-apis,代码行数:22,代码来源:SelectorDeserializer.java

示例2: string2Map

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
 * Десериализатор TreeNode массива в Map где четный элемент ключ а не четный
 * значение
 * 
 * @param <T>
 *            тип ключя карты
 * @param <V>
 *            тип значения карты
 * @param node
 *            узел
 * @param keyClass
 *            класс для ключа карты
 * @param valueClass
 *            класс для значения карты
 * @throws IOException
 *             исключение парсера
 * @return карта
 */
private static <T, V> Map<T, V> string2Map(TreeNode node, Class<T> keyClass, Class<V> valueClass)
		throws IOException {
	ObjectMapper mapper = new ObjectMapper();
	Map<T, V> ret = new HashMap<T, V>();
	if (node.isArray()) {
		T key = null;
		ArrayNode key2value = (ArrayNode) node;
		for (int i = 0; i < key2value.size(); i++) {
			if (i % 2 == 0) {
				key = mapper.treeToValue(key2value.get(i), keyClass);
			} else {
				V value = mapper.treeToValue(key2value.get(i), valueClass);
				ret.put(key, value);
			}
		}
	}
	return ret;
}
 
开发者ID:onixred,项目名称:golos4j,代码行数:37,代码来源:Util.java

示例3: deserialize

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Override
public Service deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  ObjectMapper mapper = (ObjectMapper) p.getCodec();
  if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
    return new GenericService(null, p.getValueAsString());
  }
  ObjectNode obj = mapper.readTree(p);
  if (isV1ImageService(obj)) {
    return parseV1Service(obj);
  } else if (isImageService(obj)) {
    return mapper.treeToValue(obj, ImageService.class);
  }

  String context = null;
  if (obj.has("@context")) {
    context = obj.get("@context").asText();
  }
  JsonNode profileNode = obj.get("profile");
  String profile = null;
  if (profileNode != null) {
    profile = profileNode.asText();
  }
  if (Objects.equals(context, ContentSearchService.CONTEXT)) {
    if (Objects.equals(profile, AutocompleteService.PROFILE)) {
      return mapper.treeToValue(obj, AutocompleteService.class);
    } else {
      return mapper.treeToValue(obj, ContentSearchService.class);
    }
  } else if (Objects.equals(context, AccessCookieService.CONTEXT)) {
    return mapper.treeToValue(obj, AccessCookieService.class);
  } else if (Objects.equals(context, GeoService.CONTEXT)) {
    return mapper.treeToValue(obj, GeoService.class);
  } else if (Objects.equals(context, PhysicalDimensionsService.CONTEXT)) {
    return mapper.treeToValue(obj, PhysicalDimensionsService.class);
  } else {
    return mapper.treeToValue(obj, GenericService.class);
  }
}
 
开发者ID:dbmdz,项目名称:iiif-apis,代码行数:39,代码来源:ServiceDeserializer.java

示例4: valueCanBeSerializedAndDeserialized

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Test
public void valueCanBeSerializedAndDeserialized() throws NoSuchMethodException, IOException, IntrospectionException, IllegalAccessException, InvocationTargetException {

    ObjectMapper objectMapper = new ObjectMapper();

    ObjectNode node = objectMapper.createObjectNode();
    node.put(propertyName, jsonValue.toString());

    Object pojo = objectMapper.treeToValue(node, classWithFormattedProperties);

    Method getter = new PropertyDescriptor(propertyName, classWithFormattedProperties).getReadMethod();

    assertThat(getter.invoke(pojo).toString(), is(equalTo(javaValue.toString())));

    JsonNode jsonVersion = objectMapper.valueToTree(pojo);

    assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue.toString())));

}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:20,代码来源:FormatIT.java

示例5: roundTripAssertions

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static void roundTripAssertions( ObjectMapper objectMapper, String propertyName, String jsonValue, Object javaValue) throws Exception {

        ObjectNode node = objectMapper.createObjectNode();
        node.put(propertyName, jsonValue);

        Object pojo = objectMapper.treeToValue(node, classWithMediaProperties);

        Method getter = new PropertyDescriptor(propertyName, classWithMediaProperties).getReadMethod();

        assertThat(getter.invoke(pojo), is(equalTo(javaValue)));

        JsonNode jsonVersion = objectMapper.valueToTree(pojo);

        assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue)));
    }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:MediaIT.java

示例6: node2String

import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
 * Конвертировать TreeNode в строку
 * 
 * @param node
 *            узел
 * @throws IOException
 *             исключение парсера
 * @return строка
 */
public static String node2String(TreeNode node) throws IOException {
	ObjectMapper mapper = new ObjectMapper();
	return mapper.treeToValue(node, String.class);
}
 
开发者ID:onixred,项目名称:golos4j,代码行数:14,代码来源:Util.java


注:本文中的com.fasterxml.jackson.databind.ObjectMapper.treeToValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。