ObjectiveAvro
What is ObjectiveAvro?
ObjectiveAvro is a wrapper on Avro-C, following the API conventions of Foundation's NSJSONSerialization
class.
But what is this Avro thing?
From Avro Documentation:
Apache Avro™ is a data serialization system.
Avro provides:
- Rich data structures.
- A compact, fast, binary data format.
- A container file, to store persistent data.
- Remote procedure call (RPC).
- Simple integration with dynamic languages. Code generation is not required to read or write data files nor to use or implement RPC protocols. Code generation as an optional optimization, only worth implementing for statically typed languages.
Basically, you can serialize data to a fast and compact binary format (which is very handy on a mobile device!). However, there isn't an official API for Objective-C, only C, C++, C#, Java and Python. ObjectiveAvro is the midfield between your Objective-C code and Avro-C.
Usage Examples
Registering schemas
Avro works with schemas. Before using OAVAvroSerialization
to serialize objects, you must register the schemas you'll use.
NSString *schema = @"{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"com.movile.objectiveavro.unittest.v1\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"country\",\"type\":\"string\"},{\"name\":\"age\",\"type\":\"int\"}]}";
OAVAvroSerialization *avro = [[OAVAvroSerialization alloc] init];
NSError *error;
BOOL result = [avro registerSchema:schema error:&error];
Transforming JSON to NSData
NSError *error;
NSDictionary *dict = @{@"name": @"Marcelo Fabri", @"country": @"Brazil", @"age": @20};
NSData *data = [avro dataFromJSONObject:dict forSchemaNamed:@"Person" error:&error];
Transforming NSData to JSON
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"marcelo" ofType:@"avro"]];
NSData *data = [avro JSONObjectFromData:data forSchemaNamed:@"Person" error:&error];
Requirements
ObjectiveAvro requires Xcode 5, targeting either iOS 6.0 and above, or Mac OS 10.8 Mountain Lion (64-bit with modern Cocoa runtime) and above.
ObjectiveAvro also requires Avro-C, which is automatically imported when using CocoaPods.
Installation
ObjectiveAvro is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "ObjectiveAvro"
Testing
To run the testing project; clone the repo, and run pod install
from the Example directory first. You can run the unit tests by pressing CMD + U
on Xcode.
Tests are done with XCTest
and Expecta
.
Known limitations
- Currently, only the following types are supported:
string
,float
,double
,int
,long
,boolean
,null
,bytes
,array
,map
andrecord
. That means thatenum
,union
andfixed
are not currently supported.
Author
Marcelo Fabri, me@marcelofabri.com
License
ObjectiveAvro is available under the MIT license. See the LICENSE file for more info.