Example usage for com.google.gson JsonElement toString

List of usage examples for com.google.gson JsonElement toString

Introduction

In this page you can find the example usage for com.google.gson JsonElement toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:org.esco.portlet.helpinfo.dao.impl.HelpInfoResourceGsonImpl.java

License:Apache License

private List<HelpInfo> getServiceInfos(String url) {
    if (log.isDebugEnabled()) {
        log.debug("Requesting help Infos on URL {}", url);
    }/*from w w  w  .  j a va  2  s . c o m*/
    List<HelpInfo> flL = new ArrayList<>();
    JsonParser parser = new JsonParser();
    JsonElement element;
    try {
        element = parser.parse(url);
    } catch (JsonSyntaxException e) {
        log.error("JsonSyntaxException parsing JSON Element at  :" + url + " with message : " + e.getMessage());
        return flL;
    }

    if (element != null && element.isJsonArray()) {
        final JsonArray helpInfos = element.getAsJsonArray();
        for (JsonElement elm : helpInfos) {
            final JsonObject dataset = elm.getAsJsonObject();
            // getting waited attributes
            final JsonElement jeMediaUrl = dataset.get("mediaUrl");
            final JsonElement jeTitle = dataset.get("title");
            final JsonElement jeSummary = dataset.get("summary");
            final JsonElement jeLink = dataset.get("link");
            // testing if all goes well
            if (jeMediaUrl != null && jeTitle != null && jeSummary != null && jeLink != null) {
                final String mediaUrl = jeMediaUrl.getAsString();
                final String title = jeTitle.getAsString();
                final String summary = jeSummary.getAsString();
                final String link = jeLink.getAsString();
                final String alt = title;
                final HelpInfo info = new HelpInfo(mediaUrl, title, summary, link, alt);
                flL.add(info);
            } else {
                log.warn(
                        "Problems on reading json element, needed attributes aren't found, check json source ! \nJsonElement is {}",
                        elm.toString());
            }
        }
    }
    return flL;
}

From source file:org.factpub.ui.gui.network.AuthMediaWikiIdHTTP.java

License:Open Source License

public static void authMediaWikiAccount(String wikiId, String wikiPass) throws Exception {

    AuthMediaWikiIdHTTP http = new AuthMediaWikiIdHTTP();

    System.out.println("\nAuthMediaWikiId - Send Http POST request");

    String returns1 = http.sendPost1(wikiId, wikiPass, "", "");

    JsonElement jelement1 = new JsonParser().parse(returns1);
    JsonObject jobject1 = jelement1.getAsJsonObject();

    JsonElement token = jobject1.get("token");
    JsonElement sessionid = jobject1.get("sessionid");

    JsonElement resultAuth1 = jobject1.get("result");

    if (resultAuth1.toString().replace("\"", "").equals("NeedToken")) {

        // 2nd attempt 
        String returns2 = http.sendPost2(wikiId, wikiPass, sessionid.toString().replace("\"", ""),
                token.toString().replace("\"", ""));

        JsonElement jelement2 = new JsonParser().parse(returns2);
        JsonObject jobject2 = jelement2.getAsJsonObject();

        JsonElement resultAuth2 = jobject2.get("result");

        String auth = resultAuth2.toString().replace("\"", "");
        //System.out.println(auth);

        switch (auth) {
        case "NoName":
            System.out.println("NoName");
            authorisedUser = "Anonymous";
            break;
        case "NotExists":
            System.out.println("NotExists");
            JOptionPane.showMessageDialog(MainFrame.frameMain, "User does not exist.");
            authorisedUser = "Anonymous";
            break;
        case "WrongPass":
            System.out.println("WrongPass");
            JOptionPane.showMessageDialog(MainFrame.frameMain, "User exists but password is wrong.");
            authorisedUser = "Anonymous";
            break;
        case "Success":
            System.out.println("Success");
            JOptionPane.showMessageDialog(MainFrame.frameMain, "Authentication Success!");

            // get User Name from JSON
            JsonElement username = jobject2.get("lgusername");
            authorisedUser = username.toString().replace("\"", "");
            userPassword = wikiPass;/*from ww  w . j  av  a2s  . c o m*/

            // replace space with underscore
            authorisedUser = authorisedUser.replace(" ", "_");

            //MainFrame.txtUserAuth.setText(authorisedUser);
            break;
        }

    }
}

