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.appslandia.common.json.LocalTimeAdapter.java

License:Open Source License

@Override
public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return LocalTime.parse(json.getAsString(), Jdk8DateUtils.TIME_FORMATTER);
}

From source file:com.appslandia.common.json.OffsetDateTimeAdapter.java

License:Open Source License

@Override
public OffsetDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return OffsetDateTime.parse(json.getAsString(), Jdk8DateUtils.DATETIME_FORMATTER_Z);
}

From source file:com.appslandia.common.json.OffsetTimeAdapter.java

License:Open Source License

@Override
public OffsetTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return OffsetTime.parse(json.getAsString(), Jdk8DateUtils.TIME_FORMATTER_Z);
}

From source file:com.appslandia.common.json.SqlDateAdapter.java

License:Open Source License

@Override
public java.sql.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    synchronized (this.parMutex) {
        try {//from  ww  w  .  j  a va 2  s .  co  m
            return new java.sql.Date(this.parser.parse(json.getAsString()).getTime());
        } catch (ParseException ex) {
            throw new JsonParseException(ex);
        }
    }
}

From source file:com.appslandia.common.json.SqlDateTimeAdapter.java

License:Open Source License

@Override
public java.sql.Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    synchronized (this.parMutex) {
        try {// w ww .ja  va 2  s .c  om
            return new java.sql.Timestamp(this.parser.parse(json.getAsString()).getTime());
        } catch (ParseException ex) {
            throw new JsonParseException(ex);
        }
    }
}

From source file:com.appslandia.common.json.SqlTimeAdapter.java

License:Open Source License

@Override
public java.sql.Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    synchronized (this.parMutex) {
        try {/*from w ww  .j a v  a2  s .co m*/
            return new java.sql.Time(this.parser.parse(json.getAsString()).getTime());
        } catch (ParseException ex) {
            throw new JsonParseException(ex);
        }
    }
}

From source file:com.arangodb.BaseArangoDriver.java

License:Apache License

/**
 * Checks the Http response for database or server errors
 * @param res the response of the database
 * @return The Http status code/*w  w w. j  a  v a2  s .  c o m*/
 * @throws ArangoException if any error happened
 */
private int checkServerErrors(HttpResponseEntity res) throws ArangoException {
    int statusCode = res.getStatusCode();
    if (statusCode >= 400) { // always throws ArangoException
        DefaultEntity defaultEntity = new DefaultEntity();
        if (res.getText() != null && !res.getText().equalsIgnoreCase("") && statusCode != 500) {
            JsonParser jsonParser = new JsonParser();
            JsonElement jsonElement = jsonParser.parse(res.getText());
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            JsonElement errorMessage = jsonObject.get("errorMessage");
            defaultEntity.setErrorMessage(errorMessage.getAsString());
            JsonElement errorNumber = jsonObject.get("errorNum");
            defaultEntity.setErrorNumber(errorNumber.getAsInt());
        } else {
            String statusPhrase = "";
            switch (statusCode) {
            case 400:
                statusPhrase = "Bad Request";
                break;
            case 401:
                statusPhrase = "Unauthorized";
                break;
            case 403:
                statusPhrase = "Forbidden";
                break;
            case 404:
                statusPhrase = "Not Found";
                break;
            case 405:
                statusPhrase = "Method Not Allowed";
                break;
            case 406:
                statusPhrase = "Not Acceptable";
                break;
            case 407:
                statusPhrase = "Proxy Authentication Required";
                break;
            case 408:
                statusPhrase = "Request Time-out";
                break;
            case 409:
                statusPhrase = "Conflict";
                break;
            case 500:
                statusPhrase = "Internal Server Error";
                break;
            default:
                statusPhrase = "unknown error";
                break;
            }

            defaultEntity.setErrorMessage(statusPhrase);
            if (statusCode == 500) {
                defaultEntity.setErrorMessage(statusPhrase + ": " + res.getText());
            }
        }

        defaultEntity.setCode(statusCode);
        defaultEntity.setStatusCode(statusCode);
        defaultEntity.setError(true);
        ArangoException arangoException = new ArangoException(defaultEntity);
        arangoException.setCode(statusCode);
        throw arangoException;
    }

    return statusCode;
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void stringProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//from ww  w .  j ava 2  s  .c o  m
    }
    builder.add(name, prop.getAsString());
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void dateProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//ww w .  ja  v a 2 s  .  c o m
    }
    String string = prop.getAsString();
    Matcher matcher = DATE.matcher(string);
    if (matcher.matches() == false) {
        throw new IOException(
                MessageFormat.format(Messages.getString("JsonObjectDriver.errorInvalidDateFormat"), //$NON-NLS-1$
                        name, string, "yyyy-mm-dd")); //$NON-NLS-1$
    }
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, Integer.parseInt(matcher.group(1)));
    calendar.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1);
    calendar.set(Calendar.DATE, Integer.parseInt(matcher.group(3)));
    builder.add(name, calendar);
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void timeProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//from   www.  j a  v  a 2  s . co  m
    }
    String string = prop.getAsString();
    Matcher matcher = TIME.matcher(string);
    if (matcher.matches() == false) {
        throw new IOException(
                MessageFormat.format(Messages.getString("JsonObjectDriver.errorInvalidTimeFormat"), //$NON-NLS-1$
                        name, string, "hh:mm:ss")); //$NON-NLS-1$
    }
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(1)));
    calendar.set(Calendar.MINUTE, Integer.parseInt(matcher.group(2)) - 1);
    calendar.set(Calendar.SECOND, Integer.parseInt(matcher.group(3)));
    builder.add(name, calendar);
}