Beyond JSON @ Mobile.Warsaw
-
Upload
maxim-zaks -
Category
Education
-
view
566 -
download
1
Transcript of Beyond JSON @ Mobile.Warsaw
JSON vs. XML• much more human readable
• no schema
• no streaming parser
• too early for financial sector
FlatBuffersis an efficient cross platform serialization
library for C++, Java, C#, Go, Python and JavaScript (C,
PHP & Ruby in progress).
Use generated code for readingpublic sealed class Person : Table {
public static Person GetRootAsPerson(ByteBuffer _bb) {...}
public static Person GetRootAsPerson(ByteBuffer _bb, Person obj) {...}
public string Name { get { ... } }
public int Age { get { ... } }
};
byte[] data = ...;
Person person = Person.GetRootAsPerson(new ByteBuffer(data));
Suffer through generated code for writingpublic sealed class Person : Table { public static Offset<Person> CreatePerson(FlatBufferBuilder builder, StringOffset name = default(StringOffset), int age = 0) { ... }
public static void FinishPersonBuffer(FlatBufferBuilder builder, Offset<Person> offset) { ... }};
FlatBufferBuilder fbb = new FlatBufferBuilder(1);var nameOffset = fbb.CreateString("maxim");var personOffset = Person.CreatePerson(fbb, nameOffset, 34);Person.FinishPersonBuffer(fbb, personOffset);byte[] data = fbb.SizedByteArray();
And if you want binary to JSON./flatc -t person01.fbs -- person01.bin --raw-binary --strict-json
={
"name": "maxim",
"age": 34
}
table Person {
name: string;
age : int;
}
root_type Person;
->table Person {
name: string;
age : int (deprecated);
birthday : double;
}
root_type Person;
table Person {
name: string;
age : int;
}
root_type Person;
->table Person {
name: string (id: 0);
birthday : double (id: 2);
age : int (deprecated, id: 1);
}
root_type Person;
Eager serializationpublic sealed class Person : Table {
public string name;
public int age;
public static Person FromByteArray(byte[] data) {...}
public byte[] ToByteArray(){...}
}
// var person = new Person();
byte[] data = ...;
var person = Person.FromByteArray(data);
person.name = "Alex";
byte[] data2 = person.ToByteArray();
let person = Person(name: "Maxim", age: 34)
let fbData = person.toByteArray
NSData( bytes: UnsafePointer<UInt8>(fbData),
length: fbData.count)
.writeToFile("person.bin", atomically: true)
FlatBuffersSwift benchmark• 65.015 mseconds for parsing JSON (3.6MB)
• 36.478 mseconds for eager FlatBuffers (2.1MB)
• 00.002 mseconds for lazy FlatBuffers (2.1MB)
Outlook for FlatBuffers• real Graph not a Tree
• could support streaming
• is easy to encrypt
• could be very memory efficient with custom compression