Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ A fast JSON serializing & deserializing library, accelerated by SIMD.
[![codecov](https://codecov.io/gh/bytedance/sonic-cpp/branch/master/graph/badge.svg)](https://codecov.io/gh/bytedance/sonic-cpp)

## Requirement
- c++11 or above
- x86 platform with AVX2 instruction
- GCC or LLVM compiler (not support MSVC compiler now)
- Linux OS
- C++17 or above
- A supported SIMD target: x86-64 with SSE/AVX2, Arm64 with NEON/SVE2-128,
or RISC-V 64-bit
- GCC or LLVM compiler (MSVC is not supported now)
- Linux, macOS, or Android (`arm64-v8a`)

## Features
- Complete APIs for JSON value manipulation
Expand Down Expand Up @@ -87,6 +88,37 @@ Sonic-Cpp is a header-only library, you only need to include the directory of
Sonic-Cpp header files, such as adding `-I/path/to/sonic/include/` to your
compiler.

### Android

Android `arm64-v8a` builds use the NEON implementation selected by NDK Clang.
Add the Sonic-Cpp headers to the CMake target that backs your Android native
library; no Sonic-Cpp library needs to be linked:

```cmake
add_library(native-lib SHARED native-lib.cpp)

target_compile_features(native-lib PRIVATE cxx_std_17)
target_include_directories(
native-lib PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/third_party/sonic-cpp/include")
```

Do not copy the x86-only `-march=haswell` flag from the command-line examples
below. The Android NDK toolchain supplies the appropriate Arm target flags.

A minimal JNI example is available in [`example/android`](example/android). It
exports `MainActivity.isValidJson(byte[])`; rename the JNI symbol to match your
app's package and class. Cross-compile it from the repository root with an
installed Android NDK:

```shell
cmake -S example/android -B build-android \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-21
cmake --build build-android
```

### Parsing and Serializing
```c++
#include "sonic/sonic.h"
Expand Down
12 changes: 12 additions & 0 deletions example/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.11)

project(sonic_android_example LANGUAGES CXX)

if(NOT ANDROID)
message(FATAL_ERROR "Configure this example with the Android NDK toolchain")
endif()

add_library(sonic_android_example SHARED sonic_android.cpp)
target_compile_features(sonic_android_example PRIVATE cxx_std_17)
target_include_directories(
sonic_android_example PRIVATE "${CMAKE_CURRENT_LIST_DIR}/../../include")
25 changes: 25 additions & 0 deletions example/android/sonic_android.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <jni.h>

#include <string>

#include "sonic/sonic.h"

extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_sonic_MainActivity_isValidJson(JNIEnv* env, jobject,
jbyteArray json) {
if (json == nullptr) {
return JNI_FALSE;
}

const jsize size = env->GetArrayLength(json);
std::string input(static_cast<size_t>(size), '\0');
env->GetByteArrayRegion(json, 0, size,
reinterpret_cast<jbyte*>(input.data()));
if (env->ExceptionCheck()) {
return JNI_FALSE;
}

sonic_json::Document document;
document.Parse(input);
return document.HasParseError() ? JNI_FALSE : JNI_TRUE;
}