Search in sources :

Example 6 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader 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 7 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project knime-core by knime.

the class SubnodeLayoutJSONEditorPage method isJSONValid.

/**
 * @return true, if current JSON layout structure is valid
 */
protected boolean isJSONValid() {
    ObjectMapper mapper = JSONLayoutPage.getConfiguredObjectMapper();
    ObjectReader reader = mapper.readerForUpdating(new JSONLayoutPage());
    try {
        String json = isWindows() ? m_textArea.getText() : m_jsonDocument;
        JSONLayoutPage page = reader.readValue(json);
        m_documentNodeIDs.clear();
        if (page.getRows() != null) {
            for (JSONLayoutRow row : page.getRows()) {
                populateDocumentNodeIDs(row);
            }
            compareNodeIDs();
        }
        return true;
    } catch (IOException | NumberFormatException e) {
        String errorMessage;
        if (e instanceof JsonProcessingException) {
            JsonProcessingException jsonException = (JsonProcessingException) e;
            Throwable cause = null;
            Throwable newCause = jsonException.getCause();
            while (newCause instanceof JsonProcessingException) {
                if (cause == newCause) {
                    break;
                }
                cause = newCause;
                newCause = cause.getCause();
            }
            if (cause instanceof JsonProcessingException) {
                jsonException = (JsonProcessingException) cause;
            }
            errorMessage = jsonException.getOriginalMessage().split("\n")[0];
            JsonLocation location = jsonException.getLocation();
            if (location != null) {
                errorMessage += " at line: " + (location.getLineNr() + 1) + " column: " + location.getColumnNr();
            }
        } else {
            String message = e.getMessage();
            errorMessage = message;
        }
        if (m_statusLine != null && !m_statusLine.isDisposed()) {
            m_statusLine.setForeground(new Color(Display.getCurrent(), 255, 0, 0));
            m_statusLine.setText(errorMessage);
            int textWidth = isWindows() ? m_textArea.getSize().width : m_text.getSize().x;
            Point newSize = m_statusLine.computeSize(textWidth, m_statusLine.getSize().y, true);
            m_statusLine.setSize(newSize);
        }
    }
    return false;
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation) JSONLayoutPage(org.knime.js.core.layout.bs.JSONLayoutPage) Color(org.eclipse.swt.graphics.Color) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) JSONLayoutRow(org.knime.js.core.layout.bs.JSONLayoutRow) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project data-prep by Talend.

the class MixedContentMapTest method testRead.

@Test
public void testRead() throws Exception {
    final ObjectReader reader = mapper.reader(MixedContentMap.class);
    final MixedContentMap map = reader.readValue(MixedContentMapTest.class.getResourceAsStream("mixedMapContent.json"));
    assertThat(map, notNullValue());
    assertThat(map.get("string"), is("string value"));
    assertThat(map.get("numeric"), is("10"));
    assertThat(map.get("boolean"), is("true"));
    assertThat(map.get("double"), is("10.1"));
    assertThat(map.get("null"), nullValue());
    assertThat(map.get("empty"), is(""));
    final String object = map.get("object");
    assertThat(object, sameJSONAs("{\"eq\": { \"field\": \"nbCommands\",\"value\": \"13\" }}"));
    final String array = map.get("array");
    assertThat(array, sameJSONAs("[1, 2, 3]"));
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Test(org.junit.Test)

Example 9 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project keycloak by keycloak.

the class Serialization method from.

public static <T> T from(T orig, T target) {
    if (orig == null) {
        return null;
    }
    @SuppressWarnings("unchecked") final Class<T> origClass = (Class<T>) orig.getClass();
    // Naive solution but will do.
    try {
        ObjectReader reader = MAPPER.readerForUpdating(target);
        ObjectWriter writer = WRITERS.computeIfAbsent(origClass, MAPPER::writerFor);
        final T res;
        res = reader.readValue(writer.writeValueAsBytes(orig));
        if (res != target) {
            throw new IllegalStateException("Should clone into desired target");
        }
        return res;
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException)

Example 10 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project components by Talend.

the class AbstractSpringIntegrationTests method buildTestDataSetFormData.

protected UiSpecsPropertiesDto buildTestDataSetFormData() throws java.io.IOException {
    UiSpecsPropertiesDto formDataContainer = new UiSpecsPropertiesDto();
    ObjectReader reader = mapper.readerFor(ObjectNode.class);
    formDataContainer.setDependencies(singletonList(reader.readValue(TEST_DATA_STORE_PROPERTIES)));
    formDataContainer.setProperties(reader.readValue(TEST_DATA_SET_PROPERTIES));
    return formDataContainer;
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) UiSpecsPropertiesDto(org.talend.components.service.rest.dto.UiSpecsPropertiesDto)

Aggregations

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)83 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)33 IOException (java.io.IOException)32 Test (org.junit.Test)23 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 ArrayList (java.util.ArrayList)8 JavaType (com.fasterxml.jackson.databind.JavaType)7 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)4 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)4 Method (java.lang.reflect.Method)4 Collectors (java.util.stream.Collectors)4 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)3 JSONLayoutPage (org.knime.js.core.layout.bs.JSONLayoutPage)3