All Projects → getActivity → Gsonfactory

getActivity / Gsonfactory

Licence: apache-2.0
Gson 解析容错框架,愿从此再无 Json 解析报错

Programming Languages

java
68154 projects - #9 most used programming language

Labels

Projects that are alternatives of or similar to Gsonfactory

Heyyoo
Heyyoo is a sample social media Android application 📱 built to demonstrate use of Modern Android development tools - (Kotlin, Coroutines, Architecture Components, MVVM, Room, Retrofit, Material Components).
Stars: ✭ 38 (-79.79%)
Mutual labels:  gson
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (-46.81%)
Mutual labels:  gson
Phoenix For Vk
Yet another VK client for Android
Stars: ✭ 131 (-30.32%)
Mutual labels:  gson
Gsonpath
A Java annotation processor library which generates gson type adapters using basic JsonPath style annotations
Stars: ✭ 54 (-71.28%)
Mutual labels:  gson
Kson
Gson TypeAdapter & Factory generator for Kotlin data classes
Stars: ✭ 90 (-52.13%)
Mutual labels:  gson
Rxcache
A local reactive cache for Java and Android. Now, it supports heap memory、off-heap memory and disk cache.
Stars: ✭ 102 (-45.74%)
Mutual labels:  gson
E
A zero-dependency micro library to deal with errors
Stars: ✭ 21 (-88.83%)
Mutual labels:  gson
Trailersapp
A simple demo project for The Movie DB based on MVVM clean architecture.
Stars: ✭ 180 (-4.26%)
Mutual labels:  gson
Todo
简洁清爽的Todo清单工具❤️ (MVP+okhttp3+retrofit+gson)
Stars: ✭ 93 (-50.53%)
Mutual labels:  gson
Gson Jodatime Serialisers
A set of Gson serialiser/deserialisers for dealing with Joda Time entities.
Stars: ✭ 107 (-43.09%)
Mutual labels:  gson
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (-71.28%)
Mutual labels:  gson
Moviefinderusingmvvm Android
🔥 MVVM + Clean Architecture + Best Practices | 🍿Movie Finder is a sample Android application 📱to search movies using OMDb API which is built to demonstrate use of Modern Android development tools - (Kotlin, Coroutines, Kodein, Architecture Components, MVVM, Retrofit, Gson, Material Components) 😊😊😉
Stars: ✭ 66 (-64.89%)
Mutual labels:  gson
Seppb
普兰能效平台开源版(后端)
Stars: ✭ 104 (-44.68%)
Mutual labels:  gson
Nulldefense
Removes invalid objects during Gson parsing which are marked as required, yet null/empty.
Stars: ✭ 41 (-78.19%)
Mutual labels:  gson
Gson Plugin
辅助 Gson 库的 gradle 插件,防止 Json 数据解析类型异常。
Stars: ✭ 133 (-29.26%)
Mutual labels:  gson
Auto Value Bundle
Extends Autovalue to extract data from a bundle into a value object.
Stars: ✭ 36 (-80.85%)
Mutual labels:  gson
Apiclient
A easy to use api client that combines the power of Retrofit, Realm, Gson, Rxjava and Retrolambda in a easy to use library for Java and Android
Stars: ✭ 100 (-46.81%)
Mutual labels:  gson
Jsontokotlinclass
🚀 Plugin for Android Studio And IntelliJ Idea to generate Kotlin data class code from JSON text ( Json to Kotlin )
Stars: ✭ 2,438 (+1196.81%)
Mutual labels:  gson
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-21.28%)
Mutual labels:  gson
Redgram For Reddit
An Open-Sourced Android Reddit Client
Stars: ✭ 106 (-43.62%)
Mutual labels:  gson

Gson 解析容错框架

Gradle 集成

dependencies {
    // Gson 解析容错:https://github.com/getActivity/GsonFactory
    implementation 'com.hjq.gson🏭5.2'
    // Json 解析框架:https://github.com/google/gson
    implementation 'com.google.code.gson:gson:2.8.5'
}

使用文档

  • 请使用框架返回的 Gson 对象来代替项目中的 Gson 对象
