Search in sources :

Example 11 with ObjectReader

use of com.fasterxml.jackson.databind.ObjectReader in project uPortal by Jasig.

the class JacksonColumnMapper method fromNonNullValue.

@Override
public final Object fromNonNullValue(String s) {
    try {
        final JsonWrapper jsonWrapper = objectReader.readValue(s);
        final ObjectReader typeReader = typedObjectReaders.getUnchecked(jsonWrapper.getType());
        return typeReader.readValue(jsonWrapper.getValue());
    } catch (JsonProcessingException e) {
        throw new IllegalArgumentException("Could not read from JSON: " + s, e);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not read from JSON: " + s, e);
    }
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 12 with ObjectReader

use of com.fasterxml.jackson.databind.ObjectReader in project dhis2-core by dhis2.

the class DefaultCsvEventService method readEvents.

@Override
public Events readEvents(InputStream inputStream, boolean skipFirst) throws IOException {
    Events events = new Events();
    ObjectReader reader = CSV_MAPPER.readerFor(CsvEventDataValue.class).with(CSV_SCHEMA.withSkipFirstDataRow(skipFirst));
    MappingIterator<CsvEventDataValue> iterator = reader.readValues(inputStream);
    Event event = new Event();
    event.setEvent("not_valid");
    while (iterator.hasNext()) {
        CsvEventDataValue dataValue = iterator.next();
        if (!event.getEvent().equals(dataValue.getEvent())) {
            event = new Event();
            event.setEvent(dataValue.getEvent());
            event.setStatus(StringUtils.isEmpty(dataValue.getStatus()) ? EventStatus.ACTIVE : Enum.valueOf(EventStatus.class, dataValue.getStatus()));
            event.setProgram(dataValue.getProgram());
            event.setProgramStage(dataValue.getProgramStage());
            event.setEnrollment(dataValue.getEnrollment());
            event.setOrgUnit(dataValue.getOrgUnit());
            event.setEventDate(dataValue.getEventDate());
            event.setDueDate(dataValue.getDueDate());
            if (dataValue.getLongitude() != null && dataValue.getLatitude() != null) {
                event.setCoordinate(new Coordinate(dataValue.getLongitude(), dataValue.getLatitude()));
            }
            events.getEvents().add(event);
        }
        DataValue value = new DataValue(dataValue.getDataElement(), dataValue.getValue());
        value.setStoredBy(dataValue.getStoredBy());
        value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
        event.getDataValues().add(value);
    }
    return events;
}
Also used : Events(org.hisp.dhis.dxf2.events.event.Events) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event) ObjectReader(com.fasterxml.jackson.databind.ObjectReader)

Example 13 with ObjectReader

use of com.fasterxml.jackson.databind.ObjectReader in project jvm-serializers by eishay.

the class JacksonAvroDatabind method register.

public static void register(TestGroups groups) {
    ObjectMapper mapper = new ObjectMapper(new AvroFactory());
    mapper.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
    JavaType type = mapper.constructType(MediaContent.class);
    AvroSchema schema = new AvroSchema(Avro.Media.sMediaContent);
    ObjectReader reader = mapper.readerFor(type).with(schema);
    ObjectWriter writer = mapper.writerFor(type).with(schema);
    groups.media.add(JavaBuiltIn.mediaTransformer, new StdJacksonDataBind<MediaContent>("avro/jackson/databind", type, mapper, reader, writer), new SerFeatures(SerFormat.JSON, SerGraph.FLAT_TREE, SerClass.ZERO_KNOWLEDGE, ""));
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) AvroFactory(com.fasterxml.jackson.dataformat.avro.AvroFactory) AvroSchema(com.fasterxml.jackson.dataformat.avro.AvroSchema) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) MediaContent(data.media.MediaContent) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with ObjectReader

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

the class JsonUtilClient method toXAttrNames.

static List<String> toXAttrNames(final Map<?, ?> json) throws IOException {
    if (json == null) {
        return null;
    }
    final String namesInJson = (String) json.get("XAttrNames");
    ObjectReader reader = new ObjectMapper().readerFor(List.class);
    final List<Object> xattrs = reader.readValue(namesInJson);
    final List<String> names = Lists.newArrayListWithCapacity(json.keySet().size());
    for (Object xattr : xattrs) {
        names.add((String) xattr);
    }
    return names;
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with ObjectReader

use of com.fasterxml.jackson.databind.ObjectReader in project camel by apache.

the class SObjectTreeResponseTest method shouldDeserializeJsonFromSalesforceExample.

@Test
public void shouldDeserializeJsonFromSalesforceExample() throws Exception {
    final String json = //
    "{\n" + //
    "    \"hasErrors\" : false,\n" + //
    "    \"results\" : [{\n" + //
    "     \"referenceId\" : \"ref1\",\n" + //
    "     \"id\" : \"001D000000K0fXOIAZ\"\n" + //
    "     },{\n" + //
    "     \"referenceId\" : \"ref4\",\n" + //
    "     \"id\" : \"001D000000K0fXPIAZ\"\n" + //
    "     },{\n" + //
    "     \"referenceId\" : \"ref2\",\n" + //
    "     \"id\" : \"003D000000QV9n2IAD\"\n" + //
    "     },{\n" + //
    "     \"referenceId\" : \"ref3\",\n" + //
    "     \"id\" : \"003D000000QV9n3IAD\"\n" + //
    "     }]\n" + "}";
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectReader reader = mapper.readerFor(SObjectTreeResponse.class);
    final SObjectTreeResponse response = reader.readValue(json);
    assertNotNull("Response should be parsed", response);
    assertFalse("`hasErrors` flag should be false", response.hasErrors());
    assertEquals("Should read 4 references", 4, response.getResults().size());
    assertThat("4 references should be read as expected", response.getResults(), hasItems(//
    new ReferenceId("ref1", "001D000000K0fXOIAZ", Collections.emptyList()), //
    new ReferenceId("ref4", "001D000000K0fXPIAZ", Collections.emptyList()), //
    new ReferenceId("ref2", "003D000000QV9n2IAD", Collections.emptyList()), new ReferenceId("ref3", "003D000000QV9n3IAD", Collections.emptyList())));
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 Test (org.junit.Test)12 IOException (java.io.IOException)6 JavaType (com.fasterxml.jackson.databind.JavaType)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)4 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 Method (java.lang.reflect.Method)2 NoSuchElementException (java.util.NoSuchElementException)2 AclEntry (org.apache.hadoop.fs.permission.AclEntry)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 AvroFactory (com.fasterxml.jackson.dataformat.avro.AvroFactory)1 AvroSchema (com.fasterxml.jackson.dataformat.avro.AvroSchema)1