Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decoding/Encoding Structured Data with Android

Decoding/Encoding Structured Data with Android

Ben Wicks

March 08, 2016
Tweet

More Decks by Ben Wicks

Other Decks in Programming

Transcript

  1. Agenda • Discuss Decoding/Encoding • Explain Structured Data Formats •

    Look at Android Libraries • Interactive Demonstration
  2. Decoding/Encoding Text file 01011011 00110101 01010100 00100100 01001010 01101101 01010101

    00101011 01010101 01101101 01010101 00101011 01101101 01010101 ... Byte stream <?xml version=”1.0”?> <person> <name> Some guy </name> <birthday> 1990-01-01 </birthday> </person> Object Person - name : String - birthday : Date + getName() : String + isBirthday() : boolean Deserialization / Decoding 01011011 00110101 01010100 00100100 01001010 01101101 01010101 00101011 01010101 01101101 01010101 00101011 01101101 01010101 ... Byte stream Text file <?xml version=”1.0”?> <person> <name> Some guy </name> <birthday> 1990-01-01 </birthday> </person> Serialization / Encoding
  3. JSON Pros • Human- and machine-readable • (again, somewhat) Self-describing

    Cons • Each key is spelled out for each entry • Each key has associated delimiters ( “{“, “}”, “,”, “:”, “[“, “]”, “””)
  4. Protocol Buffers Pros • Compact definition • Forward- and backward-compatible

    • Language- and platform-neutral Cons • Not human-readable
  5. Protobuf message definition • Define the message once ◦ Use

    it on client and server side to define your data
  6. Android-specific Libraries • XML ◦ Pull ◦ SAX • JSON

    ◦ Google’s gson ◦ FasterXML’s Jackson ◦ Square’s Moshi
  7. XML: Pull / SAX • Pull Parser is recommended by

    Android docs • You write pretty verbose implementations to decode/encode • My implementations: ◦ PullParser ◦ XmlSaxParser
  8. JSON: gson • Released June 2008 • Still actively developed

    (https://github.com/google/gson) • My implementation: ◦ GsonParser ◦ SpeciesWrapper ◦ Species ▪ @SerializedName annotations
  9. JSON: Jackson • Released March 2008 • Still actively developed

    (https://github.com/FasterXML/jackson) • My implementation: ◦ JacksonHelper ◦ JacksonParser ◦ Species ▪ @JsonCreator and @JsonProperty annotations
  10. JSON: Moshi • Released August 2014 • My implementation: ◦

    MoshiParser ◦ SpeciesDateAdapter ◦ SpeciesWrapper ◦ Species ▪ @Json annotation
  11. Protobuf: Wire • Released December 2012 (https://github.com/square/wire) • Generated file

    added 623 lines • According to square’s blog post, they reduced their method count by nearly 10k by switching their protobuf implementation to wire • My implementation: ◦ SpeciesList.proto and fine-tune my package structure ▪ Wire generated SpeciesList.java
  12. Protobuf: Google’s Protoc • Released July 2008 (https://github.com/google/protobuf) • Generated

    file added 444 lines • My implementation: ◦ SpeciesList.proto and fine-tune my package structure ▪ protoc (with nano option) generated OuterGoogleSpeciesList.java ◦ ProtobufParser ◦ Species#fromGoogleSpecies and #toGoogleSpecies