Search in sources :

Example 76 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestWebHDFS method toBlockLocationArray.

private BlockLocation[] toBlockLocationArray(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MapType subType = mapper.getTypeFactory().constructMapType(Map.class, String.class, BlockLocation[].class);
    MapType rootType = mapper.getTypeFactory().constructMapType(Map.class, mapper.constructType(String.class), mapper.constructType(subType));
    Map<String, Map<String, BlockLocation[]>> jsonMap = mapper.readValue(json, rootType);
    Map<String, BlockLocation[]> locationMap = jsonMap.get("BlockLocations");
    BlockLocation[] locationArray = locationMap.get(BlockLocation.class.getSimpleName());
    return locationArray;
}
Also used : BlockLocation(org.apache.hadoop.fs.BlockLocation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MapType(com.fasterxml.jackson.databind.type.MapType)

Example 77 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestJsonUtil method testGetXAttrFromJson.

@Test
public void testGetXAttrFromJson() throws IOException {
    String jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"}," + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}";
    ObjectReader reader = new ObjectMapper().readerFor(Map.class);
    Map<?, ?> json = reader.readValue(jsonString);
    // Get xattr: user.a2
    byte[] value = JsonUtilClient.getXAttr(json, "user.a2");
    Assert.assertArrayEquals(XAttrCodec.decodeValue("0x313131"), value);
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 78 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class HttpExceptionUtils method validateResponse.

/**
   * Validates the status of an <code>HttpURLConnection</code> against an
   * expected HTTP status code. If the current status code is not the expected
   * one it throws an exception with a detail message using Server side error
   * messages if available.
   * <p/>
   * <b>NOTE:</b> this method will throw the deserialized exception even if not
   * declared in the <code>throws</code> of the method signature.
   *
   * @param conn the <code>HttpURLConnection</code>.
   * @param expectedStatus the expected HTTP status code.
   * @throws IOException thrown if the current status code does not match the
   * expected one.
   */
@SuppressWarnings("unchecked")
public static void validateResponse(HttpURLConnection conn, int expectedStatus) throws IOException {
    if (conn.getResponseCode() != expectedStatus) {
        Exception toThrow;
        InputStream es = null;
        try {
            es = conn.getErrorStream();
            ObjectMapper mapper = new ObjectMapper();
            Map json = mapper.readValue(es, Map.class);
            json = (Map) json.get(ERROR_JSON);
            String exClass = (String) json.get(ERROR_CLASSNAME_JSON);
            String exMsg = (String) json.get(ERROR_MESSAGE_JSON);
            if (exClass != null) {
                try {
                    ClassLoader cl = HttpExceptionUtils.class.getClassLoader();
                    Class klass = cl.loadClass(exClass);
                    Constructor constr = klass.getConstructor(String.class);
                    toThrow = (Exception) constr.newInstance(exMsg);
                } catch (Exception ex) {
                    toThrow = new IOException(String.format("HTTP status [%d], exception [%s], message [%s] ", conn.getResponseCode(), exClass, exMsg));
                }
            } else {
                String msg = (exMsg != null) ? exMsg : conn.getResponseMessage();
                toThrow = new IOException(String.format("HTTP status [%d], message [%s]", conn.getResponseCode(), msg));
            }
        } catch (Exception ex) {
            toThrow = new IOException(String.format("HTTP status [%d], message [%s]", conn.getResponseCode(), conn.getResponseMessage()));
        } finally {
            if (es != null) {
                try {
                    es.close();
                } catch (IOException ex) {
                //ignore
                }
            }
        }
        throwEx(toThrow);
    }
}
Also used : InputStream(java.io.InputStream) Constructor(java.lang.reflect.Constructor) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 79 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project hadoop by apache.

the class TestConfiguration method testDumpConfiguration.

public void testDumpConfiguration() throws IOException {
    StringWriter outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    String jsonStr = outWriter.toString();
    ObjectMapper mapper = new ObjectMapper();
    JsonConfiguration jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    int defaultLength = jconf.getProperties().length;
    // add 3 keys to the existing configuration properties
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.key1", "value1");
    appendProperty("test.key2", "value2", true);
    appendProperty("test.key3", "value3");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    out.close();
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    int length = jconf.getProperties().length;
    // check for consistency in the number of properties parsed in Json format.
    assertEquals(length, defaultLength + 3);
    //change few keys in another resource file
    out = new BufferedWriter(new FileWriter(CONFIG2));
    startConfig();
    appendProperty("test.key1", "newValue1");
    appendProperty("test.key2", "newValue2");
    endConfig();
    Path fileResource1 = new Path(CONFIG2);
    conf.addResource(fileResource1);
    out.close();
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    // put the keys and their corresponding attributes into a hashmap for their 
    // efficient retrieval
    HashMap<String, JsonProperty> confDump = new HashMap<String, JsonProperty>();
    for (JsonProperty prop : jconf.getProperties()) {
        confDump.put(prop.getKey(), prop);
    }
    // check if the value and resource of test.key1 is changed
    assertEquals("newValue1", confDump.get("test.key1").getValue());
    assertEquals(false, confDump.get("test.key1").getIsFinal());
    assertEquals(fileResource1.toString(), confDump.get("test.key1").getResource());
    // check if final parameter test.key2 is not changed, since it is first 
    // loaded as final parameter
    assertEquals("value2", confDump.get("test.key2").getValue());
    assertEquals(true, confDump.get("test.key2").getIsFinal());
    assertEquals(fileResource.toString(), confDump.get("test.key2").getResource());
    // check for other keys which are not modified later
    assertEquals("value3", confDump.get("test.key3").getValue());
    assertEquals(false, confDump.get("test.key3").getIsFinal());
    assertEquals(fileResource.toString(), confDump.get("test.key3").getResource());
    // check for resource to be "Unknown" for keys which are loaded using 'set' 
    // and expansion of properties
    conf.set("test.key4", "value4");
    conf.set("test.key5", "value5");
    conf.set("test.key6", "${test.key5}");
    outWriter = new StringWriter();
    Configuration.dumpConfiguration(conf, outWriter);
    jsonStr = outWriter.toString();
    mapper = new ObjectMapper();
    jconf = mapper.readValue(jsonStr, JsonConfiguration.class);
    confDump = new HashMap<String, JsonProperty>();
    for (JsonProperty prop : jconf.getProperties()) {
        confDump.put(prop.getKey(), prop);
    }
    assertEquals("value5", confDump.get("test.key6").getValue());
    assertEquals("programmatically", confDump.get("test.key4").getResource());
    outWriter.close();
}
Also used : Path(org.apache.hadoop.fs.Path) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BufferedWriter(java.io.BufferedWriter)

Example 80 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project flink by apache.

the class CheckpointStatsHandlerTest method testCheckpointStatsRequest.

/**
	 * Tests a complete checkpoint stats snapshot.
	 */
@Test
public void testCheckpointStatsRequest() throws Exception {
    TestCheckpointStats testCheckpointStats = createTestCheckpointStats();
    CheckpointStatsHandler handler = new CheckpointStatsHandler(mock(ExecutionGraphHolder.class));
    String json = handler.handleRequest(testCheckpointStats.graph, Collections.<String, String>emptyMap());
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(json);
    compareCheckpointStats(testCheckpointStats, rootNode);
}
Also used : ExecutionGraphHolder(org.apache.flink.runtime.webmonitor.ExecutionGraphHolder) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1706 Test (org.junit.Test)641 JsonNode (com.fasterxml.jackson.databind.JsonNode)270 IOException (java.io.IOException)238 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)183 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)124 HashMap (java.util.HashMap)119 ArrayList (java.util.ArrayList)81 File (java.io.File)72 InputStream (java.io.InputStream)65 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)55 JCodeModel (com.sun.codemodel.JCodeModel)52 List (java.util.List)49 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)47 JPackage (com.sun.codemodel.JPackage)44 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)41 JsonException (jmri.server.json.JsonException)41 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)37