本文整理汇总了Java中org.codehaus.jackson.map.type.TypeFactory.defaultInstance方法的典型用法代码示例。如果您正苦于以下问题:Java TypeFactory.defaultInstance方法的具体用法?Java TypeFactory.defaultInstance怎么用?Java TypeFactory.defaultInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.jackson.map.type.TypeFactory
的用法示例。
在下文中一共展示了TypeFactory.defaultInstance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMappedJsonObject
import org.codehaus.jackson.map.type.TypeFactory; //导入方法依赖的package包/类
/**
* Takes a JSON string and attempts to "map" it to the given class. It assumes the JSON
* string is valid, and that it maps to the object you've indicated. If you want to run outside of "strict" mode,
* pass false for the failOnUnknownProperties flag
* @param jsonObjectClass The Class you wish to map the contents to
* @param json The JSON you wish to map to the given Class
* @param failOnUnknownProperties Whether or not to throw an exception if an unknown JSON attribute is encountered
* @return An instance of the given Class, based on the attributes of the given JSON
*/
public static <T> T getMappedJsonObject( Class<T> jsonObjectClass, String json, boolean failOnUnknownProperties ) {
TypeFactory t = TypeFactory.defaultInstance();
ObjectMapper mapper = new ObjectMapper();
mapper.configure( DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties );
T mappedObject;
try {
mappedObject = mapper.readValue( json, t.constructType( jsonObjectClass ) );
} catch ( Exception e ) {
throw new RuntimeException( "Could not instantiate object of class [" + jsonObjectClass.getName()+ "]: " + e );
}
return mappedObject;
}
示例2: getMappedJsonObjectList
import org.codehaus.jackson.map.type.TypeFactory; //导入方法依赖的package包/类
/**
* Returns a list of objects of the specified type. If you send "false" in the 3rd parameter, it will be
* forgiving of JSON properties that are not defined or inaccessible in the specified jsonObjectClass
* @param jsonObjectClass The Class you wish to map the json to
* @param json The JSON you wish to map to the given Class
* @param failOnUnknownProperties Whether or not to throw an exception if an unknown JSON attribute is encountered
* @return An instance of the given Class, based on the attributes of the given JSON
*/
public static <T> List<T> getMappedJsonObjectList(Class<T> jsonObjectClass, String json, boolean failOnUnknownProperties) {
List<T> list;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
TypeFactory t = TypeFactory.defaultInstance();
try {
list = mapper.readValue(json, t.constructCollectionType(ArrayList.class, jsonObjectClass));
return list;
} catch (Exception ex) {
ex.printStackTrace();
}
throw new RuntimeException("Could not process JSON");
}
示例3: HttpMetricsIngestionHandler
import org.codehaus.jackson.map.type.TypeFactory; //导入方法依赖的package包/类
public HttpMetricsIngestionHandler(HttpMetricsIngestionServer.Processor processor, TimeValue timeout, boolean enablePerTenantMetrics) {
this.mapper = new ObjectMapper();
this.typeFactory = TypeFactory.defaultInstance();
this.timeout = timeout;
this.processor = processor;
this.enablePerTenantMetrics = enablePerTenantMetrics;
}