Example usage for com.fasterxml.jackson.core JsonFactory createParser

List of usage examples for com.fasterxml.jackson.core JsonFactory createParser

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory createParser.

Prototype

public JsonParser createParser(String content) throws IOException, JsonParseException 

Source Link

Document

Method for constructing parser for parsing contents of given String.

Usage

From source file:com.cinnober.msgcodec.json.JsonCodec.java

@Override
public Object decode(InputStream in) throws IOException {
    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();/*from   w  w  w  .  ja  v a2  s .com*/
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }
    return dynamicGroupHandler.readValue(p);
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param hash The crash hash to retrieve
 * @param diagnostics include detailed diagnostics information for crash
 * @param getOtherCrashes include other crashes and legacy crash groups now part of this group
 * @return Crash object/*from w  w  w .ja va 2 s .  com*/
 */
public Crash getCrash(String hash, boolean diagnostics, boolean getOtherCrashes) {
    String params = "?diagnostics=" + diagnostics + "&get_other_crashes=" + getOtherCrashes;
    Crash crash = null;
    try {
        HttpsURLConnection conn = sendGetRequest(API_CRASH_DETAILS.replace("{hash}", hash), params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crash = mapper.treeToValue(node, Crash.class);
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crash;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID The app ID//from w ww. j  a  v a 2  s  . c  o m
 * @param hash The crash hash to retrieve
 * @return Array of impacted users
 */
public ArrayList<User> getCrashUsersAffected(String appID, String hash) {
    ArrayList<User> users = null;
    try {
        HttpsURLConnection conn = sendGetRequest(
                API_CRASH_USERS_AFFECTED.replace("{appId}", appID).replace("{hash}", hash));
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        users = mapper.readValue(jp, new TypeReference<ArrayList<User>>() {
        });
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return users;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * Query the API for a list of Apps, and the details of each App
 * @return map of App IDs -> App Objects
 *///from ww w  .  j  ava  2 s  .  c  o  m
public Map<String, App> getAppListing() {
    Map<String, App> map = null;
    try {
        String urlParameters = "?attributes=appName%2CappType%2CappVersions%2CcrashPercent%2Cdau%2Clatency%2C"
                + "latestAppStoreReleaseDate%2ClatestVersionString%2ClinkToAppStore%2CiconURL%2Cmau%2Crating%2Crole";
        HttpsURLConnection conn = sendGetRequest(API_APPS, urlParameters);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        map = mapper.readValue(jp, new TypeReference<Map<String, App>>() {
        });
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return map;
}

From source file:com.cinnober.msgcodec.json.JsonCodec.java

/**
 * Read a static group from the specified stream, when the JSON does not contain the '$type' field.
 *
 * @param groupType the expected group type, not null.
 * @param in the stream to read from, not null.
 * @return the decoded value./*from   ww w.j  a v a  2s .c om*/
 * @throws IOException if the underlying stream throws an exception.
 * @throws DecodeException if the value could not be decoded, or if a required field is missing.
 */
public Object decodeStatic(Object groupType, InputStream in) throws IOException {
    StaticGroupHandler groupHandler = staticGroupsByGroupType.get(groupType);
    if (groupHandler == null) {
        throw new IllegalArgumentException("Unknown group type");
    }

    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }
    return groupHandler.readValue(p);
}

From source file:com.cinnober.msgcodec.json.JsonCodec.java

/**
 * Read a static group from the specified stream, when the JSON does not contain the '$type' field.
 *
 * @param groupName the expected group name, not null.
 * @param in the stream to read from, not null.
 * @return the decoded value.//from   w  w  w. java 2  s  . c  om
 * @throws IOException if the underlying stream throws an exception.
 * @throws DecodeException if the value could not be decoded, or if a required field is missing.
 */
public Object decodeStatic(String groupName, InputStream in) throws IOException {
    StaticGroupHandler groupHandler = lookupGroupByName(groupName);
    if (groupHandler == null) {
        throw new IllegalArgumentException("Unknown group name");
    }

    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(in);
    JsonToken token = p.nextToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new DecodeException("Expected {");
    }
    return groupHandler.readValue(p);
}

From source file:com.modelsolv.kaboom.serializer.SerializerTest.java

private JsonNode parseJson(String json) {
    try {/*from   w ww .ja v  a  2s. co  m*/
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = mapper.getFactory();
        JsonParser jp = factory.createParser(json);
        return mapper.readTree(jp);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse JSON returned from serializer.", e);
    }
}

From source file:org.messic.server.api.tagwizard.discogs.DiscogsTAGWizardPlugin.java

private Album getAlbum(String id) {
    String baseURL = "http://api.discogs.com/releases/" + id;
    try {// w  w  w.j a v  a 2  s  .c o m
        URL url = new URL(baseURL);
        Proxy proxy = getProxy();
        URLConnection uc = (proxy != null ? url.openConnection(proxy) : url.openConnection());
        uc.setRequestProperty("User-Agent", "Messic/1.0 +http://spheras.github.io/messic/");

        Album album = new Album();

        album.name = "";
        album.author = "";
        album.comments = "Info obtained by Discogs provider (http://www.discogs.com/)";
        album.year = 1900;
        album.genre = "";

        JsonFactory jsonFactory = new JsonFactory(); // or, for data binding,
        JsonParser jParser = jsonFactory.createParser(uc.getInputStream());
        while (jParser.nextToken() != null) {
            String fieldname = jParser.getCurrentName();
            if ("title".equals(fieldname)) {
                jParser.nextToken();
                album.name = jParser.getText();
            }
            if ("year".equals(fieldname)) {
                jParser.nextToken();
                try {
                    album.year = Integer.valueOf(jParser.getText());
                } catch (Exception e) {
                    album.year = 0;
                }
            }
            if ("notes".equals(fieldname)) {
                jParser.nextToken();
                album.comments = jParser.getText();
            }
            if ("genres".equals(fieldname)) {
                jParser.nextToken();
                jParser.nextToken();
                album.genre = jParser.getText();
                do {
                    jParser.nextToken();
                } while (!"genres".equals(jParser.getCurrentName()));
            }
            if ("artists".equals(fieldname)) {
                jParser.nextToken();
                while (!"name".equals(jParser.getCurrentName())) {
                    jParser.nextToken();
                }
                jParser.nextToken();
                album.author = jParser.getText();
                do {
                    jParser.nextToken();
                } while (!"artists".equals(jParser.getCurrentName()));
            }

            if ("tracklist".equals(fieldname)) {
                album.songs = new ArrayList<Song>();
                do {
                    Song newsong = new Song();
                    int tracknumber = 1;
                    while (jParser.nextToken() != JsonToken.END_OBJECT) {
                        String trackfieldname = jParser.getCurrentName();

                        if ("extraartists".equals(trackfieldname)) {
                            do {
                                while (jParser.nextToken() != JsonToken.END_OBJECT) {

                                }
                                jParser.nextToken();
                            } while (!"extraartists".equals(jParser.getCurrentName()));
                        }
                        if ("position".equals(trackfieldname)) {
                            jParser.nextToken();
                            try {
                                newsong.track = Integer.valueOf(jParser.getText());
                            } catch (Exception e) {
                                newsong.track = tracknumber;
                            }
                            tracknumber++;
                        }
                        if ("title".equals(trackfieldname)) {
                            jParser.nextToken();
                            newsong.name = jParser.getText();
                        }
                    }
                    album.songs.add(newsong);
                    jParser.nextToken();
                } while (!"tracklist".equals(jParser.getCurrentName()));
                jParser.nextToken();
            }
        }

        return album;
    } catch (Exception e) {
        log.error("failed!", e);
    }

    return null;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return/*from   w w  w  . j ava  2 s .  c o m*/
 */
public CrashSummary getErrorPie(String appID, CrashSummary.MetricType metricType, int duration,
        Pie.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_PIE, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return/*from   w  ww.j a  va2 s  .c  o m*/
 */
public CrashSummary getErrorSparklines(String appID, CrashSummary.MetricType metricType, int duration,
        Sparklines.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_SPARKLINES, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}