Java Code Examples for org.codehaus.jackson.map.type.TypeFactory#defaultInstance()

The following examples show how to use org.codehaus.jackson.map.type.TypeFactory#defaultInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONUtils.java    From TestRailSDK with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: JSONUtils.java    From TestRailSDK with MIT License 5 votes vote down vote up
/**
 * 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");
}
 
Example 3
Source File: HttpMetricsIngestionHandler.java    From blueflood with Apache License 2.0 5 votes vote down vote up
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;
}