Last active
November 5, 2015 14:09
-
-
Save frogermcs/e385aa7376e1076558d7 to your computer and use it in GitHub Desktop.
FlatBuffs - parsing JSON with FlatBuffers source files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'com.jakewharton.hugo' | |
android { | |
compileSdkVersion 23 | |
buildToolsVersion "23.0.1" | |
defaultConfig { | |
applicationId "frogermcs.io.flatbuffs" | |
minSdkVersion 15 | |
targetSdkVersion 23 | |
versionCode 1 | |
versionName "1.0" | |
} | |
} | |
dependencies { | |
compile project(':flatbuffersparser') | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:23.1.0' | |
compile 'com.google.code.gson:gson:2.3.1' | |
compile 'com.jakewharton:butterknife:7.0.1' | |
compile 'io.reactivex:rxjava:1.0.10' | |
compile 'io.reactivex:rxandroid:1.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:1.4.0-beta6' | |
classpath 'com.android.tools.build:gradle-experimental:0.3.0-alpha7' | |
classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1' | |
} | |
} | |
allprojects { | |
repositories { | |
jcenter() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FlatBuffersParser { | |
static { | |
System.loadLibrary("FlatBuffersParser"); | |
} | |
public ByteBuffer parseJson(String json, String schema) { | |
final byte[] bytes = parseJsonNative(json, schema); | |
return ByteBuffer.wrap(bytes); | |
} | |
private native byte[] parseJsonNative(String json, String schema); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.model.library' | |
model { | |
android { | |
compileSdkVersion = 23 | |
buildToolsVersion = "23.0.1" | |
defaultConfig.with { | |
minSdkVersion.apiLevel = 15 | |
targetSdkVersion.apiLevel = 23 | |
} | |
} | |
android.ndk { | |
moduleName = "FlatBuffersParser" | |
platformVersion = 15 | |
cppFlags = ['-std=c++11', '-fexceptions', '-Wall', '-Wno-literal-suffix'] | |
cppFlags += "-I${file("src/main/jni/flatbufferslib")}".toString() | |
ldLibs = ["android", "log"] | |
stl = "gnustl_shared" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __MAIN_H__ | |
#define __MAIN_H__ | |
#include <flatbuffers/idl.h> | |
#include "main.h" | |
JNIEXPORT jbyteArray JNICALL | |
Java_frogermcs_io_flatbuffersparser_FlatBuffersParser_parseJsonNative(JNIEnv *env, | |
jobject instance, | |
jstring json_, | |
jstring schema_) { | |
const char *json = env->GetStringUTFChars(json_, 0); | |
const char *schema = env->GetStringUTFChars(schema_, 0); | |
flatbuffers::Parser parser; | |
bool ok = parser.Parse(schema) && parser.Parse(json); | |
env->ReleaseStringUTFChars(json_, json); | |
env->ReleaseStringUTFChars(schema_, schema); | |
if (ok) { | |
flatbuffers::uoffset_t length = parser.builder_.GetSize(); | |
jbyteArray result = env->NewByteArray(length); | |
uint8_t *bufferPointer = parser.builder_.GetBufferPointer(); | |
env->SetByteArrayRegion(result, 0, length, reinterpret_cast<jbyte *>(bufferPointer)); | |
return result; | |
} | |
} | |
#endif // __MAIN_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include "flatbuffers/flatbuffers.h" | |
extern "C" { | |
JNIEXPORT jbyteArray JNICALL | |
Java_frogermcs_io_flatbuffersparser_FlatBuffersParser_parseJsonNative(JNIEnv *env, | |
jobject instance, | |
jstring json_, | |
jstring schema_); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment