五、完整代码
1、主界面代码
package kim.hsl.protobuf import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import com.alibaba.fastjson.JSON import com.example.tutorial.protos.AddressBook import com.example.tutorial.protos.Person import com.google.gson.Gson class MainActivity : AppCompatActivity() { companion object{ val TAG = "MainActivity" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 测试 Protobuf 性能 protobufTest() // 测试 Json 性能 JsonTest().jsonTest() } fun protobufTest(){ // 创建 Person.PhoneNumber.Builder 对象 var phoneNumber1Builder: Person.PhoneNumber.Builder = Person.PhoneNumber.newBuilder(). setNumber("666") // 创建 Person.Builder 对象 var person1Builder: Person.Builder = Person.newBuilder(). setName("Tom"). setId(0). addPhones(phoneNumber1Builder) // 创建 Person.PhoneNumber.Builder 对象 var phoneNumber2Builder: Person.PhoneNumber.Builder = Person.PhoneNumber.newBuilder(). setNumber("888") // 创建 Person.Builder 对象 var person2Builder: Person.Builder = Person.newBuilder(). setName("Jerry"). setId(1). addPhones(phoneNumber2Builder) // 使用 newBuilder 方法创建 AddressBook.Builder 对象 var addressBookBuilder: AddressBook.Builder = AddressBook.newBuilder(). addPeople(person1Builder). addPeople(person2Builder) // 将上述各个 Builder 拼装完毕后 , 最后调用 build // 即可得到最终对象 var addressBook: AddressBook = addressBookBuilder.build() // 序列化操作 var serializeStart = System.currentTimeMillis() // 将 addressBook 对象转为字节数组 var bytes: ByteArray = addressBook.toByteArray() Log.i(TAG, "序列化耗时 ${System.currentTimeMillis() - serializeStart} ms , " + "序列化大小 ${bytes.size} 字节") // 反序列化操作 var deserializeStart = System.currentTimeMillis() var deserializeAddressBook: AddressBook = AddressBook.parseFrom(bytes) Log.i(TAG, "反序列化耗时 ${System.currentTimeMillis() - deserializeStart} ms") } }
2、JSON 测试代码
package kim.hsl.protobuf import android.util.Log import com.alibaba.fastjson.JSON import com.google.gson.Gson class JsonTest { fun jsonTest(){ // json 测试 // 初始化 kim.hsl.protobuf.AddressBook 对象 var addressBook: AddressBook = AddressBook() addressBook.persons = mutableListOf( Person("Tom", 0, "", mutableListOf(PhoneNumber("666", PhoneNumber.PhoneType.MOBILE))), Person("Jerry", 1, "", mutableListOf(PhoneNumber("888", PhoneNumber.PhoneType.MOBILE))) ) // 测试 fastjson var fastjsonStart = System.currentTimeMillis() // fastjson 序列化 : 将 addressBook 转为 json 字符串 var jsonString: String = JSON.toJSONString(addressBook) // 将字符串转为 Byte 数组 var bytes = jsonString.toByteArray() Log.i( MainActivity.TAG, "fastjson 序列化耗时 ${System.currentTimeMillis() - fastjsonStart} ms , " + "序列化大小 ${bytes.size} 字节") fastjsonStart = System.currentTimeMillis() // 序列化操作 JSON.parseObject(String(bytes), com.example.tutorial.protos.AddressBook::class.java) Log.i(MainActivity.TAG, "fastjson 反序列化耗时 ${System.currentTimeMillis() - fastjsonStart} ms") // 测试 gson var gsonStart = System.currentTimeMillis() // gson 序列化 : 将 addressBook 转为 json 字符串 jsonString = Gson().toJson(addressBook) // 将字符串转为 Byte 数组 bytes = jsonString.toByteArray() Log.i( MainActivity.TAG, "gson 序列化耗时 ${System.currentTimeMillis() - gsonStart} ms , " + "序列化大小 ${bytes.size} 字节") gsonStart = System.currentTimeMillis() Gson().fromJson(String(bytes), com.example.tutorial.protos.AddressBook::class.java) Log.i(MainActivity.TAG, "gson 反序列化耗时 ${System.currentTimeMillis() - gsonStart} ms") } } class PhoneNumber{ enum class PhoneType{ MOBILE, HOME, WORK; } var number: String = "" var type: PhoneType = PhoneType.MOBILE constructor(number: String, type: PhoneType) { this.number = number this.type = type } } class Person{ lateinit var name: String var id: Int = 0 var email: String = "" var phones: MutableList<PhoneNumber> = mutableListOf() constructor(name: String, id: Int, email: String, phones: MutableList<PhoneNumber>) { this.name = name this.id = id this.email = email this.phones = phones } } class AddressBook{ var persons: MutableList<Person> = mutableListOf() }
3、执行结果
执行结果 :
2021-04-27 22:51:02.513 16698-16698/kim.hsl.protobuf I/MainActivity: 序列化耗时 4 ms , 序列化大小 34 字节 2021-04-27 22:51:02.515 16698-16698/kim.hsl.protobuf I/MainActivity: 反序列化耗时 2 ms 2021-04-27 22:51:02.539 16698-16698/kim.hsl.protobuf I/MainActivity: fastjson 序列化耗时 21 ms , 序列化大小 169 字节 2021-04-27 22:51:02.568 16698-16698/kim.hsl.protobuf I/MainActivity: fastjson 反序列化耗时 29 ms 2021-04-27 22:51:02.597 16698-16698/kim.hsl.protobuf I/MainActivity: gson 序列化耗时 29 ms , 序列化大小 169 字节 2021-04-27 22:51:02.609 16698-16698/kim.hsl.protobuf I/MainActivity: gson 反序列化耗时 12 ms
六、参考资料
Protobuf 参考资料 :
Protobuf 官网主页 : https://developers.google.com/protocol-buffers
Protobuf 语法指南 : https://developers.google.com/protocol-buffers/docs/proto
Protobuf Java 语言对应用法 : https://developers.google.com/protocol-buffers/docs/javatutorial
Protobuf 下载地址 : https://developers.google.com/protocol-buffers/docs/downloads
Protobuf 源码地址 : https://github.com/protocolbuffers/protobuf
Protobuf 发布版本下载地址 : https://github.com/protocolbuffers/protobuf/releases
protobuf-gradle-plugin 项目地址 : https://github.com/google/protobuf-gradle-plugin
博客源码 :
GitHub 地址 : https://github.com/han1202012/Protocol_Buffers
CSDN 快照 : https://download.csdn.net/download/han1202012/18181938