Java Code Examples for com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter#serializeAll()

The following examples show how to use com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter#serializeAll() . 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: Trace.java    From heimdall with Apache License 2.0 5 votes vote down vote up
private ObjectMapper mapper() {
    SimpleBeanPropertyFilter customFilter;

    if (!this.printFilters) {
        customFilter = SimpleBeanPropertyFilter.serializeAllExcept("filters");
    } else {
        customFilter = SimpleBeanPropertyFilter.serializeAll();
    }

    FilterProvider filters = new SimpleFilterProvider().addFilter("customFilter", customFilter);

    return new ObjectMapper().setFilterProvider(filters);

}
 
Example 2
Source File: MetricsHttpSink.java    From beam with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: MapperTest.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Test
   public void AlarmStateMessageTest() {
       String expectedJsonString = "{\"severity\":\"OK\",\"message\":\"OK\",\"value\":\"-2.5614483916185438\",\"time\":{\"seconds\":\"1531143702\",\"nanos\":\"487182900\"},\"current_severity\":\"OK\",\"current_message\":\"NONE\",\"notify\":false,\"latch\":false}";

       AlarmStateMessage message = new AlarmStateMessage();
       message.setValue("-2.5614483916185438");
       message.setSeverity("OK");
       message.setMessage("OK");
       message.setCurrent_severity("OK");
       message.setCurrent_message("NONE");
       HashMap<String, String> timeMap = new HashMap<>();
       timeMap.put("seconds", "1531143702");
       timeMap.put("nanos", "487182900");
       message.setTime(timeMap);
       message.setMode(null);
message.setNotify(false);

       ObjectMapper objectMapper = new ObjectMapper();

       SimpleBeanPropertyFilter emptyFilter = SimpleBeanPropertyFilter.serializeAll();
       FilterProvider filters = new SimpleFilterProvider().addFilter("timeFilter", emptyFilter);

       try {
           // Parsing object to json string
           assertEquals("Failed to map the AlarmStateMessage", expectedJsonString,
                   objectMapper.writer(filters).writeValueAsString(message));
           // Serializing object to byte[]
           assertArrayEquals("Failed to parse AlarmStateMessage to byte[] ", expectedJsonString.getBytes(),
                   objectMapper.writer(filters).writeValueAsBytes(message));

           // Check the pasrsing json string to object
           // bjectMapper.
           AlarmStateMessage state = objectMapper.readValue(expectedJsonString, AlarmStateMessage.class);
           assertEquals("Failed to map the AlarmStateMessage", message,
                   objectMapper.readValue(expectedJsonString, AlarmStateMessage.class));
           // Check sdeserializing byte[] to object
           assertEquals("Failed to parse AlarmStateMessage to byte[] ", message,
                   objectMapper.readValue(expectedJsonString.getBytes(), AlarmStateMessage.class));

       } catch (IOException e) {
           e.printStackTrace();
           fail(e.getMessage());
       }

   }