Search in sources :

Example 41 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonExtension method createGsonBuilder.

public static GsonBuilder createGsonBuilder(MemorySerializer memorySerializer) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Memory.class, memorySerializer);
    builder.registerTypeAdapter(NullMemory.class, memorySerializer);
    builder.registerTypeAdapter(UndefinedMemory.class, memorySerializer);
    builder.registerTypeAdapter(ReferenceMemory.class, memorySerializer);
    builder.registerTypeAdapter(TrueMemory.class, memorySerializer);
    builder.registerTypeAdapter(FalseMemory.class, memorySerializer);
    builder.registerTypeAdapter(LongMemory.class, memorySerializer);
    builder.registerTypeAdapter(DoubleMemory.class, memorySerializer);
    builder.registerTypeAdapter(ObjectMemory.class, memorySerializer);
    builder.registerTypeAdapter(ArrayMemory.class, memorySerializer);
    builder.registerTypeAdapter(BinaryMemory.class, memorySerializer);
    builder.registerTypeAdapter(CharMemory.class, memorySerializer);
    builder.registerTypeAdapter(KeyValueMemory.class, memorySerializer);
    builder.registerTypeAdapter(StringBuilderMemory.class, memorySerializer);
    builder.registerTypeAdapter(StringMemory.class, memorySerializer);
    return builder;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 42 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonFunctions method json_decode.

public static Memory json_decode(Environment env, String json, boolean assoc, int depth) {
    MemoryDeserializer memoryDeserializer = new MemoryDeserializer();
    memoryDeserializer.setEnv(env);
    GsonBuilder gsonBuilder = JsonExtension.createGsonBuilderForDecode(memoryDeserializer);
    memoryDeserializer.setAssoc(assoc);
    memoryDeserializer.setMaxDepth(depth);
    Gson gson = gsonBuilder.create();
    try {
        env.setUserValue(JsonFunctions.class.getName() + "#error", null);
        Memory r = gson.fromJson(json, Memory.class);
        if (r == null)
            return Memory.NULL;
        else
            return assoc ? r.toImmutable() : r;
    } catch (MemoryDeserializer.MaxDepthException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_DEPTH);
    } catch (JsonSyntaxException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_SYNTAX);
    } catch (JsonParseException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_STATE_MISMATCH);
    }
    return Memory.NULL;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Memory(php.runtime.Memory) StringMemory(php.runtime.memory.StringMemory) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) MemoryDeserializer(org.develnext.jphp.json.gson.MemoryDeserializer)

Example 43 with GsonBuilder

use of com.google.gson.GsonBuilder in project Tusky by Vavassor.

the class BaseActivity method createMastodonAPI.

protected void createMastodonAPI() {
    mastodonApiDispatcher = new Dispatcher();
    Gson gson = new GsonBuilder().registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()).registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()).create();
    OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request.Builder builder = originalRequest.newBuilder();
            String accessToken = getAccessToken();
            if (accessToken != null) {
                builder.header("Authorization", String.format("Bearer %s", accessToken));
            }
            Request newRequest = builder.build();
            return chain.proceed(newRequest);
        }
    }).dispatcher(mastodonApiDispatcher).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl()).client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
    mastodonAPI = retrofit.create(MastodonAPI.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) SpannedTypeAdapter(com.keylesspalace.tusky.json.SpannedTypeAdapter) Request(okhttp3.Request) Gson(com.google.gson.Gson) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) StringWithEmojiTypeAdapter(com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter) MastodonAPI(com.keylesspalace.tusky.network.MastodonAPI) Interceptor(okhttp3.Interceptor) StringWithEmoji(com.keylesspalace.tusky.json.StringWithEmoji)

Example 44 with GsonBuilder

use of com.google.gson.GsonBuilder in project Weather by Sparker0i.

the class FetchWeather method gsonWeather.

private WeatherInfo gsonWeather() throws IOException {
    URL day = new URL(builtDay.toString());
    HttpURLConnection connection1 = (HttpURLConnection) day.openConnection();
    connection1.addRequestProperty("x-api-key", preferences.getWeatherKey());
    InputStream content = connection1.getInputStream();
    try {
        //Read the server response and attempt to parse it as JSON
        Reader reader = new InputStreamReader(content);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("M/d/yy hh:mm a");
        Gson gson = gsonBuilder.create();
        WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
        System.out.println(gson.toJson(posts));
        content.close();
        return posts;
    } catch (Exception ex) {
        Log.e("FetchWeather", "Failed to parse JSON due to: " + ex);
    }
    return null;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) URL(java.net.URL) IOException(java.io.IOException)

Example 45 with GsonBuilder

use of com.google.gson.GsonBuilder in project wh-app-android by WhiteHouse.

the class DataTest method createGsonBuilder.

public static GsonBuilder createGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(FavoritesMap.class, new FavoritesMap.FavoritesMapGsonDeserializer());
    builder.registerTypeAdapter(FeedCategoryConfig.class, new FeedCategoryConfig.FeedCategoryConfigGsonDeserializer());
    builder.registerTypeAdapter(FeedCategoryItem.class, new FeedCategoryItem.FeedCategoryItemGsonDeserializer());
    builder.registerTypeAdapter(FeedItem.class, new FeedItem.FeedItemGsonDeserializer());
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
    builder.serializeNulls();
    return builder;
}
Also used : FeedCategoryConfig(gov.whitehouse.data.model.FeedCategoryConfig) FeedItem(gov.whitehouse.data.model.FeedItem) GsonBuilder(com.google.gson.GsonBuilder) FavoritesMap(gov.whitehouse.data.model.FavoritesMap) FeedCategoryItem(gov.whitehouse.data.model.FeedCategoryItem)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)268 Gson (com.google.gson.Gson)175 IOException (java.io.IOException)44 ArrayList (java.util.ArrayList)28 JsonElement (com.google.gson.JsonElement)27 Type (java.lang.reflect.Type)27 Map (java.util.Map)26 JsonObject (com.google.gson.JsonObject)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonSyntaxException (com.google.gson.JsonSyntaxException)15 Date (java.util.Date)14 File (java.io.File)13 InputStream (java.io.InputStream)13 List (java.util.List)13 OkHttpClient (okhttp3.OkHttpClient)13 Retrofit (retrofit2.Retrofit)13 InputStreamReader (java.io.InputStreamReader)11 JsonParseException (com.google.gson.JsonParseException)9 JsonParser (com.google.gson.JsonParser)9