Developer notes written down before they get lost.
JSON Mapping in Piriti
Following a proposal by Jerome
Louvel, I added JSON mapping to Piriti. Piriti is a JSON / XML mapper for GWT based on annotations and deferred
binding.
To map JSON data you have to annotate your model classes with @JsonField annotations. In case you have the following
JSON structure:
1234567891011121314
{isbn:"978-0345417954",pages:432,title:"The Hotel New Hampshire",author:{firstname:"John",surname:"Irving",},reviews:["A hectic gaudy saga with the verve of a Marx Brothers movie.","Rejoice! John Irving has written another book according to your world.","Spellbinding, intensely human, a high-wire act of dazzling virtuosity."]}
You can use the following code to map the JSON data to your model:
123456789101112131415161718192021222324252627
publicclassBook{interfaceBookReaderextendsJsonReader<Book>{}publicstaticfinalBookReaderJSON=GWT.create(BookReader.class);@JsonFieldStringisbn;@JsonFieldintpages;@JsonFieldStringtitle;@JsonFieldAuthorauthor;@JsonFieldList<String>reviews;}...publicclassAuthor{interfaceAuthorReaderextendsJsonReader<Author>{}publicstaticfinalAuthorReaderJSON=GWT.create(AuthorReader.class);@JsonFieldStringfirstname;@JsonFieldStringsurname;}...StringjsonString=...;// The JSON structure (see above) Bookbook=Book.JSON.read(jsonString);