// 获取单例的 Gson 对象(已处理容错)
Gson gson = GsonFactory.getSingletonGson();
  • 因为框架中的 Gson 对象已经对解析规则进行了容错处理

其他 API

// 设置自定义的 Gson 对象
GsonFactory.setSingletonGson(Gson gson);

// 创建一个 Gson 构建器(已处理容错)
GsonBuilder gsonBuilder = GsonFactory.newGsonBuilder();

// 注册类型适配器
GsonFactory.registerTypeAdapterFactory(TypeAdapterFactory factory);

// 注册构造函数创建器
GsonFactory.registerInstanceCreator(Type type, InstanceCreator<?> creator);

// 设置 Json 解析容错监听
GsonFactory.setJsonCallback(new JsonCallback() {

    @Override
    public void onTypeException(TypeToken<?> typeToken, String fieldName, JsonToken jsonToken) {
        // Log.e("GsonFactory", "类型解析异常:" + typeToken + "#" + fieldName + ",后台返回的类型为:" + jsonToken);
        // 上报到 Bugly 错误列表
        CrashReport.postCatchedException(new IllegalArgumentException("类型解析异常:" + typeToken + "#" + fieldName + ",后台返回的类型为:" + jsonToken));
    }
});

容错介绍

  • 目前支持容错的数据类型有:

    • Bean 类

    • 数组集合

    • String(字符串)

    • boolean / Boolean(布尔值)

    • int / Integer(整数,属于数值类)

    • long /Long(长整数,属于数值类)

    • float / Float(单精度浮点数,属于数值类)

    • double / Double(双精度浮点数,属于数值类)

    • BigDecimal(精度更高的浮点数,属于数值类)

  • 基本涵盖 99.99% 的开发场景,可以运行 Demo 中的单元测试用例来查看效果:

数据类型 容错的范围 数据示例
bean 集合、字符串、布尔值、数值 []""false0
集合 bean、字符串、布尔值、数值 {}""false0
字符串 bean、集合、布尔值、数值 {}[]false0
布尔值 bean、集合、字符串、数值 {}[]""0
数值 bean、集合、字符串、布尔值 {}[]""false
  • 大家可能觉得 Gson 解析容错没什么,那是因为我们对 Gson 解析失败的场景没有了解过:

    • 类型不对:后台有数据时返回 JsonObject,没数据返回 [],Gson 会直接抛出异常

    • 措手不及:如果客户端定义的是整数,但是后台返回浮点数,Gson 会直接抛出异常

    • 意想不到:如果客户端定义的是布尔值,但是后台返回的是 0 或者 1,Gson 会直接抛出异常

  • 以上情况框架已经做了容错处理,具体处理规则如下:

    • 如果后台返回的类型和客户端定义的类型不匹配,框架就不解析这个字段

    • 如果客户端定义的是整数,但后台返回浮点数,框架就对数值进行取整并赋值给字段

    • 如果客户端定义布尔值,但是后台返回整数,框架则将非 0 的数值则赋值为 true,否则为 false

常见疑问解答

  • Retrofit + RxJava 怎么替换?
Retrofit retrofit = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create(GsonFactory.getSingletonGson()))
        .build();
  • 有没有必要处理 Json 解析容错?

我觉得非常有必要,因为后台返回的数据结构是什么样我们把控不了,但是有一点是肯定的,我们都不希望它崩,因为一个接口的失败导致整个 App 崩溃退出实属不值得,但是 Gson 很敏感,动不动就崩。

  • 我们后台用的是 Java,有必要处理容错吗?

如果你们的后台用的是 PHP,那我十分推荐你使用这个框架,因为 PHP 返回的数据结构很乱,这块经历过的人都懂,没经历过的人怎么说都不懂。

如果你们的后台用的是 Java,那么可以根据实际情况而定,例如我现在的公司用的就是 Java 后台,但是 Bugly 有上报一个关于 Gson 解析的 Crash,所以后台的话只能信一半。

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术分享 QQ 群:78797078

如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作

点击查看捐赠列表

License

Copyright 2020 Huang JinQun

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].