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.intellij.plugins.haxe.haxelib.HaxelibMetadata.java

License:Apache License

@Nullable
private String getString(String attr) {
    if (null != root) {
        JsonElement elem = root.get(attr);
        if (null != elem) {
            return elem.getAsString();
        }//  ww  w. j a v a2s.co  m
    }
    return null;
}

From source file:com.intellij.plugins.haxe.haxelib.HaxelibMetadata.java

License:Apache License

@Nullable
private List<String> getStringList(String attr) {
    if (null != root) {
        JsonArray ary = root.get(attr).getAsJsonArray();
        List<String> list = new ArrayList<String>(ary.size());
        for (JsonElement el : ary) {
            list.add(el.getAsString());
        }//from w ww  .  j a  v  a2  s.c om
        return list;
    }
    return null;
}

From source file:com.intuit.wasabi.tests.service.IntegrationAuthorization.java

License:Apache License

/**
 * Test getting user permission/*w ww . ja v  a2  s.c om*/
 */
@Test(dependsOnMethods = { "t_createExperiments" })
public void t_getUserPermissions() {
    String uri = "authorization/users/" + userName + "/permissions";
    response = apiServerConnector.doGet(uri);
    assertReturnCode(response, HttpStatus.SC_OK);
    List<Map<String, Object>> jsonStrings = response.jsonPath().getList("permissionsList");
    Assert.assertTrue(!jsonStrings.isEmpty());
    boolean foundApp = false;
    for (@SuppressWarnings("rawtypes")
    Map jsonMap : jsonStrings) {
        String permissionJStr = simpleGson.toJson(jsonMap);
        JsonElement jElement = new JsonParser().parse(permissionJStr);
        JsonObject jobject = jElement.getAsJsonObject();

        JsonElement jElement2 = jobject.get("permissions");
        Assert.assertTrue(jElement2.toString().contains("SUPERADMIN"));

        JsonElement jsonElement = jobject.get("applicationName");
        String applicationName = jsonElement.getAsString();
        if (applicationName.equals(experiment.applicationName)) {
            LOGGER.info("Found application=" + experiment.applicationName);
            foundApp = true;
            break;
        }
    }
    Assert.assertTrue(foundApp);
}

From source file:com.intuit.wasabi.tests.service.IntegrationAuthorization.java

License:Apache License

/**
 * Test getting application user roles//w  w  w  .j  av a 2  s  . c om
 */
@Test(dependsOnMethods = { "t_getUserRoles" })
public void t_getApplicationUsersByRole() {

    String uri = "authorization/applications/" + experiment.applicationName;
    response = apiServerConnector.doGet(uri);

    assertReturnCode(response, HttpStatus.SC_OK);
    List<Map<String, Object>> jsonStrings = response.jsonPath().getList("roleList");
    Assert.assertTrue(!jsonStrings.isEmpty());
    Assert.assertTrue(jsonStrings.size() == 1);
    // UserRoleList
    for (@SuppressWarnings("rawtypes")
    Map jsonMap : jsonStrings) {
        String userRoleListJStr = simpleGson.toJson(jsonMap);
        JsonElement jElement = new JsonParser().parse(userRoleListJStr);
        JsonObject jobject = jElement.getAsJsonObject();

        JsonElement jsonElement = jobject.get("firstName");
        Assert.assertEquals(jsonElement.getAsString(), "Wasabi");
        jsonElement = jobject.get("lastName");
        Assert.assertEquals(jsonElement.getAsString(), userLastName);
        jsonElement = jobject.get("role");
        Assert.assertEquals(jsonElement.getAsString(), "ADMIN");
        jsonElement = jobject.get("userEmail");
        Assert.assertEquals(jsonElement.getAsString(), userEmail);
        jsonElement = jobject.get("userID");
        Assert.assertEquals(jsonElement.getAsString(), userName);
        jsonElement = jobject.get("applicationName");
        Assert.assertEquals(jsonElement.getAsString(), experiment.applicationName);
    }
}

From source file:com.javacreed.examples.gson.part1.BookDeserializer.java

License:Apache License

@Override
public Book deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();

    final JsonElement jsonTitle = jsonObject.get("title");
    final String title = jsonTitle.getAsString();

    final String isbn10 = jsonObject.get("isbn-10").getAsString();
    final String isbn13 = jsonObject.get("isbn-13").getAsString();

    final JsonArray jsonAuthorsArray = jsonObject.get("authors").getAsJsonArray();
    final String[] authors = new String[jsonAuthorsArray.size()];
    for (int i = 0; i < authors.length; i++) {
        final JsonElement jsonAuthor = jsonAuthorsArray.get(i);
        authors[i] = jsonAuthor.getAsString();
    }/*w w w .jav  a  2s  .c o  m*/

    final Book book = new Book();
    book.setTitle(title);
    book.setIsbn10(isbn10);
    book.setIsbn13(isbn13);
    book.setAuthors(authors);
    return book;
}

From source file:com.jboss.examples.SQLDateTypeAdapter.java

License:Open Source License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a string value");
    }//from w  ww.  j  av  a 2  s .  c  om

    try {
        return format.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }

}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

@Nullable
@Override//from www  .  j  a  v a 2s.c o  m
public Object get(String keyPath) {
    PreCon.notNull(keyPath);

    keyPath = getFullPath(keyPath);

    if (keyPath.isEmpty())
        return _object;

    String[] path = TextUtils.PATTERN_DOT.split(keyPath);

    JsonElement element = getJsonElement(path);
    if (element == null || element.isJsonObject())
        return null;

    if (element.isJsonArray()) {
        return getStringList(keyPath, null);
    }

    return element.getAsString();
}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

@Override
public Map<String, Object> getAllValues() {

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

    JsonElement selfElement = getJsonElement("");
    if (selfElement == null)
        return result;

    if (!selfElement.isJsonObject()) {
        result.put("", selfElement.getAsString());
        return result;
    }//from  w  ww.j ava 2  s.  com

    JsonObject object = selfElement.getAsJsonObject();

    String basePath = "";

    getAllValuesRecursive(object, basePath, result);

    return result;
}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

@Override
@Nullable/*from   w  w  w.  j av a 2 s  . com*/
protected Object getStringObject(String keyPath) {
    JsonElement element = getJsonElement(keyPath);
    if (element == null || element.isJsonObject())
        return null;

    return element.getAsString();
}

From source file:com.jcwhatever.nucleus.storage.JsonDataNode.java

License:MIT License

private void getAllValuesRecursive(JsonObject object, String basePath, Map<String, Object> result) {

    Set<Map.Entry<String, JsonElement>> entrySet = object.entrySet();

    for (Map.Entry<String, JsonElement> entry : entrySet) {
        String name = basePath.isEmpty() ? entry.getKey() : basePath + '.' + entry.getKey();
        JsonElement element = entry.getValue();

        if (element.isJsonObject()) {
            getAllValuesRecursive(element.getAsJsonObject(), name, result);
        } else {/*ww w.  java2  s . co m*/
            result.put(name, element.getAsString());
        }
    }
}