Comments (10)
@GuangNian10000 小伙子,你提供一下 Bean 类源码及 Json 内容给我,我复现一下这个问题。
from gsonfactory.
我又试了fastjson更快
try {
// 读取文件内容
val file = File(getDirectory(id), "json.txt")
val fileReader = FileReader(file)
val text = BufferedReader(fileReader).use(BufferedReader::readText)
fileReader.close()
val executionTime = measureTimeMillis {
GsonFactory.getSingletonGson().fromJson(text, Bean::class.java) //700
//Gson().fromJson(text, BookCatalog::class.java) //40
}
Timber.d(executionTime)
} catch (e: Exception) {
e.printStackTrace()
null // 返回 null 或者其他指定的错误值来表示读取失败
}
from gsonfactory.
我在家用电脑模拟器试发现GsonFactory解析用时60毫秒左右,可能是因为之前用的测试机,配置不好。但我切到Gson().fromJson用时基本都在20毫秒以下。
不同手机耗时可能有差异,下周我再用测试机跑跑看
from gsonfactory.
荣耀P20 GsonFactory 耗时100左右, Gson30-50
from gsonfactory.
from gsonfactory.
小伙子,我最近修改了 Kotlin data class 类的实例化方式,可能和你这个问题有关系,你下载上面的 aar 包试试看是否还有这个问题?
from gsonfactory.
没有明显区别,模拟器跑在70-100毫秒,Gson20左右
from gsonfactory.
@GuangNian10000 小伙子,我对这个问题进行了复现,核实了确实框架存在此问题,GsonFactory 比直接使用 Gson 实例化要耗时,这部分耗时主要来源于反射 kotlin data class 类,在经过了大量的测试和验证后,发现这个问题属于正常的耗时,这是因为框架内部引用了 kotlin 反射库来创建对象,第一次反射创建 kotlin data class 类耗时在 100 多毫秒,后面反射创建耗时基本在 1 毫秒以内(反射创建 2 ~ 3 个对象在 1 毫秒),加上 Json 数据量大,所以导致反射耗时也随时增加。如果直接换成 Gson 来做,确实没有这个问题,但是有另外的问题,kotlin data class 类字段上面的默认值将无法被保留,这个也是框架要使用 kotlin 反射库来反射创建 kotlin 类的原因,如果不使用此方式将无法实现该需求。
经过认真思考和斟酌后,决定不处理该问题,这是因为 kotlin data class 保留默认值的需求要比大数据解析耗时长优先级要高,里面的原因也很简单,前者的需求量比后者大,并且前者的需求比较常见,后者的需求比较少见。
from gsonfactory.
好的谢谢
from gsonfactory.
问题描述【必填】
我分别使用GsonFactory.getSingletonGson().fromJson(str,type)和Gson().fromJson(str,type)来解析文件大小为 300kb 的 json 字符串,前者耗时 700-800 毫秒,后者100 毫秒不到。是我使用的方式不对吗?还是因为框架为了适配做了很多处理的缘故。 版本 9.2
框架文档是否提及了该问题【必答】
否
是否已经查阅框架文档但还未能解决的【必答】
是
issue 列表中是否有人曾提过类似的问题【必答】
否
是否已经搜索过了 issue 列表但还未能解决的【必答】
是
你可以尝试 https://github.com/NBXXF/gson_plugin
from gsonfactory.
Related Issues (20)
- json名字中带特殊符表情比如😁(unicode)的数据是否有容错处理 HOT 1
- json名字中带特殊符表情比如😁(unicode)的数据是否有容错处理 HOT 11
- 建议适配kotlin默认值 HOT 19
- 容错范围 HOT 2
- 能否通过getDelegateAdapter方法给所有TypeAdapter都套层代理,出错时直接跳到下一个呢 HOT 8
- [疑惑]:关于readme写的为什么gson解析后,kotlin默认值无效的问题不太对 HOT 10
- [Bug]:com.google.gson.internal.ConstructorConstructor$9.construct (ConstructorConstructor.java:262) HOT 3
- [建议]:Gson 框架转义问题 HOT 2
- [疑惑]:<init> argument 4 has type int, got null, HOT 3
- [建议]:兼容类型不匹配的情况 HOT 33
- [建议]:我想去掉这样的警告打印<init> argument 4 has type int, got null HOT 7
- [疑惑]:9.0升级9.2 HOT 2
- [Bug]:9.0升级9.2 HOT 5
- [Bug]:升级到 9.3 直接无法解析 HOT 5
- [Bug]:从 9.0 升级 9.3 出现崩溃 HOT 21
- 9.0升级9.3[Bug]: HOT 1
- [Bug]:解析出现报错 HOT 4
- [疑惑]:为啥fastjson解析的是正常的,本框架解析的不一样 HOT 12
- 8.0版本 jitpack无法引用依赖 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gsonfactory.