diff --git a/README.md b/README.md index 3d2bb960..c6bc325d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" diff --git a/example/android/CMakeLists.txt b/example/android/CMakeLists.txt new file mode 100644 index 00000000..45721262 --- /dev/null +++ b/example/android/CMakeLists.txt @@ -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") diff --git a/example/android/sonic_android.cpp b/example/android/sonic_android.cpp new file mode 100644 index 00000000..d1df1cd4 --- /dev/null +++ b/example/android/sonic_android.cpp @@ -0,0 +1,25 @@ +#include + +#include + +#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), '\0'); + env->GetByteArrayRegion(json, 0, size, + reinterpret_cast(input.data())); + if (env->ExceptionCheck()) { + return JNI_FALSE; + } + + sonic_json::Document document; + document.Parse(input); + return document.HasParseError() ? JNI_FALSE : JNI_TRUE; +}