Example usage for com.fasterxml.jackson.databind ObjectMapper readTree

List of usage examples for com.fasterxml.jackson.databind ObjectMapper readTree

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper readTree.

Prototype

public JsonNode readTree(URL source) throws IOException, JsonProcessingException 

Source Link

Document

Method to deserialize JSON content as tree expressed using set of JsonNode instances.

Usage

From source file:com.leclercb.taskunifier.gui.processes.license.ProcessGetTrial.java

public static String getLicense(HttpResponse response) {
    if (!response.isSuccessfull())
        return null;

    try {//w w  w  .j  a v  a 2s. c om
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(response.getContent());

        String code = node.get("code").asText();
        String license = null;

        if (EqualsUtils.equals(code, "0")) {
            license = node.get("data").get("license").asText();
        }

        return license;
    } catch (Exception e) {
        GuiLogger.getLogger().log(Level.WARNING, "Cannot read license", e);

        return null;
    }
}

From source file:net.sf.jasperreports.engine.util.JsonUtil.java

public static JsonNode parseJson(InputStream jsonStream) throws JRException {
    ObjectMapper mapper = createObjectMapper();
    JsonNode jsonTree;//from  w w w .j  av  a2  s .  co m
    try {
        jsonTree = mapper.readTree(jsonStream);
    } catch (JsonProcessingException e) {
        throw new JRException(e);
    } catch (IOException e) {
        throw new JRException(e);
    }
    return jsonTree;
}

From source file:com.liferay.sync.engine.util.SyncSystemTestUtil.java

protected static long getCompanyId(long syncAccountId) throws Exception {
    if (_companyId > 0) {
        return _companyId;
    }//ww w.  j av  a 2 s .co  m

    Map<String, Object> parameters = new HashMap<String, Object>();

    parameters.put("virtualHost", "localhost");

    Session session = SessionManager.getSession(syncAccountId);

    HttpResponse httpResponse = session.executePost("/company/get-company-by-virtual-host", parameters);

    HttpEntity httpEntity = httpResponse.getEntity();

    String response = EntityUtils.toString(httpEntity);

    ObjectMapper mapper = new ObjectMapper();

    JsonNode rootJsonNode = mapper.readTree(response);

    JsonNode companyIdJsonNode = rootJsonNode.get("companyId");

    _companyId = companyIdJsonNode.asLong();

    return _companyId;
}

From source file:com.liferay.sync.engine.util.SyncSystemTestUtil.java

public static long getGuestGroupId(long syncAccountId) throws Exception {
    if (_guestGroupId > 0) {
        return _guestGroupId;
    }//from  ww w  . j ava 2 s  .c o m

    Map<String, Object> parameters = new HashMap<String, Object>();

    parameters.put("companyId", getCompanyId(syncAccountId));
    parameters.put("name", "Guest");

    Session session = SessionManager.getSession(syncAccountId);

    HttpResponse httpResponse = session.executePost("/group/get-group", parameters);

    HttpEntity httpEntity = httpResponse.getEntity();

    String response = EntityUtils.toString(httpEntity);

    ObjectMapper mapper = new ObjectMapper();

    JsonNode rootJsonNode = mapper.readTree(response);

    JsonNode groupIdJsonNode = rootJsonNode.get("groupId");

    _guestGroupId = groupIdJsonNode.asLong();

    return _guestGroupId;
}

From source file:com.sangupta.comparator.JSONComparer.java

/**
 * Compare two JSON string representations.
 * /*from   w  w w .j  ava  2s. c  o  m*/
 * @param json1
 *            the first representation
 * 
 * @param json2
 *            the second representation
 * 
 * @return <code>true</code> if the two JSON representations represent the
 *         same object, <code>false</code> otherwise.
 * 
 * @throws JsonParseException
 *             if something fails
 * 
 * @throws JsonProcessingException
 *             if something fails
 * 
 * @throws IOException
 *             if something fails
 * 
 */
public static boolean compareJson(String json1, String json2) throws JsonProcessingException, IOException {
    if (json1 == null || json2 == null) {
        return false;
    }

    if (json1 == json2) {
        return true;
    }

    JsonFactory factory = new JsonFactory();
    factory.enable(Feature.ALLOW_COMMENTS);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node1 = mapper.readTree(json1);
    JsonNode node2 = mapper.readTree(json2);

    return node1.equals(node2);
}

From source file:com.sangupta.comparator.JSONComparer.java

/**
 * Compare two JSON representations.//www  .  j a va 2  s . c om
 * 
 * @param reader1
 *            the first representation
 * 
 * @param reader2
 *            the second representation
 * 
 * @return <code>true</code> if the two JSON representations represent the
 *         same object, <code>false</code> otherwise.
 * 
 * @throws JsonParseException
 *             if something fails
 * 
 * @throws JsonProcessingException
 *             if something fails
 * 
 * @throws IOException
 *             if something fails
 * 
 */
public static boolean compareJson(Reader reader1, Reader reader2) throws JsonProcessingException, IOException {
    if (reader1 == null || reader2 == null) {
        return false;
    }

    if (reader1 == reader2) {
        return true;
    }

    JsonFactory factory = new JsonFactory();
    factory.enable(Feature.ALLOW_COMMENTS);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node1 = mapper.readTree(reader1);
    JsonNode node2 = mapper.readTree(reader2);

    return node1.equals(node2);
}

From source file:com.sangupta.comparator.JSONComparer.java

