Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Cannot Reproduce
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Serializer
    • Labels:
      None
    • Number of attachments :
      0

      Description

      For serialization, a simpler property filtering configuration should be available to not require use of annotations.

      A good solution might be to allow registering type-based filters with a SimpleFilterProvider, such that this code

      public class JacksonFoo
      {
        public static void main(String[] args) throws Exception
        {
          ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
          mapper.getSerializationConfig().addMixInAnnotations(Object.class, PropertyFilterMixIn.class);
      
          String[] ignorableFieldNames = { "id", "color" };
          FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
          ObjectWriter writer = mapper.writer(filters);
      
          System.out.println(writer.writeValueAsString(new Bar()));
          // output:
          // {"name":"James","foo":{"size":"big","height":"tall"}}
        }
      }
      
      @JsonFilter("filter properties by name")
      class PropertyFilterMixIn
      {
        
      }
      
      class Bar
      {
        String id = "42";
        String name = "Fred";
        String color = "blue";
        Foo foo = new Foo();
      }
      
      class Foo
      {
        String id = "99";
        String size = "big";
        String height = "tall";
      }
      

      ...might then be reduced to just

      public class JacksonFoo
      {
        public static void main(String[] args) throws Exception
        {
          ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
      
          String[] ignorableFieldNames = { "id", "color" };
          FilterProvider filters = new SimpleFilterProvider().addFilter(Bar.class, SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
          ObjectWriter writer = mapper.writer(filters);
      
          System.out.println(writer.writeValueAsString(new Bar()));
          // output:
          // {"name":"James","foo":{"size":"big","height":"tall"}}
        }
      }
      
      class Bar
      {
        String id = "42";
        String name = "Fred";
        String color = "blue";
        Foo foo = new Foo();
      }
      
      class Foo
      {
        String id = "99";
        String size = "big";
        String height = "tall";
      }
      

        Activity

        Hide
        Programmer Bruce added a comment -

        ...or if

        addFilter(Bar.class ...

        is too dissimilar to the current configuration approach, then

        addFilter(Object.class ...

        would be more appropriate in the example above.

        Show
        Programmer Bruce added a comment - ...or if addFilter(Bar.class ... is too dissimilar to the current configuration approach, then addFilter( Object .class ... would be more appropriate in the example above.
        Hide
        Tatu Saloranta added a comment -

        Ok, I have to think about this a bit. Obviously if this could be implemented using existing facilities, it'd be great (make use of default filter etc perhaps?).
        And as usual, contributions welcome.

        Also, since we are heading for 2.0, which is a backwards incompatible update, we have more freedom in changing API of existing pieces as well if absolutely necessary.

        Show
        Tatu Saloranta added a comment - Ok, I have to think about this a bit. Obviously if this could be implemented using existing facilities, it'd be great (make use of default filter etc perhaps?). And as usual, contributions welcome. Also, since we are heading for 2.0, which is a backwards incompatible update, we have more freedom in changing API of existing pieces as well if absolutely necessary.
        Hide
        Tatu Saloranta added a comment -

        Will close as part of Codehaus Jackson Jira deprecation: new concrete RFEs can be filed at:

        https://github.com/FasterXML/jackson-databind/issues

        Show
        Tatu Saloranta added a comment - Will close as part of Codehaus Jackson Jira deprecation: new concrete RFEs can be filed at: https://github.com/FasterXML/jackson-databind/issues

          People

          • Assignee:
            Tatu Saloranta
            Reporter:
            Programmer Bruce
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: