Search in sources :

Example 56 with JsonElement

use of com.google.gson.JsonElement in project remusic by aa112901.

the class HttpUtil method getResposeJsonObject.

public static JsonObject getResposeJsonObject(String action1) {
    try {
        mOkHttpClient.setConnectTimeout(3000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(3000, TimeUnit.MINUTES);
        Request request = new Request.Builder().url(action1).build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String c = response.body().string();
            //                FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/" + System.currentTimeMillis() + ".txt");
            //                fileOutputStream.write(c.getBytes());
            //                fileOutputStream.close();
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(c);
            return el.getAsJsonObject();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) JsonElement(com.google.gson.JsonElement) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParser(com.google.gson.JsonParser)

Example 57 with JsonElement

use of com.google.gson.JsonElement in project sonarqube by SonarSource.

the class ScannerWsClient method tryParseAsJsonError.

public static String tryParseAsJsonError(String responseContent) {
    try {
        JsonParser parser = new JsonParser();
        JsonObject obj = parser.parse(responseContent).getAsJsonObject();
        JsonArray errors = obj.getAsJsonArray("errors");
        List<String> errorMessages = new ArrayList<>();
        for (JsonElement e : errors) {
            errorMessages.add(e.getAsJsonObject().get("msg").getAsString());
        }
        return Joiner.on(", ").join(errorMessages);
    } catch (Exception e) {
        return responseContent;
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) HttpException(org.sonarqube.ws.client.HttpException) MessageException(org.sonar.api.utils.MessageException) JsonParser(com.google.gson.JsonParser)

Example 58 with JsonElement

use of com.google.gson.JsonElement in project sonarqube by SonarSource.

the class QualityGateDetailsFormatter method formatConditionPeriod.

private static void formatConditionPeriod(ProjectStatusWsResponse.Condition.Builder conditionBuilder, JsonObject jsonCondition) {
    JsonElement periodIndex = jsonCondition.get("period");
    if (periodIndex == null) {
        return;
    }
    conditionBuilder.setPeriodIndex(periodIndex.getAsInt());
}
Also used : JsonElement(com.google.gson.JsonElement)

Example 59 with JsonElement

use of com.google.gson.JsonElement in project ninja by ninjaframework.

the class ApiControllerDocTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 60 with JsonElement

use of com.google.gson.JsonElement in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Aggregations

JsonElement (com.google.gson.JsonElement)682 JsonObject (com.google.gson.JsonObject)381 JsonArray (com.google.gson.JsonArray)166 JsonPrimitive (com.google.gson.JsonPrimitive)107 JsonParser (com.google.gson.JsonParser)105 Gson (com.google.gson.Gson)69 Map (java.util.Map)63 ArrayList (java.util.ArrayList)51 HashMap (java.util.HashMap)50 IOException (java.io.IOException)48 Test (org.testng.annotations.Test)45 JsonReader (com.google.gson.stream.JsonReader)31 InputStreamReader (java.io.InputStreamReader)28 GsonBuilder (com.google.gson.GsonBuilder)26 Pager (com.willshex.blogwt.shared.api.Pager)26 Type (java.lang.reflect.Type)25 StringReader (java.io.StringReader)21 Test (org.junit.Test)21 User (com.willshex.blogwt.shared.api.datatype.User)19 JsonParseException (com.google.gson.JsonParseException)17