Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.TaskOperationsParser.java

License:Apache License

@Override
public TaskOperations apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }//from   w  ww .  j  a  v  a2 s  .  com
    JsonObject json = jsonElement.getAsJsonObject();
    return new TaskOperations(json.get("editable").getAsBoolean(), json.get("deletable").getAsBoolean(),
            json.get("transitionable").getAsBoolean());
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.TaskParser.java

License:Apache License

@Override
public Task apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }//  ww w. ja va2 s.  c om
    JsonObject json = jsonElement.getAsJsonObject();
    return new Task(json.get("id").getAsLong(), TaskState.valueOf(json.get("state").getAsString()),
            json.get("text").getAsString(), new Date(json.get("createdDate").getAsLong()),
            commentParser().apply(json.getAsJsonObject("anchor")),
            userParser().apply(json.getAsJsonObject("author")),
            taskOperationsParser().apply(json.getAsJsonObject("permittedOperations")));
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.UserParser.java

License:Apache License

@Override
public User apply(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();
    return new User(json.get("id").getAsLong(), json.get("name").getAsString(),
            json.get("emailAddress").getAsString(), json.get("displayName").getAsString(),
            json.get("active").getAsBoolean(), json.get("slug").getAsString(),
            UserType.valueOf(json.get("type").getAsString()));

}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.UserSshKeyParser.java

License:Apache License

@Override
public UserSshKey apply(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();

    return new UserSshKey(json.get("id").getAsLong(), json.get("text").getAsString(),
            json.has(LABEL) ? json.get(LABEL).getAsString() : null);
}

From source file:com.cdancy.artifactory.rest.fallbacks.ArtifactoryFallbacks.java

License:Apache License

public static RequestStatus createPromoteBuildFromError(String message) {
    List<Message> messages = new ArrayList<>();
    List<Error> errors = new ArrayList<>();

    JsonReader reader = new JsonReader(new StringReader(message));
    reader.setLenient(true);//from   w ww  .  jav  a2s.com

    JsonElement possibleMessages = parser.parse(reader).getAsJsonObject().get("messages");
    if (possibleMessages != null) {
        JsonArray jsonArray = possibleMessages.getAsJsonArray();
        Iterator<JsonElement> iter = jsonArray.iterator();
        while (iter.hasNext()) {
            JsonElement jsonElement = iter.next();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Message mess = Message.create(jsonObject.get("level").getAsString(),
                    jsonObject.get("message").getAsString());
            messages.add(mess);
        }
    }

    JsonElement possibleErrors = parser.parse(message).getAsJsonObject().get("errors");
    if (possibleErrors != null) {
        JsonArray jsonArray = possibleErrors.getAsJsonArray();
        Iterator<JsonElement> iter = jsonArray.iterator();
        while (iter.hasNext()) {
            JsonElement jsonElement = iter.next();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Error error = Error.create(jsonObject.get("status").getAsInt(),
                    jsonObject.get("message").getAsString());
            errors.add(error);
        }
    }

    return RequestStatus.create(messages, errors);
}

From source file:com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks.java

License:Apache License

/**
 * Parse list of Error's from output.//from  w ww.j  a  va2  s . c  o m
 *
 * @param output json containing errors hash
 * @return List of Error's or empty list if none could be found
 */
public static List<Error> getErrors(String output) {
    JsonElement element = parser.parse(output);
    JsonObject object = element.getAsJsonObject();
    JsonArray errorsArray = object.get("errors").getAsJsonArray();

    List<Error> errors = Lists.newArrayList();
    Iterator<JsonElement> it = errorsArray.iterator();
    while (it.hasNext()) {
        JsonObject obj = it.next().getAsJsonObject();
        JsonElement context = obj.get("context");
        JsonElement message = obj.get("message");
        JsonElement exceptionName = obj.get("exceptionName");
        Error error = Error.create(!context.isJsonNull() ? context.getAsString() : null,
                !message.isJsonNull() ? message.getAsString() : null,
                !exceptionName.isJsonNull() ? exceptionName.getAsString() : null);
        errors.add(error);
    }

    return errors;
}

From source file:com.cdancy.etcd.rest.fallbacks.EtcdFallbacks.java

License:Apache License

/**
 * Create a User instance from the returned Error message.
 * //from   w  w  w . ja  v a 2 s  . c o  m
 * @param message
 *            error message from etcd
 * @return User instance
 */
public static User createUserFromErrorMessage(String message) {
    JsonElement element = PARSER.parse(message);
    JsonObject object = element.getAsJsonObject();
    ErrorMessage error = ErrorMessage.create(-1, object.get("message").getAsString(), null, -1);

    Pattern pattern = Pattern.compile(".*User (.+) already exists.*");
    Matcher matcher = pattern.matcher(error.message());
    if (matcher.find() && matcher.groupCount() == 1) {
        return User.create(matcher.group(1), null, error);
    } else {
        return null;
    }
}

From source file:com.cdancy.etcd.rest.fallbacks.EtcdFallbacks.java

License:Apache License

/**
 * Create a Role instance from the returned Error message.
 * // w ww  . j av a 2s . c o m
 * @param message
 *            error message from etcd
 * @return Role instance
 */
public static Role createRoleFromErrorMessage(String message) {
    JsonElement element = PARSER.parse(message);
    JsonObject object = element.getAsJsonObject();
    ErrorMessage error = ErrorMessage.create(-1, object.get("message").getAsString(), null, -1);

    Pattern pattern = Pattern.compile(".*Role (.+) already exists.*");
    Matcher matcher = pattern.matcher(error.message());
    if (matcher.find() && matcher.groupCount() == 1) {
        return Role.create(matcher.group(1), null, null, null, error);
    } else {
        return null;
    }
}

From source file:com.cdancy.etcd.rest.fallbacks.EtcdFallbacks.java

License:Apache License

/**
 * Create a Auth instance from the returned Error message.
 * /*  ww w  . j av a  2 s  . co  m*/
 * @param message
 *            error message from etcd
 * @return Auth instance
 */
public static AuthState createAuthStateFromErrorMessage(String message) {
    if (message.contains("auth: No root user available")) {
        JsonElement element = PARSER.parse(message);
        JsonObject object = element.getAsJsonObject();
        ErrorMessage error = ErrorMessage.create(-1, object.get("message").getAsString(), null, -1);
        return AuthState.create(false, error);
    } else if (message.contains("auth: already disabled")) {
        return AuthState.create(false, null);
    } else if (message.contains("auth: already enabled")) {
        return AuthState.create(true, null);
    } else {
        return null;
    }
}

From source file:com.cdancy.etcd.rest.fallbacks.EtcdFallbacks.java

License:Apache License

/**
 * Create a Key instance from the returned Error message.
 * /*from w  w w.  j  a v  a2 s  .  co  m*/
 * @param message
 *            error message from etcd
 * @return Key instance
 */
public static Key createKeyFromErrorMessage(String message) {
    JsonElement element = PARSER.parse(message);
    JsonObject object = element.getAsJsonObject();
    ErrorMessage error = ErrorMessage.create(object.get("errorCode").getAsInt(),
            object.get("message").getAsString(), object.get("cause").getAsString(),
            object.get("index").getAsInt());
    return Key.create(null, null, null, error);
}