Grails
  1. Grails
  2. GRAILS-9888 REST and URL mapping changes
  3. GRAILS-5791

Create a way to easily exclude properties from Serialization when using the XML or JSON converters

    Details

    • Type: Sub-task Sub-task
    • Status: Closed
    • Priority: Trivial Trivial
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.3-RC1
    • Component/s: Converters
    • Labels:
      None
    • Environment:
      ALL

      Description

      Using the default converters there is currently not a quick way to exclude properties from serialization.

      It would be great to have a quick way to pass a list of properties to be excluded from serialization.

      While this can currently be done via the builders or with the following approach:
      http://stackoverflow.com/questions/1700668/grails-jsonp-callback-without-id-and-class-in-json-file/1701258#1701258

      But the approach in the SO post is done at a global level.
      It would be great to offer an alternative way to invoke serialization and pass a list of excluded properties
      at the time serialization is invoked.

      something logically equivalent to:

      def excludedProps=['id','lastUpdatedId']
      def json= GrailsUtils.runConverter(JSON,myDomainObj,excludedProps)

      Hopefully the implementation could be a little more elegant than my example.
      Adding some easy flexibility wold be a great improvement to the current implementation.

        Activity

        Hide
        Peter Ledbrook added a comment -

        Sorry, I wasn't clear. I've updated to comment to clarify that it's a suggested new feature for the JSON class, not something that's already supported.

        Show
        Peter Ledbrook added a comment - Sorry, I wasn't clear. I've updated to comment to clarify that it's a suggested new feature for the JSON class, not something that's already supported.
        Hide
        Jason Stonebraker added a comment -

        @pledbrook's proposed usage case would be an excellent addition to the JSON Converter. It would satisfy Daniel's original request, "...[to] pass a list of excluded properties at the time serialization is invoked."

        I'm also a fan of this proposal because when I needed to exclude properties from serialization (different exclusions for different cases - not globally or at the class level), I went looking through the grails.converters.JSON doc for something like:

        public void exclude(List<String> properties)
        public void include(List<String> properties)

        Just my two cents... The amazing work you guys do is greatly appreciated!

        Show
        Jason Stonebraker added a comment - @pledbrook's proposed usage case would be an excellent addition to the JSON Converter. It would satisfy Daniel's original request, "... [to] pass a list of excluded properties at the time serialization is invoked." I'm also a fan of this proposal because when I needed to exclude properties from serialization (different exclusions for different cases - not globally or at the class level), I went looking through the grails.converters.JSON doc for something like: public void exclude(List<String> properties) public void include(List<String> properties) Just my two cents... The amazing work you guys do is greatly appreciated!
        Hide
        Graeme Rocher added a comment -

        You can customize includes / excludes declaratively now. see http://grails.org/doc/2.3.x/guide/webServices.html#defaultRenderers

        but will leave this open for runtime customization.

        Show
        Graeme Rocher added a comment - You can customize includes / excludes declaratively now. see http://grails.org/doc/2.3.x/guide/webServices.html#defaultRenderers but will leave this open for runtime customization.
        Hide
        Jason Stonebraker added a comment -

        That works. Thanks... I really don't know how ya do it, Graeme.

        Show
        Jason Stonebraker added a comment - That works. Thanks... I really don't know how ya do it, Graeme.
        Hide
        Graeme Rocher added a comment -

        It is now possible to include/exclude properties and set the content type in the way demonstrated by Peter's comment

        Show
        Graeme Rocher added a comment - It is now possible to include/exclude properties and set the content type in the way demonstrated by Peter's comment

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Daniel Honig
          • Votes:
            24 Vote for this issue
            Watchers:
            22 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Time Tracking

              Estimated:
              Original Estimate - 3 days
              3d
              Remaining:
              Remaining Estimate - 3 days
              3d
              Logged:
              Time Spent - Not Specified
              Not Specified

                Development