Search in sources :

Example 66 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project beam by apache.

the class MetricsHttpSink method serializeMetrics.

private String serializeMetrics(MetricQueryResults metricQueryResults) throws Exception {
    SimpleModule module = new JodaModule();
    module.addSerializer(new MetricNameSerializer(MetricName.class));
    module.addSerializer(new MetricKeySerializer(MetricKey.class));
    module.addSerializer(new MetricResultSerializer(MetricResult.class));
    objectMapper.registerModule(module);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    // need to register a filter as soon as @JsonFilter annotation is specified.
    // So specify an pass through filter
    SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAll();
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider.addFilter("committedMetrics", filter);
    objectMapper.setFilterProvider(filterProvider);
    String result;
    try {
        result = objectMapper.writeValueAsString(metricQueryResults);
    } catch (JsonMappingException exception) {
        if ((exception.getCause() instanceof UnsupportedOperationException) && exception.getCause().getMessage().contains("committed metrics")) {
            filterProvider.removeFilter("committedMetrics");
            filter = SimpleBeanPropertyFilter.serializeAllExcept("committed");
            filterProvider.addFilter("committedMetrics", filter);
            result = objectMapper.writeValueAsString(metricQueryResults);
        } else {
            throw exception;
        }
    }
    return result;
}
Also used : JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) MetricResult(org.apache.beam.sdk.metrics.MetricResult) MetricName(org.apache.beam.sdk.metrics.MetricName) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MetricKey(org.apache.beam.sdk.metrics.MetricKey) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 67 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project druid by druid-io.

the class QueryGranularityTest method testSerializePeriod.

@Test
public void testSerializePeriod() throws Exception {
    ObjectMapper mapper = new DefaultObjectMapper();
    String json = "{ \"type\": \"period\", \"period\": \"P1D\" }";
    Granularity gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), null, null), gran);
    json = "{ \"type\": \"period\", \"period\": \"P1D\"," + "\"timeZone\": \"America/Los_Angeles\", \"origin\": \"1970-01-01T00:00:00Z\"}";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), new DateTime(0L), DateTimeZone.forID("America/Los_Angeles")), gran);
    PeriodGranularity expected = new PeriodGranularity(new Period("P1D"), new DateTime("2012-01-01"), DateTimeZone.forID("America/Los_Angeles"));
    String jsonOut = mapper.writeValueAsString(expected);
    Assert.assertEquals(expected, mapper.readValue(jsonOut, Granularity.class));
    String illegalJson = "{ \"type\": \"period\", \"period\": \"P0D\" }";
    try {
        mapper.readValue(illegalJson, Granularity.class);
        Assert.fail();
    } catch (JsonMappingException e) {
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) PeriodGranularity(io.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Granularity(io.druid.java.util.common.granularity.Granularity) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) PeriodGranularity(io.druid.java.util.common.granularity.PeriodGranularity) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 68 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project druid by druid-io.

the class CoordinatorDynamicConfigTest method testSerdeWithKillAllDataSources.

@Test
public void testSerdeWithKillAllDataSources() throws Exception {
    String jsonStr = "{\n" + "  \"millisToWaitBeforeDeleting\": 1,\n" + "  \"mergeBytesLimit\": 1,\n" + "  \"mergeSegmentsLimit\" : 1,\n" + "  \"maxSegmentsToMove\": 1,\n" + "  \"replicantLifetime\": 1,\n" + "  \"replicationThrottleLimit\": 1,\n" + "  \"balancerComputeThreads\": 2, \n" + "  \"emitBalancingStats\": true,\n" + "  \"killAllDataSources\": true\n" + "}\n";
    ObjectMapper mapper = TestHelper.getObjectMapper();
    CoordinatorDynamicConfig actual = mapper.readValue(mapper.writeValueAsString(mapper.readValue(jsonStr, CoordinatorDynamicConfig.class)), CoordinatorDynamicConfig.class);
    Assert.assertEquals(new CoordinatorDynamicConfig(1, 1, 1, 1, 1, 1, 2, true, ImmutableSet.of(), true), actual);
    //ensure whitelist is empty when killAllDataSources is true
    try {
        jsonStr = "{\n" + "  \"killDataSourceWhitelist\": [\"test1\",\"test2\"],\n" + "  \"killAllDataSources\": true\n" + "}\n";
        mapper.readValue(jsonStr, CoordinatorDynamicConfig.class);
        Assert.fail("deserialization should fail.");
    } catch (JsonMappingException e) {
        Assert.assertTrue(e.getCause() instanceof IAE);
    }
}
Also used : CoordinatorDynamicConfig(io.druid.server.coordinator.CoordinatorDynamicConfig) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IAE(io.druid.java.util.common.IAE) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 69 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project ambrose by twitter.

the class HRavenStatsReadService method getEventsSinceId.

@SuppressWarnings("rawtypes")
@Override
public List<Event> getEventsSinceId(String workflowId, int eventId, int maxEvents) throws IOException {
    Preconditions.checkArgument(maxEvents > 0);
    WorkflowId id = WorkflowId.parseString(workflowId);
    FlowEventKey flowEventKey = new FlowEventKey(toFlowKey(id), eventId);
    List<FlowEvent> flowEventList = flowEventService.getFlowEventsSince(flowEventKey);
    // TODO push this limit into the FlowEventService
    int numElems = 0;
    List<Event> workflowEvents = Lists.newArrayListWithCapacity(maxEvents);
    for (FlowEvent flowEvent : flowEventList) {
        if (numElems >= maxEvents) {
            break;
        }
        String eventDataJson = flowEvent.getEventDataJSON();
        try {
            Event event = Event.fromJson(eventDataJson);
            numElems++;
            workflowEvents.add(event);
        } catch (JsonMappingException e) {
            LOG.error("Could not deserialize json: " + eventDataJson, e);
        }
    }
    return workflowEvents;
}
Also used : FlowEvent(com.twitter.hraven.FlowEvent) FlowEventKey(com.twitter.hraven.FlowEventKey) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Event(com.twitter.ambrose.model.Event) FlowEvent(com.twitter.hraven.FlowEvent) WorkflowId(com.twitter.ambrose.model.WorkflowId)

Example 70 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project torodb by torodb.

the class AbstractBackendSerializer method depositSchemaProperty.

private void depositSchemaProperty(JsonObjectFormatVisitor v, String name, JavaType type) throws JsonMappingException {
    BeanProperty prop = new BeanProperty.Std(PropertyName.construct(name), type, null, null, null, PropertyMetadata.STD_OPTIONAL) {

        @Override
        public void depositSchemaProperty(JsonObjectFormatVisitor v) {
            try {
                if (v != null) {
                    if (isRequired()) {
                        v.property(this);
                    } else {
                        v.optionalProperty(this);
                    }
                }
            } catch (JsonMappingException jsonMappingException) {
                throw new RuntimeException(jsonMappingException);
            }
        }
    };
    prop.depositSchemaProperty(v);
}
Also used : JsonObjectFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)140 IOException (java.io.IOException)68 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 JsonParseException (com.fasterxml.jackson.core.JsonParseException)52 Test (org.junit.Test)31 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)19 HashMap (java.util.HashMap)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 File (java.io.File)9 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 List (java.util.List)7 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Response (javax.ws.rs.core.Response)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5