Search in sources :

Example 1 with JsonStreamParser

use of com.google.gson.JsonStreamParser in project buck by facebook.

the class XctoolOutputParsing method streamOutputFromReader.

/**
   * Decodes a stream of JSON objects as produced by {@code xctool -reporter json-stream}
   * and invokes the callbacks in {@code eventCallback} with each event in the stream.
   */
public static void streamOutputFromReader(Reader reader, XctoolEventCallback eventCallback) {
    Gson gson = new Gson();
    JsonStreamParser streamParser = new JsonStreamParser(reader);
    try {
        while (streamParser.hasNext()) {
            dispatchEventCallback(gson, streamParser.next(), eventCallback);
        }
    } catch (JsonParseException e) {
        LOG.warn(e, "Couldn't parse xctool JSON stream");
    }
}
Also used : Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonStreamParser(com.google.gson.JsonStreamParser)

Example 2 with JsonStreamParser

use of com.google.gson.JsonStreamParser in project commons by twitter.

the class TTextProtocol method readStructBegin.

@Override
public TStruct readStructBegin() throws TException {
    getCurrentContext().read();
    JsonElement structElem;
    // is the BaseContext
    if (1 == contextStack.size()) {
        structElem = parser.next();
        if (null == structElem) {
            throw new TException("parser.next() has nothing to parse!");
        }
    } else {
        structElem = getCurrentContext().getCurrentChild();
    }
    if (getCurrentContext().isMapKey()) {
        structElem = new JsonStreamParser(structElem.getAsString()).next();
    }
    if (!structElem.isJsonObject()) {
        throw new TException("Expected Json Object!");
    }
    pushContext(new StructContext(structElem.getAsJsonObject()));
    return ANONYMOUS_STRUCT;
}
Also used : TException(org.apache.thrift.TException) JsonElement(com.google.gson.JsonElement) JsonStreamParser(com.google.gson.JsonStreamParser)

Example 3 with JsonStreamParser

use of com.google.gson.JsonStreamParser in project mycore by MyCoRe-Org.

the class MCRClassificationEditorResource method save.

@POST
@Path("save")
@MCRRestrictedAccess(MCRClassificationWritePermission.class)
@Consumes(MediaType.APPLICATION_JSON)
public Response save(String json) {
    JsonStreamParser jsonStreamParser = new JsonStreamParser(json);
    if (jsonStreamParser.hasNext()) {
        JsonArray saveObjArray = jsonStreamParser.next().getAsJsonArray();
        List<JsonObject> saveList = new ArrayList<>();
        for (JsonElement jsonElement : saveObjArray) {
            saveList.add(jsonElement.getAsJsonObject());
        }
        saveList.sort(new IndexComperator());
        for (JsonObject jsonObject : saveList) {
            String status = getStatus(jsonObject);
            SaveElement categ = getCateg(jsonObject);
            MCRJSONCategory parsedCateg = parseJson(categ.getJson());
            if ("update".equals(status)) {
                new UpdateOp(parsedCateg, jsonObject).run();
            } else if ("delete".equals(status)) {
                deleteCateg(categ.getJson());
            } else {
                return Response.status(Status.BAD_REQUEST).build();
            }
        }
        // Status.CONFLICT
        return Response.status(Status.OK).build();
    } else {
        return Response.status(Status.BAD_REQUEST).build();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) MCRJSONCategory(org.mycore.frontend.classeditor.json.MCRJSONCategory) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonStreamParser(com.google.gson.JsonStreamParser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) MCRRestrictedAccess(org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)

Example 4 with JsonStreamParser

use of com.google.gson.JsonStreamParser in project mycore by MyCoRe-Org.

the class MCRWCMSNavigationResource method save.

@POST
@Path("save")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response save(String json) throws Exception {
    JsonStreamParser jsonStreamParser = new JsonStreamParser(json);
    if (!jsonStreamParser.hasNext()) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    JsonObject saveObject = jsonStreamParser.next().getAsJsonObject();
    // get navigation
    MCRNavigation newNavigation = MCRWCMSNavigationManager.fromJSON(saveObject);
    // save navigation
    MCRWCMSNavigationManager.save(newNavigation);
    // save content
    JsonArray items = saveObject.get(MCRWCMSNavigationProvider.JSON_ITEMS).getAsJsonArray();
    getContentManager().save(items);
    return Response.ok().build();
}
Also used : MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) JsonStreamParser(com.google.gson.JsonStreamParser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 5 with JsonStreamParser

use of com.google.gson.JsonStreamParser in project mycore by MyCoRe-Org.

the class MCRCategUtils method getCategoryIDMap.

public static HashMap<MCRCategoryID, String> getCategoryIDMap(String json) {
    HashMap<MCRCategoryID, String> categories = new HashMap<>();
    JsonStreamParser jsonStreamParser = new JsonStreamParser(json);
    if (jsonStreamParser.hasNext()) {
        JsonArray saveObjArray = jsonStreamParser.next().getAsJsonArray();
        for (JsonElement jsonElement : saveObjArray) {
            // jsonObject.item.id.rootid
            JsonObject root = jsonElement.getAsJsonObject();
            String rootId = root.getAsJsonObject("item").getAsJsonObject("id").getAsJsonPrimitive("rootid").getAsString();
            String state = root.getAsJsonPrimitive("state").getAsString();
            JsonElement parentIdJSON = root.get("parentId");
            if (parentIdJSON != null && parentIdJSON.isJsonPrimitive() && "_placeboid_".equals(parentIdJSON.getAsString())) {
                state = "new";
            }
            categories.put(MCRCategoryID.rootID(rootId), state);
        }
    } else {
        return null;
    }
    return categories;
}
Also used : JsonArray(com.google.gson.JsonArray) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonStreamParser(com.google.gson.JsonStreamParser)

Aggregations

JsonStreamParser (com.google.gson.JsonStreamParser)6 JsonElement (com.google.gson.JsonElement)4 JsonArray (com.google.gson.JsonArray)3 JsonObject (com.google.gson.JsonObject)3 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 TException (org.apache.thrift.TException)2 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Produces (javax.ws.rs.Produces)1 TMap (org.apache.thrift.protocol.TMap)1 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)1 MCRJSONCategory (org.mycore.frontend.classeditor.json.MCRJSONCategory)1 MCRRestrictedAccess (org.mycore.frontend.jersey.filter.access.MCRRestrictedAccess)1 MCRNavigation (org.mycore.wcms2.datamodel.MCRNavigation)1