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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations