项目作者: szhua

项目描述 :
twitter的序列化工具serial 测试。。不好用
高级语言: Java
项目地址: git://github.com/szhua/Serial-vs-Parcelable.git
创建时间: 2017-11-23T06:33:43Z
项目社区:https://github.com/szhua/Serial-vs-Parcelable

开源协议:Apache License 2.0

下载


Serial-vs-Parcelable

twitter的序列化工具serial 测试。。不好用

  1. //============================Twitter.Serial=====================================================
  2. SubObject subObject = new SubObject("1", "szhua");
  3. ExampleObject exampleObject = new ExampleObject(2, "fff", "www.ffff", subObject);
  4. LegacySerial serial = new LegacySerial();
  5. TextView text = findViewById(R.id.content);
  6. // object==>bytes ;
  7. findViewById(R.id.objec_to_serial).setOnClickListener((View view) -> {
  8. try {
  9. bytes = serial.toByteArray(exampleObject, ExampleObject.SERIALIZER);
  10. text.setText(new String(bytes) + "=====Size:" + bytes.length);
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. });
  15. //bytes ==>object;
  16. findViewById(R.id.serial_to_object).setOnClickListener(view -> {
  17. try {
  18. ExampleObject obj = serial.fromByteArray(bytes, ExampleObject.SERIALIZER);
  19. text.setText(obj.toString());
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. }
  25. });
  26. LegacySerial serial2 = new LegacySerial();
  27. //For example, to serialize a list of Strings, you can use:
  28. Serializer<List<String>> a = CollectionSerializers.getListSerializer(CoreSerializers.STRING);
  29. findViewById(R.id.objec_to_serial_pa).setOnClickListener(view -> {
  30. String[] data =new String[]{"今天","mingitan","fwefwe","fdsfsdf","werjwer"} ;
  31. try {
  32. bytes = serial2.toByteArray(Arrays.asList(data),a);
  33. text.setText(new String(bytes, "UTF8"));
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. });
  38. findViewById(R.id.serial_to_object_pa).setOnClickListener(view -> {
  39. try {
  40. List<String> data = serial2.fromByteArray(bytes, a);
  41. text.setText(data.get(0));
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. } catch (ClassNotFoundException e) {
  45. e.printStackTrace();
  46. }
  47. });