0

I'm having problems using Halarious (Java library for the HAL specification) and Gson to serialise a list of links in the _links section with just a single element. The array is serialized into an object instead of being an array with a single link.

EXAMPLE: What I'm getting now is:

{
  "year": 2008,
  "_embedded": {
    "items": {
      "_links": {
        "self": {
          "href": "/first_item"
        }
      }
    }
  }
}

Instead of:

{
  "year": 2008,
  "_embedded": {
    "items": {
      "_links": {
        "self": [
          {
            "href": "/first_item"
          }
        ]
      }
    }
  }
}

I solved the same problem with the _embedded section but I can't solve it for the links section.

Thanks

Gabe
  • 5,997
  • 5
  • 46
  • 92
  • I'll probably adapt my consumer in this way: http://stackoverflow.com/questions/7668507/gson-handle-object-or-array?rq=1 – Gabe Jan 27 '16 at 14:44

1 Answers1

0

I solved using a workaround. I dont' use @HalLink but a surrogate ad hoc class that contains all the hierarchy and which instance is named "_links".

So using a list of custom Href objects when it has a single link I'll receive back the expected self attribute as list with a single element.

After all the HAL documentation (http://stateless.co/hal_specification.html) says: ​"If you're unsure whether the link should be singular, assume it will be multiple" and from http://blog.stateless.co/post/13296666138/json-linking-with-hal "Where a relation may potentially have multiple links sharing the same key the value should be an array of link objects". ​In this way I won't break consumers having them to deal with either a JSON array or an object.

Gabe
  • 5,997
  • 5
  • 46
  • 92