Search in sources :

Example 1 with PublicationType

use of gov.usgs.cida.coastalhazards.model.summary.Publication.PublicationType in project coastal-hazards by USGS-CIDA.

the class FullSummaryAdapter method deserialize.

@Override
public Full deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Full result = new Full();
    if (json instanceof JsonObject) {
        JsonObject fullJson = (JsonObject) json;
        result.setTitle(fullJson.getAsJsonPrimitive("title").getAsString());
        result.setText(fullJson.getAsJsonPrimitive("text").getAsString());
        List<Publication> fullPubList = new LinkedList<>();
        JsonObject publications = fullJson.getAsJsonObject("publications");
        for (PublicationType type : PublicationType.values()) {
            JsonArray typedArray = publications.getAsJsonArray(type.name());
            if (typedArray != null) {
                List<Map<String, Object>> typeList = context.deserialize(typedArray, ArrayList.class);
                for (Map<String, Object> pubMap : typeList) {
                    if (!pubMap.containsKey(Publication.TITLE) || !pubMap.containsKey(Publication.LINK)) {
                        throw new IllegalStateException("Expected publication, was not a publication");
                    }
                    Publication pub = new Publication();
                    pub.setTitle((String) pubMap.get(Publication.TITLE));
                    pub.setLink((String) pubMap.get(Publication.LINK));
                    pub.setType(type);
                    fullPubList.add(pub);
                }
            }
        }
        result.setPublications(fullPubList);
    }
    return result;
}
Also used : PublicationType(gov.usgs.cida.coastalhazards.model.summary.Publication.PublicationType) JsonObject(com.google.gson.JsonObject) Publication(gov.usgs.cida.coastalhazards.model.summary.Publication) Full(gov.usgs.cida.coastalhazards.model.summary.Full) LinkedList(java.util.LinkedList) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 2 with PublicationType

use of gov.usgs.cida.coastalhazards.model.summary.Publication.PublicationType in project coastal-hazards by USGS-CIDA.

the class FullSummaryAdapter method serialize.

@Override
public JsonElement serialize(Full src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject fullSummary = new JsonObject();
    fullSummary.add("title", context.serialize(src.getTitle()));
    fullSummary.add("text", context.serialize(src.getText()));
    JsonObject publications = new JsonObject();
    for (PublicationType type : PublicationType.values()) {
        List<Publication> typedPubs = Publication.getTypedPublications(src.getPublications(), type);
        publications.add(type.name(), context.serialize(typedPubs));
    }
    fullSummary.add("publications", publications);
    return fullSummary;
}
Also used : PublicationType(gov.usgs.cida.coastalhazards.model.summary.Publication.PublicationType) JsonObject(com.google.gson.JsonObject) Publication(gov.usgs.cida.coastalhazards.model.summary.Publication)

Aggregations

JsonObject (com.google.gson.JsonObject)2 Publication (gov.usgs.cida.coastalhazards.model.summary.Publication)2 PublicationType (gov.usgs.cida.coastalhazards.model.summary.Publication.PublicationType)2 JsonArray (com.google.gson.JsonArray)1 Full (gov.usgs.cida.coastalhazards.model.summary.Full)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1