Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:com.claresco.tinman.json.XapiCredentialsJson.java

License:Open Source License

@Override
public XapiCredentials deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    JsonObject theJsonCredentials = JsonUtility.convertJsonElementToJsonObject(arg0);

    // Default values
    XapiPerson thePerson = null;/*www .j  a v  a 2  s. c  o m*/
    boolean theHistorical = false;
    ArrayList<String> theScope = new ArrayList<String>();
    String theExpiry = null;
    ArrayList<String> theActivityIDs = new ArrayList<String>();
    String theRegistration = null;

    // Retrieve actor
    if (JsonUtility.hasElement(theJsonCredentials, ACTORS)) {
        thePerson = JsonUtility.delegateDeserialization(arg2, JsonUtility.get(theJsonCredentials, ACTORS),
                XapiPerson.class);
    }
    // Retrieve historical
    if (JsonUtility.hasElement(theJsonCredentials, HISTORICAL)) {
        theHistorical = JsonUtility.getElementAsBool(theJsonCredentials, HISTORICAL);
    }
    // Retrieve expiry
    if (JsonUtility.hasElement(theJsonCredentials, EXPIRY)) {
        theExpiry = JsonUtility.getElementAsString(theJsonCredentials, EXPIRY);
    }
    //Retrieve registration
    if (JsonUtility.hasElement(theJsonCredentials, REGISTRATION)) {
        theRegistration = JsonUtility.getElementAsString(theJsonCredentials, REGISTRATION);
    }
    //Retrieve scope
    if (JsonUtility.hasElement(theJsonCredentials, SCOPE)) {
        JsonArray theArray = theJsonCredentials.getAsJsonArray(SCOPE);
        for (JsonElement e : theArray) {
            theScope.add(e.getAsString());
        }
    }
    //Retrieve activities
    if (JsonUtility.hasElement(theJsonCredentials, ACTIVITY)) {
        JsonArray theArray = theJsonCredentials.getAsJsonArray(ACTIVITY);
        for (JsonElement e : theArray) {
            theActivityIDs.add(e.getAsString());
        }
    }

    DateTime theReceivedTimestamp = DateTime.now();

    try {
        return new XapiCredentials(theScope, theExpiry, theHistorical, thePerson, theActivityIDs,
                theRegistration, theReceivedTimestamp);
    } catch (XapiBadParamException e) {
        throw new XapiBadRequestException(e.getMessage());
    }

}

From source file:com.claresco.tinman.json.XapiInteractionJson.java

License:Open Source License

@Override
public XapiInteraction deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    JsonObject theInteractionJson = JsonUtility.convertJsonElementToJsonObject(arg0);

    String theType = JsonUtility.getElementAsString(theInteractionJson, "interactionType");
    ArrayList<String> theCorrectResponse = new ArrayList<String>();
    ArrayList<XapiInteractionComponent> theChoices = JsonUtility.handleInteraction(theInteractionJson,
            "choices", arg2);
    ArrayList<XapiInteractionComponent> theScale = JsonUtility.handleInteraction(theInteractionJson, "scale",
            arg2);//from   w ww .  j  a  v a2 s .  c  o  m
    ArrayList<XapiInteractionComponent> theTarget = JsonUtility.handleInteraction(theInteractionJson, "target",
            arg2);
    ArrayList<XapiInteractionComponent> theSource = JsonUtility.handleInteraction(theInteractionJson, "source",
            arg2);
    ArrayList<XapiInteractionComponent> theSteps = JsonUtility.handleInteraction(theInteractionJson, "steps",
            arg2);

    // Getting correct response pattern
    if (JsonUtility.hasElement(theInteractionJson, "correctResponsesPattern")) {
        JsonArray theResponse = JsonUtility.getAnArray(theInteractionJson, "correctResponsesPattern");
        for (JsonElement element : theResponse) {
            theCorrectResponse.add(element.getAsString());
        }
    }

    XapiInteraction theInteraction = new XapiInteraction(theType, theCorrectResponse, theChoices, theScale,
            theTarget, theSource, theSteps);

    // This is to make sure the interaction type valid
    if (!theInteraction.isTypeValid()) {
        throw new XapiBadInteractionException(
                "Interaction type : " + theInteraction.getType() + " is not valid");
    }

    return theInteraction;
}

From source file:com.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

@Override
public XapiPerson deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {

    if (arg0.isJsonObject()) {
        JsonObject theJsonObject = JsonUtility.convertJsonElementToJsonObject(arg0);
        ArrayList<String> theName = fillTheList("name", theJsonObject);
        ArrayList<String> theMboxSha1sum = fillTheList("mbox_sha1sum", theJsonObject);
        ArrayList<String> theOpenid = fillTheList("openid", theJsonObject);
        ArrayList<XapiIRI> theMbox = new ArrayList<XapiIRI>();
        ArrayList<XapiAccount> theAccounts = new ArrayList<XapiAccount>();

        JsonArray theArray;//from  w  ww . ja  v a 2s . co  m

        if (JsonUtility.hasElement(theJsonObject, "mbox")) {

            theArray = JsonUtility.getAnArray(theJsonObject, "mbox");
            for (JsonElement e : theArray) {
                if (!e.isJsonNull()) {
                    String theEmailAddress = e.getAsString();
                    if (!theEmailAddress.startsWith("mailto:")) {
                        throw new XapiBadIdentifierException("Mbox has to start with \'mailto:\'");
                    }
                    theMbox.add(new XapiIRI(e.getAsString()));
                }
            }
        }

        if (JsonUtility.hasElement(theJsonObject, "account")) {
            theArray = JsonUtility.getAnArray(theJsonObject, "account");
            for (JsonElement e : theArray) {
                theAccounts.add((XapiAccount) JsonUtility.delegateDeserialization(arg2, e, XapiAccount.class));
            }
        }

        return new XapiPerson(theName, theMbox, theMboxSha1sum, theOpenid, theAccounts);

    } else {
        throw new XapiBadPersonException("XapiPerson should be a JSON object");
    }
}

