Gson源码之泛型、反射、注解,序列化与反序列化(Fastjson,Protocol Buffers)等

> gson源码- https://github.com/google/gson
Gson源码分析之Json结构抽象和注解使用,泛型,Gson关于反射。数据序列化Parcelable、Serializable
Gson序列化(Object--->JsonString)和反序列化(JsonString--->Object);

-- Gson泛型的一些函数:
1.getOwnerType: 代表拥有者类型,比如你的类型是一个内部类,那么这个Type就指向的是你外部类的类型
2.getRawType : 指向泛型的最外部的类型,比如List<User>指向的就是List类型
3.getGenericComponentType : 指向数组成员类型
4.Gson中将Array类型的Class也归纳为GenericArrayTypeImpl类型
5.getEnclosingClass: 获得外部类
-- Gson 源码分析- https://blog.csdn.net/u012422440/article/details/48860893

涉及的Gson中范型和反射的;反射部分的精髓,主要的就是TypeToken和TypeAdapter

TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT);

TypeAdapter<T> typeAdapter = getAdapter(typeToken);

> Fastjson源码- https://github.com/alibaba/fastjson  (注解与asm )
Fastjson内幕- http://wenshao.iteye.com/blog/1142031/
一、Fastjson中Serialzie的优化实现 
1、自行编写类似StringBuilder的工具类SerializeWriter。
2、使用ThreadLocal来缓存buf。 
3、使用asm避免反射
4、使用一个特殊的IdentityHashMap优化性能。
5、缺省启用sort field输出 
6、集成jdk实现的一些优化算法 
二、fastjson的deserializer的主要优化算法 
1、读取token基于预测。
2、sort field fast match算法 
3、使用asm避免反射 
4、对utf-8的json bytes,针对性使用优化的版本来转换编码。 

5、symbolTable算法。

> Protocol Buffers(数据交互,IM中进行数据通信)
Protocol Buffers - Google's data interchange format- https://github.com/google/protobuf
Android下使用Protobuf进行序列化- https://blog.csdn.net/duanbokan/article/details/51029240
GoogleProtoBuf Jar文件- https://download.csdn.net/download/duanbokan/9478313
利用Android Studio将Protobuf文件生成Java文件- https://www.jianshu.com/p/3a543757d086
Protobuf Plugin for Gradle- https://github.com/google/protobuf-gradle-plugin

猜你喜欢

转载自blog.csdn.net/shareus/article/details/80695238