Java Code Examples for com.google.gson.JsonStreamParser

The following examples show how to use com.google.gson.JsonStreamParser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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 = MCRWCMSNavigationUtils.fromJSON(saveObject);
    // save navigation
    MCRWCMSNavigationUtils.save(newNavigation);
    // save content
    JsonArray items = saveObject.get(MCRWCMSNavigationProvider.JSON_ITEMS).getAsJsonArray();
    getContentManager().save(items);
    return Response.ok().build();
}
 
Example 2
Source Project: mycore   Source File: MCRCategUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source Project: docker-maven-plugin   Source File: ImagesService.java    License: Apache License 2.0 6 votes vote down vote up
private static void parseStreamToDisplayImageDownloadStatus(final InputStream inputStream) {
    InputStreamReader isr = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(isr);

    JsonStreamParser parser = new JsonStreamParser(reader);

    while (parser.hasNext()) {
        JsonElement element = parser.next();
        JsonObject object = element.getAsJsonObject();
        if (object.has("status")) {
            System.out.print(".");
        }
        if (object.has("error")) {
            System.err.println("ERROR: " + object.get("error").getAsString());
        }
    }
    System.out.println("");
}
 
Example 4
@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();
    }
}
 
Example 5
Source Project: gson   Source File: ReadersWritersTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testReadWriteTwoStrings() throws IOException {
  Gson gson= new Gson();
  CharArrayWriter writer= new CharArrayWriter();
  writer.write(gson.toJson("one").toCharArray());
  writer.write(gson.toJson("two").toCharArray());
  CharArrayReader reader = new CharArrayReader(writer.toCharArray());
  JsonStreamParser parser = new JsonStreamParser(reader);
  String actualOne = gson.fromJson(parser.next(), String.class);
  assertEquals("one", actualOne);
  String actualTwo = gson.fromJson(parser.next(), String.class);
  assertEquals("two", actualTwo);
}
 
Example 6
Source Project: gson   Source File: ReadersWritersTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testReadWriteTwoObjects() throws IOException {
  Gson gson= new Gson();
  CharArrayWriter writer= new CharArrayWriter();
  BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
  writer.write(gson.toJson(expectedOne).toCharArray());
  BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
  writer.write(gson.toJson(expectedTwo).toCharArray());
  CharArrayReader reader = new CharArrayReader(writer.toCharArray());
  JsonStreamParser parser = new JsonStreamParser(reader);
  BagOfPrimitives actualOne = gson.fromJson(parser.next(), BagOfPrimitives.class);
  assertEquals("one", actualOne.stringValue);
  BagOfPrimitives actualTwo = gson.fromJson(parser.next(), BagOfPrimitives.class);
  assertEquals("two", actualTwo.stringValue);
  assertFalse(parser.hasNext());
}
 
Example 7
Source Project: docker-maven-plugin   Source File: MiscService.java    License: Apache License 2.0 5 votes vote down vote up
private static String parseSteamForImageId(final InputStream inputStream) {
    InputStreamReader isr = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(isr);

    JsonStreamParser parser = new JsonStreamParser(reader);

    String imageId = null;

    while (parser.hasNext()) {
        JsonElement element = parser.next();
        JsonObject object = element.getAsJsonObject();
        if (object.has("stream")) {
            String text = object.get("stream").getAsString();
            System.out.print(text);
            Matcher matcher = BUILD_IMAGE_ID_EXTRACTION_PATTERN.matcher(text);
            if (matcher.matches()) {
                imageId = matcher.group(2);
            }
        }
        if (object.has("status")) {
            System.out.println(object.get("status").getAsString());
        }
        if (object.has("error")) {
            System.err.println("ERROR: " + object.get("error").getAsString());
        }
    }
    return imageId;
}