Skip to content

Instantly share code, notes, and snippets.

@vasouv
Created February 10, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasouv/fe39a0e9d7b7e1d75de2ba57ce0202cc to your computer and use it in GitHub Desktop.
Save vasouv/fe39a0e9d7b7e1d75de2ba57ce0202cc to your computer and use it in GitHub Desktop.
JSON (de)serialization using the Processing API - no other libs - Adam Bien
public class Workshop {
private String name;
private int duration;
public Workshop(String name, int duration) {
this.name = name;
this.duration = duration;
}
public Workshop(JsonObject input) {
this.name = input.getString("name");
this.duration = input.
getJsonNumber("duration").
intValue();
}
public JsonObject toJson() {
return Json.createObjectBuilder().
add("name", this.name).
add("duration", this.duration).
build();
}
}
@Stateless
@Path("workshops")
public class WorkshopsResource {
@Inject
RegistrationStore store;
@GET
public JsonArray all() {
JsonArrayBuilder list = Json.createArrayBuilder();
List<Workshop> all = this.store.all();
all.stream().map(Workshop::toJson).forEach(list::add);
return list.build();
}
@POST
public void save(JsonObject input) {
this.store.save(new Workshop(input));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment