Siren Java Serializer/Deserializer

451 views
Skip to first unread message

Kevin Swiber

unread,
Jan 25, 2013, 5:24:43 PM1/25/13
to siren-hy...@googlegroups.com
I remember hearing some talk of creating a Java library for parsing Siren. Has anyone made progress on this?

Thanks,

Kevin

Mark Derricutt

unread,
Jan 25, 2013, 5:39:58 PM1/25/13
to siren-hy...@googlegroups.com
This is something I was actually hoping to look into over this long weekend - as an extension module to my HalBuilder library.

The only thing I'll need to work out is how to handle actions.  Everything else is just a serialization/deserialization concern.

I'll post back with any progress I make.

Mark Derricutt

unread,
Jan 25, 2013, 10:28:57 PM1/25/13
to siren-hy...@googlegroups.com
Kevin,

I just pushed a very basic renderer extension to https://github.com/HalBuilder/halbuilder-siren

With this, one can create the RepresentationFactory and add Siren support:

        DefaultRepresentationFactory rf = new DefaultRepresentationFactory();
        rf.withFlag(RepresentationFactory.PRETTY_PRINT);
        rf.withRenderer("application/vnd.siren+json", SirenRepresentationWriter.class);

Then create a representation, with nested representations:

        Representation rep = rf.newRepresentation("/api/user/mark").withProperty("name", "Mark");
        rep.withLink("licence", "http://www.apache.org/licenses/LICENSE-2.0.html");
        Representation address = rf.newRepresentation()
                .withProperty("city", "Auckland")
                .withProperty("country", "New Zealand");
        rep.withRepresentation("address", address);

Then convert to a String in the desired media type:

        System.out.println(rep.toString("application/vnd.siren+json"));

which yields:

{
  "links" : [ {
    "rel" : [ "self" ],
    "href" : "/api/user/mark"
  }, {
    "rel" : [ "licence" ],
    "href" : "http://www.apache.org/licenses/LICENSE-2.0.html"
  } ],
  "properties" : {
    "name" : "Mark"
  },
  "entities" : [ {
    "rel" : [ "address" ],
    "properties" : {
      "city" : "Auckland",
      "country" : "New Zealand"
    }
  } ]
}

Currently it lacks support for multiple rel items ( as required by Hal ), and doesn't support the top level "class" attribute or actions yet.  I thinking they could just be modeled as a specific 'property' with a key of say 'urn:halbuilder:siren:class' or an embedded representation using 'urn:halbuilder:siren:action' ( I already use this URN scheme for my flags like pretty print ).

But already, this will let me generate both hal+basic siren without changing client code ( other than the bootstrap of RepresentationFactory ).

Writing the parser/deserializer may take a bit longer/more-thought at requires some basic API changes of HalBuilder, but shouldn't be that difficult.

Mark


26 January 2013 11:24 AM

Erik Serating

unread,
Apr 18, 2013, 9:52:36 PM4/18/13
to siren-hy...@googlegroups.com
I have been working what I think is going to be a pretty useful library for working with Siren with java.
Its pretty close to having its first release. I will be using it at my work for our REST services.

Erik
Reply all
Reply to author
Forward
0 new messages