Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

halarious is a simple java- framework for serializing and deserializing JSON according to the Java-Classes to JSON following the HAL-specification. It's based on GSON for maximum capatibility and flexibility. 

  • HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API.

  • halarious is based on GSON, a lightweight library by Google.
  • References and embedded fields can simply be marked with annotations. No further configuration nedded.

 

Code Block
languagejava
titleJava
public class ProductResource implements HalResource {
    @HalLink
    private String self;
    private String name;
    private int weight;
    private String description;
    @HalEmbedded
    private ManufacturerResource manufacturer;

    /** Getter/Setter... */
}
 
public class ManufacturerResource implements HalResource {
    @HalLink
    private String self;
    @HalLink
    private String homepage;
    private String name;

    /** Getter/Setter... */
}
Code Block
languagejs
titleJSON
{
   "name":"A product",
   "weight":400,
   "description":"A great product",
   "_links":{
      "self":{
         "href":"/product/987"
      }
   },
   "_embedded":{
      "manufacturer":{
         "name":"http://hoverdonkey.com",
         "_links":{
            "self":{
               "href":"/manufacturers/328764"
            },
            "homepage":{
               "href":"Manufacturer Inc."
            }
         }
      }
   }
}
Code Block
languagejava
titleConverter
public class ManufacturerConverter {
    private Gson gson;
    public ManufacturerConverter() {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(HalResource.class, new HalSerializer());
        builder.registerTypeAdapter(HalResource.class, new HalDeserializer(ProductResource.class));
        builder.setExclusionStrategies(new HalExclusionStrategy());
        this.gson = builder.create();
    }
    public String toJson(ProductResource product) {
        return gson.toJson(product, HalResource.class);
    }
    public ProductResource fromJson(String json) {
        return (ProductResource) gson.fromJson(json, HalResource.class);
    }
}