From source file:org.fenixedu.bennu.adapters.LocalizedStringAdapter.java

License:Open Source License

@Override
public LocalizedString deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    if (json instanceof JsonArray) {
        return LocalizedString.fromJson(json);
    } else {//from   w ww  .  java2  s  . c  o  m
        JsonObject parsed;
        try {
            parsed = new JsonParser()
                    .parse(json.toString().replace("\\", "").replace("\"{", "{").replace("}\"", "}"))
                    .getAsJsonObject();
        } catch (JsonSyntaxException ex) {
            parsed = new JsonParser().parse(json.toString()).getAsJsonObject();
        }

        return LocalizedString.fromJson(parsed);
    }
}

From source file:org.fenixedu.bennu.converters.BeanConverterService.java

License:Open Source License

private Object convertObject(JsonElement jsonElement, TypeDescriptor targetType) {
    String className = jsonElement.getAsJsonObject().get("classname").getAsString();
    Class beanObjectClass;//from   ww  w  .ja v a 2 s.c o m
    try {
        beanObjectClass = Class.forName(className);
        Gson gson = Converters.registerAll(builder).create();
        Object beanObject = gson.fromJson(jsonElement, beanObjectClass);
        //throws ClassCastException if not required domain type
        return targetType.getType().cast(beanObject);
    } catch (Exception e) {
        System.out.print("Error deserializing bean :" + e.getMessage() + "\n");
        System.out.print("Bean JSON :" + jsonElement.toString() + "\n");
        return null;
    }
}

From source file:org.fenixedu.bennu.core.domain.exceptions.BennuCoreDomainException.java

License:Open Source License

public static BennuCoreDomainException wrongJsonFormat(JsonElement json, String expected) {
    return new BennuCoreDomainException("error.bennu.core.wrongJsonFormat", json.toString(), expected);
}

From source file:org.fenixedu.bennu.spring.FenixEDUBaseController.java

License:Open Source License

protected String getBeanJson(final IBean bean) {
    GsonBuilder builder = new GsonBuilder();
    registerTypeAdapters(builder);//from  www.ja va  2s . c o m
    Gson gson = Converters.registerAll(builder).create();

    // CREATING JSON TREE TO ADD CLASSNAME ATTRIBUTE MUST DO THIS AUTOMAGICALLY
    JsonElement jsonTree = gson.toJsonTree(bean);
    jsonTree.getAsJsonObject().addProperty("classname", bean.getClass().getName());
    return jsonTree.toString();
}

From source file:org.fenixedu.cms.domain.SiteAnalytics.java

License:Open Source License

public SiteAnalytics(JsonElement metadata) {
    this.metadata = new Gson().fromJson(metadata.toString(), JsonElement.class);
}

From source file:org.fenixedu.spaces.domain.SpaceClassification.java

License:Open Source License

private JsonElement computeUpdate(JsonElement metadataSpec) {
    JsonElement toInherit = filterInherited(metadataSpec);
    toInherit = new JsonParser().parse(toInherit.toString());
    for (JsonElement ti : toInherit.getAsJsonArray()) {
        JsonObject job = ti.getAsJsonObject();
        job.addProperty("inherited", true);
    }/*www  .ja  va 2 s. c o m*/
    return toInherit;
}

From source file:org.hillview.dataset.api.IJson.java

License:Open Source License

/**
 * Default JSON string representation of this.
 * @return The JSON representation of this.
 *//*from w w w  .j  a v  a  2  s  .c o m*/
default String toJson() {
    JsonElement t = this.toJsonTree();
    return t.toString();
}