/**
 * Compare two JSON representations./*from   w w w.  j  a  v  a 2 s  .  c  om*/
 * 
 * @param stream1
 *            the first representation
 * 
 * @param stream2
 *            the second representation
 * 
 * @return <code>true</code> if the two JSON representations represent the
 *         same object, <code>false</code> otherwise.
 * 
 * @throws JsonParseException
 *             if something fails
 * 
 * @throws JsonProcessingException
 *             if something fails
 * 
 * @throws IOException
 *             if something fails
 * 
 */
public static boolean compareJson(InputStream stream1, InputStream stream2)
        throws JsonProcessingException, IOException {
    if (stream1 == null || stream2 == null) {
        return false;
    }

    if (stream1 == stream2) {
        return true;
    }

    JsonFactory factory = new JsonFactory();
    factory.enable(Feature.ALLOW_COMMENTS);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node1 = mapper.readTree(stream1);
    JsonNode node2 = mapper.readTree(stream2);

    return node1.equals(node2);
}

From source file:jp.opencollector.guacamole.auth.delegated.DelegatedAuthenticationProvider.java

private static Optional<GuacamoleConfiguration> buildConfigurationFromRequest(HttpServletRequest req)
        throws GuacamoleException {
    try {//ww  w .java 2  s  .c  o  m
        if (req.getClass().getName().equals("org.glyptodon.guacamole.net.basic.rest.APIRequest")) {
            final GuacamoleConfiguration config = new GuacamoleConfiguration();
            final String protocol = req.getParameter("protocol");
            if (protocol == null)
                throw new GuacamoleException("required parameter \"protocol\" is missing");
            config.setProtocol(protocol);
            for (Map.Entry<String, String[]> param : req.getParameterMap().entrySet()) {
                String[] values = param.getValue();
                if (values.length > 0)
                    config.setParameter(param.getKey(), values[0]);
            }
            return Optional.of(config);
        } else {
            final ServletInputStream is = req.getInputStream();
            if (!is.isReady()) {
                MediaType contentType = MediaType.parse(req.getContentType());
                boolean invalidContentType = true;
                if (contentType.type().equals("application")) {
                    if (contentType.subtype().equals("json")) {
                        invalidContentType = false;
                    } else if (contentType.subtype().equals("x-www-form-urlencoded")
                            && req.getParameter("token") != null) {
                        return Optional.<GuacamoleConfiguration>absent();
                    }
                }
                if (invalidContentType)
                    throw new GuacamoleException(String.format("expecting application/json, got %s",
                            contentType.withoutParameters()));
                final GuacamoleConfiguration config = new GuacamoleConfiguration();
                try {
                    final ObjectMapper mapper = new ObjectMapper();
                    JsonNode root = (JsonNode) mapper.readTree(
                            createJsonParser(req.getInputStream(), contentType.charset().or(UTF_8), mapper));
                    {
                        final JsonNode protocol = root.get("protocol");
                        if (protocol == null)
                            throw new GuacamoleException("required parameter \"protocol\" is missing");
                        final JsonNode parameters = root.get("parameters");
                        if (parameters == null)
                            throw new GuacamoleException("required parameter \"parameters\" is missing");
                        config.setProtocol(protocol.asText());
                        {
                            for (Iterator<Entry<String, JsonNode>> i = parameters.fields(); i.hasNext();) {
                                Entry<String, JsonNode> member = i.next();
                                config.setParameter(member.getKey(), member.getValue().asText());
                            }
                        }
                    }
                } catch (ClassCastException e) {
                    throw new GuacamoleException("error occurred during parsing configuration", e);
                }
                return Optional.of(config);
            } else {
                return Optional.<GuacamoleConfiguration>absent();
            }
        }
    } catch (IOException e) {
        throw new GuacamoleException("error occurred during retrieving configuration from the request body", e);
    }
}

From source file:com.mirth.connect.client.ui.components.rsta.FindReplaceProperties.java

static FindReplaceProperties fromJSON(String findReplaceJSON) {
    List<String> findHistory = new ArrayList<String>();
    List<String> replaceHistory = new ArrayList<String>();
    Map<String, Boolean> optionsMap = new HashMap<String, Boolean>();

    if (StringUtils.isNotBlank(findReplaceJSON)) {
        try {//from   w  ww . j a  va 2  s.  c  o m
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode rootNode = (ObjectNode) mapper.readTree(findReplaceJSON);

            ArrayNode findHistoryNode = (ArrayNode) rootNode.get("findHistory");
            for (Iterator<JsonNode> it = findHistoryNode.elements(); it.hasNext();) {
                findHistory.add(it.next().asText());
            }

            ArrayNode replaceHistoryNode = (ArrayNode) rootNode.get("replaceHistory");
            for (Iterator<JsonNode> it = replaceHistoryNode.elements(); it.hasNext();) {
                replaceHistory.add(it.next().asText());
            }

            ObjectNode optionsNode = (ObjectNode) rootNode.get("options");
            for (Iterator<Entry<String, JsonNode>> it = optionsNode.fields(); it.hasNext();) {
                Entry<String, JsonNode> entry = it.next();

                if (!entry.getValue().isNull()) {
                    optionsMap.put(entry.getKey(), entry.getValue().asBoolean());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return new FindReplaceProperties(findHistory, replaceHistory, optionsMap);
}

From source file:gate.corpora.twitter.TweetUtils.java

public static List<Tweet> readTweetList(String string, List<String> contentKeys, List<String> featureKeys)
        throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    List<Tweet> tweets = new ArrayList<Tweet>();
    ArrayNode jarray = (ArrayNode) mapper.readTree(string);
    for (JsonNode jnode : jarray) {
        tweets.add(Tweet.readTweet(jnode, contentKeys, featureKeys));
    }/*from  w  w w.  j a v  a 2  s  .  co  m*/
    return tweets;
}