From source file:com.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

private ArrayList<String> fillTheList(String key, JsonObject theObject) {
    ArrayList<String> theList = new ArrayList<String>();

    if (JsonUtility.hasElement(theObject, key)) {
        JsonElement theElement = JsonUtility.get(theObject, key);
        if (theElement.isJsonArray()) {
            JsonArray theArray = JsonUtility.getAnArray(theObject, key);
            for (JsonElement e : theArray) {
                theList.add(e.getAsString());
            }//from w w w  .j a v a  2s. c  o m
        }
    }

    return theList;
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public String getAsString(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsString();
}

From source file:com.cloudant.client.internal.views.RowImpl.java

License:Open Source License

@Override
public String getId() {
    JsonElement id = row.get("id");
    if (id != null) {
        return id.getAsString();
    } else {/* w  w w.  j a  v a 2s  .c om*/
        return null;
    }
}

From source file:com.code19.library.GsonUtil.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern) {
    Object obj = null;/*from  w  w w.ja  v  a  2 s  . c o  m*/
    gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            String dateStr = json.getAsString();
            try {
                return format.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    }).setDateFormat(pattern).create();
    if (gson != null) {
        obj = gson.fromJson(jsonStr, cl);
    }
    return (T) obj;
}

From source file:com.cognifide.aet.job.common.collectors.accessibility.AccessibilityIssueDeserializer.java

License:Apache License

@Override
public AccessibilityIssue deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext jsonDeserializationContext) {
    JsonObject jo = json.getAsJsonObject();
    String message = jo.get("message").getAsString();
    String code = jo.get("code").getAsString();
    IssueType type = IssueType.byValue(jo.get("type").getAsString());
    JsonElement jsonElement = jo.get("elementString");
    String elementString = NULL_ELEMENT_STRING;
    if (jsonElement != null) {
        elementString = jsonElement.getAsString();
    }//ww  w. j a  v  a  2  s.  co m

    String elementStringAbbrv = StringEscapeUtils.escapeHtml4(elementString);
    elementStringAbbrv = StringUtils.abbreviateMiddle(elementStringAbbrv, "...", MAX_PRE_STRING_LENGTH);

    return new AccessibilityIssue(type, message, code, elementString, elementStringAbbrv);
}

From source file:com.cognifide.aet.job.common.comparators.w3chtml5.parser.W3cHtml5IssueDeserializer.java

License:Apache License

private String getAsString(JsonObject jsonObject, String key) {
    JsonElement element = jsonObject.get(key);
    if (element != null) {
        return element.getAsString();
    }//from   ww  w.j a  v a2 s.  c om
    return null;
}

From source file:com.collaborne.build.txgh.GitHubServlet.java

License:Apache License

protected void processPushEvent(TXGHProject project, JsonObject payloadObject) throws IOException {
    // FIXME: Match up with the 'branch' in the TXGHProject
    if (payloadObject.get("ref").getAsString().equals("refs/heads/master")) {
        GitHubProject gitHubProject = project.getGitHubProject();
        GitHubApi gitHubApi = gitHubProject.getGitHubApi();
        Repository gitHubRepository = gitHubApi.getRepository();
        TransifexProject transifexProject = project.getTransifexProject();

        Map<String, TransifexResource> sourceFileMap = transifexProject.getSourceFileMap();
        Map<String, TransifexResource> updatedTransifexResourceMap = new LinkedHashMap<>();

        for (JsonElement commitElement : payloadObject.get("commits").getAsJsonArray()) {
            JsonObject commitObject = commitElement.getAsJsonObject();
            for (JsonElement modified : commitObject.get("modified").getAsJsonArray()) {
                String modifiedSourceFile = modified.getAsString();
                LOGGER.debug("Modified source file: " + modifiedSourceFile);
                if (sourceFileMap.containsKey(modifiedSourceFile)) {
                    LOGGER.debug("Watched source file has been found: " + modifiedSourceFile);
                    updatedTransifexResourceMap.put(commitObject.get("id").getAsString(),
                            sourceFileMap.get(modifiedSourceFile));
                }//from   w  ww.j  ava 2 s .c  o  m
            }
        }

        for (Entry<String, TransifexResource> entry : updatedTransifexResourceMap.entrySet()) {

            String sourceFile = entry.getValue().getSourceFile();
            LOGGER.debug("Modified source file (watched): " + sourceFile);
            String treeSha = gitHubApi.getCommitTreeSha(gitHubRepository, entry.getKey());
            Tree tree = gitHubApi.getTree(gitHubRepository, treeSha);
            for (TreeEntry file : tree.getTree()) {
                LOGGER.debug("Repository file: " + file.getPath());
                if (sourceFile.equals(file.getPath())) {
                    transifexProject.getTransifexApi().update(entry.getValue(),
                            gitHubApi.getFileContent(gitHubRepository, file.getSha()));
                    break;
                }
            }
        }
    }
}