How to ignore null values in domain class

596 views
Skip to first unread message

Ahmed

unread,
Jul 23, 2013, 5:32:32 PM7/23/13
to halbuil...@googlegroups.com
Hi everyone,

We are using Json-HAL to convert our domain objects for our REST API.  Is there a way to ignore null values in a given class or list of classes as shown below?  For example, I am using the below code to 

        public Representation representDomainObject(Representation representation, SiteInfo siteInfo) {
            representation.withLink("self", "rest/site/" + siteInfo.getSiteId());

            if (siteInfo.getAddress() != null) {
                representation.withProperty("address", siteInfo.getAddress());
            }

            if (siteInfo.getUsers() != null) {
                representation.withProperty("users", siteInfo.getUsers());
            }

            return representation;
        }

Below is the json response:
{ "_links" : {
    "self" : {
      "href" : "rest/site/186"
    }
  },
  "address" : {
    "street1" : "15122",
    "city" : "Rockville",
    "state" : "MD",
    "country" : "US",
    "verified" : false
  },
  "isActive" : true,
  "isSuspended" : false,
  "timeZone" : "US/Pacific:-7",
  "users" : [ {
    "name" : null,
    "userId" : "186",
    "emailAddress" : "te...@test.net",
    "links" : null,
    "siteIds" : null,
    "primary" : true,
    "status" : "active"
  } ]
}

Is there a way the json HAL generated for null values to be ignored? I have highlighted the areas I would like ignored.  Not all fields would be utilized for the domain object for every request. (Equivalent to @JsonInclude(Include.NON_NULL) in jackson)I am using this annotation now in the class, however it is not working still.

Mark Derricutt

unread,
Jul 23, 2013, 6:14:02 PM7/23/13
to halbuil...@googlegroups.com
On 24/07/2013, at 9:32 AM, Ahmed <aba...@gmail.com> wrote:

We are using Json-HAL to convert our domain objects for our REST API.  Is there a way to ignore null values in a given class or list of classes as shown below?  For example, I am using the below code to 

Hi Ahmed,

Unfortunately not in the current version of halbuilder-json, at least - not fully…

com.theoryinpractise.halbuilder.json.JsonRepresentationWriter#getJsonFactory was made a protected method when it was extracted from the core library, and this means you can override it in a subclass and change it to:

    protected JsonFactory getJsonFactory() {
        JsonFactory f = new JsonFactory();
        ObjectMapper om = new ObjectMapper();
        om.setSerializationInclusion(Include.NON_NULL);
        f.setCodec(om);
        f.enable(JsonGenerator.Feature.QUOTE_FIELD_NAMES);
        return f;
    }

and configure your RepresentationFactory to use your subclass.

  rf.withRenderer(HAL_JSON, YourSubClassHere.class);

This will configure Jackson to not include the nulls for any embedded objects, but since the top level properties themselves and handled via a JsonGenerator they won't be covered by this.

I hope to push out some minor releases of some of the sub modules this weekend so I'll take a look at see if I can come up with a good/clean way of filtering the nulls.

In the meantime, I hope this helps.

Mark

Mark Derricutt

unread,
Jul 29, 2013, 7:17:44 AM7/29/13
to halbuil...@googlegroups.com
I've just released:

  halbuilder-api-2.1.1
  halbuilder-json-3.1.1

this adds a new URI flag RepresentationFactory.STRIP_NULLS which, if included will configure the Jackson ObjectMapper used to use Include.NON_NULL and also strip any top level property with a null value.

I'll update and roll halbuilder-xml with a similar update in a few days.

Mark



On 24/07/2013, at 9:32 AM, Ahmed <aba...@gmail.com> wrote:

Evan Pipho

unread,
Jul 29, 2013, 5:49:44 PM7/29/13
to halbuil...@googlegroups.com
Any chance you could also expose an option to turn off WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED for the object mapper and for top level objects? I have several apps that get confused when an array of 1 element is flattened out. 

If this isn't a feature on the horizon could you point me in the general direction of where such a feature would be implemented so I can take a crack at it myself?

Thanks
-Evan

Mark Derricutt

unread,
Jul 29, 2013, 6:18:09 PM7/29/13
to halbuil...@googlegroups.com
At this stage I don't particular want to add options for every permutation of Jackson - as the fact that jackson is used is purely an internal thing and may even be replaced in the future.

Personally I don't particular think nested objects should EVER be used either as I believe those things should really be _embedded resources ( adding the ObjectMapper to allow that was a concession, but also breaks the XML variant ).

HAL-JSON has the annoying convention of flattening out a 1 element array of links/embeds to an object which I find really annoying, but that should be handled by your parser - using the raw JSON as an object IMHO isn't the ideal way of dealing with things - it's simple yes, but that doesn't make it correct ( my opinion only there really ).

If you want to try a crack at it - and submit a pull request with a compelling request - you can see the simple change I did for this release ( which I forgot to push last night):


Mark
Reply all
Reply to author
Forward
0 new messages