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");
}
}
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;
}
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();
}
}
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();
}
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;
}
Aggregations