Created
February 10, 2017 12:31
-
-
Save vasouv/fe39a0e9d7b7e1d75de2ba57ce0202cc to your computer and use it in GitHub Desktop.
JSON (de)serialization using the Processing API - no other libs - Adam Bien
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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