當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。