Skip to content

Commit

Permalink
Merge pull request #3 from miloyip/master
Browse files Browse the repository at this point in the history
Merge back
  • Loading branch information
StilesCrisis authored Mar 17, 2017
2 parents 5b593bb + e5635fb commit 1391e42
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 118 deletions.
38 changes: 29 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,35 @@ install(DIRECTORY example/
# Provide config and version files to be used by other applications
# ===============================

export(PACKAGE ${PROJECT_NAME})

# cmake-modules
CONFIGURE_FILE(${PROJECT_NAME}Config.cmake.in
${PROJECT_NAME}Config.cmake
@ONLY)
CONFIGURE_FILE(${PROJECT_NAME}ConfigVersion.cmake.in
${PROJECT_NAME}ConfigVersion.cmake
@ONLY)
################################################################################
# Export package for use from the build tree
EXPORT( PACKAGE ${PROJECT_NAME} )

# Create the RapidJSONConfig.cmake file for other cmake projects.
# ... for the build tree
SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR})
CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY )
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake @ONLY)

# ... for the install tree
SET( CMAKECONFIG_INSTALL_DIR lib/cmake/${PROJECT_NAME} )
FILE( RELATIVE_PATH REL_INCLUDE_DIR
"${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR}"
"${CMAKE_INSTALL_PREFIX}/include" )

SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/${REL_INCLUDE_DIR}" )
SET( CONFIG_SOURCE_DIR )
SET( CONFIG_DIR )
CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake @ONLY )

INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake"
DESTINATION ${CMAKECONFIG_INSTALL_DIR} )

# Install files
INSTALL(FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
Expand Down
18 changes: 15 additions & 3 deletions RapidJSONConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
get_filename_component(RAPIDJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(RAPIDJSON_INCLUDE_DIRS "@INCLUDE_INSTALL_DIR@")
message(STATUS "RapidJSON found. Headers: ${RAPIDJSON_INCLUDE_DIRS}")
################################################################################
# RapidJSON source dir
set( RapidJSON_SOURCE_DIR "@CONFIG_SOURCE_DIR@")

################################################################################
# RapidJSON build dir
set( RapidJSON_DIR "@CONFIG_DIR@")

################################################################################
# Compute paths
get_filename_component(RapidJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

set( RapidJSON_INCLUDE_DIR "@RapidJSON_INCLUDE_DIR@" )
set( RapidJSON_INCLUDE_DIRS "@RapidJSON_INCLUDE_DIR@" )
message(STATUS "RapidJSON found. Headers: ${RapidJSON_INCLUDE_DIRS}")
165 changes: 83 additions & 82 deletions example/lookaheadparser/lookaheadparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
//
// After calling EnterObject, you retrieve keys via NextObjectKey() and values via
// the normal getters. When NextObjectKey() returns null, you have exited the
// object, or you can call ExitObject() to skip to the end of the object
// object, or you can call SkipObject() to skip to the end of the object
// immediately. If you fetch the entire object (i.e. NextObjectKey() returned null),
// you should not call ExitObject().
// you should not call SkipObject().
//
// After calling EnterArray(), you must alternate between calling NextArrayValue()
// to see if the array has more data, and then retrieving values via the normal
// getters. You can call ExitArray() to skip to the end of the array immediately.
// getters. You can call SkipArray() to skip to the end of the array immediately.
// If you fetch the entire array (i.e. NextArrayValue() returned null),
// you should not call ExitArray().
// you should not call SkipArray().
//
// This parser uses in-situ strings, so the JSON buffer will be altered during the
// parse.
Expand All @@ -37,15 +37,15 @@ using namespace rapidjson;

class LookaheadParserHandler {
public:
bool Null() { st_ = kHasValue; v_.SetNull(); return true; }
bool Bool(bool b) { st_ = kHasValue; v_.SetBool(b); return true; }
bool Int(int i) { st_ = kHasValue; v_.SetInt(i); return true; }
bool Uint(unsigned u) { st_ = kHasValue; v_.SetUint(u); return true; }
bool Int64(int64_t i) { st_ = kHasValue; v_.SetInt64(i); return true; }
bool Uint64(uint64_t u) { st_ = kHasValue; v_.SetUint64(u); return true; }
bool Double(double d) { st_ = kHasValue; v_.SetDouble(d); return true; }
bool Null() { st_ = kHasNull; v_.SetNull(); return true; }
bool Bool(bool b) { st_ = kHasBool; v_.SetBool(b); return true; }
bool Int(int i) { st_ = kHasNumber; v_.SetInt(i); return true; }
bool Uint(unsigned u) { st_ = kHasNumber; v_.SetUint(u); return true; }
bool Int64(int64_t i) { st_ = kHasNumber; v_.SetInt64(i); return true; }
bool Uint64(uint64_t u) { st_ = kHasNumber; v_.SetUint64(u); return true; }
bool Double(double d) { st_ = kHasNumber; v_.SetDouble(d); return true; }
bool RawNumber(const char*, SizeType, bool) { return false; }
bool String(const char* str, SizeType length, bool) { st_ = kHasValue; v_.SetString(str, length); return true; }
bool String(const char* str, SizeType length, bool) { st_ = kHasString; v_.SetString(str, length); return true; }
bool StartObject() { st_ = kEnteringObject; return true; }
bool Key(const char* str, SizeType length, bool) { st_ = kHasKey; v_.SetString(str, length); return true; }
bool EndObject(SizeType) { st_ = kExitingObject; return true; }
Expand All @@ -58,8 +58,12 @@ class LookaheadParserHandler {

protected:
enum LookaheadParsingState {
kInit,
kError,
kHasValue,
kHasNull,
kHasBool,
kHasNumber,
kHasString,
kHasKey,
kEnteringObject,
kExitingObject,
Expand All @@ -75,7 +79,7 @@ class LookaheadParserHandler {
static const int parseFlags = kParseDefaultFlags | kParseInsituFlag;
};

LookaheadParserHandler::LookaheadParserHandler(char* str) : ss_(str) {
LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), st_(kInit), r_(), ss_(str) {
r_.IterativeParseInit();
ParseNext();
}
Expand All @@ -93,10 +97,8 @@ class LookaheadParser : protected LookaheadParserHandler {
public:
LookaheadParser(char* str) : LookaheadParserHandler(str) {}

void EnterObject();
void EnterArray();
void ExitObject();
void ExitArray();
bool EnterObject();
bool EnterArray();
const char* NextObjectKey();
bool NextArrayValue();
int GetInt();
Expand All @@ -105,57 +107,52 @@ class LookaheadParser : protected LookaheadParserHandler {
bool GetBool();
void GetNull();

void SkipObject();
void SkipArray();
void SkipValue();
Value* PeekValue();
int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array)

bool IsValid() { return st_ != kError; }

protected:
void SkipOut(int depth);
};

void LookaheadParser::EnterObject() {
bool LookaheadParser::EnterObject() {
if (st_ != kEnteringObject) {
st_ = kError;
return;
return false;
}

ParseNext();
return true;
}

void LookaheadParser::EnterArray() {
bool LookaheadParser::EnterArray() {
if (st_ != kEnteringArray) {
st_ = kError;
return;
return false;
}

ParseNext();
}

void LookaheadParser::ExitObject() {
while (NextObjectKey()) {
SkipValue();
}
}

void LookaheadParser::ExitArray() {
while (NextArrayValue()) {
SkipValue();
}
return true;
}

const char* LookaheadParser::NextObjectKey() {
if (st_ == kExitingObject) {
if (st_ == kHasKey) {
const char* result = v_.GetString();
ParseNext();
return 0;
return result;
}

if (st_ != kHasKey || !v_.IsString()) {
if (st_ != kExitingObject) {
st_ = kError;
return 0;
}

const char* result = v_.GetString();
ParseNext();
return result;
return 0;
}

bool LookaheadParser::NextArrayValue() {
Expand All @@ -164,11 +161,16 @@ bool LookaheadParser::NextArrayValue() {
return false;
}

if (st_ == kError || st_ == kExitingObject || st_ == kHasKey) {
st_ = kError;
return false;
}

return true;
}

int LookaheadParser::GetInt() {
if (st_ != kHasValue || !v_.IsInt()) {
if (st_ != kHasNumber || !v_.IsInt()) {
st_ = kError;
return 0;
}
Expand All @@ -179,7 +181,7 @@ int LookaheadParser::GetInt() {
}

double LookaheadParser::GetDouble() {
if (st_ != kHasValue || !v_.IsNumber()) {
if (st_ != kHasNumber) {
st_ = kError;
return 0.;
}
Expand All @@ -190,7 +192,7 @@ double LookaheadParser::GetDouble() {
}

bool LookaheadParser::GetBool() {
if (st_ != kHasValue || !v_.IsBool()) {
if (st_ != kHasBool) {
st_ = kError;
return false;
}
Expand All @@ -201,7 +203,7 @@ bool LookaheadParser::GetBool() {
}

void LookaheadParser::GetNull() {
if (st_ != kHasValue || !v_.IsNull()) {
if (st_ != kHasNull) {
st_ = kError;
return;
}
Expand All @@ -210,7 +212,7 @@ void LookaheadParser::GetNull() {
}

const char* LookaheadParser::GetString() {
if (st_ != kHasValue || !v_.IsString()) {
if (st_ != kHasString) {
st_ = kError;
return 0;
}
Expand All @@ -220,58 +222,57 @@ const char* LookaheadParser::GetString() {
return result;
}

void LookaheadParser::SkipValue() {
int depth = 0;
void LookaheadParser::SkipOut(int depth) {
do {
switch (st_) {
case kEnteringArray:
case kEnteringObject:
++depth;
break;

case kExitingArray:
case kExitingObject:
--depth;
break;

case kError:
return;

case kHasKey:
case kHasValue:
break;
if (st_ == kEnteringArray || st_ == kEnteringObject) {
++depth;
}
else if (st_ == kExitingArray || st_ == kExitingObject) {
--depth;
}
else if (st_ == kError) {
return;
}

ParseNext();
}
while (depth > 0);
}

void LookaheadParser::SkipValue() {
SkipOut(0);
}

void LookaheadParser::SkipArray() {
SkipOut(1);
}

void LookaheadParser::SkipObject() {
SkipOut(1);
}

Value* LookaheadParser::PeekValue() {
if (st_ == kHasValue || st_ == kHasKey) {
if (st_ >= kHasNull && st_ <= kHasKey) {
return &v_;
}

return 0;
}

int LookaheadParser::PeekType() {
switch (st_) {
case kHasValue:
case kHasKey:
return v_.GetType();

case kEnteringArray:
return kArrayType;

case kEnteringObject:
return kObjectType;

case kExitingArray:
case kExitingObject:
case kError:
default:
return -1;
if (st_ >= kHasNull && st_ <= kHasKey) {
return v_.GetType();
}

if (st_ == kEnteringArray) {
return kArrayType;
}

if (st_ == kEnteringObject) {
return kObjectType;
}

return -1;
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -325,7 +326,7 @@ int main() {
cout << r.GetString() << " ";
}
else {
r.ExitArray();
r.SkipArray();
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,7 @@ class GenericObject {
GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
void RemoveAllMembers() { return value_.RemoveAllMembers(); }
void RemoveAllMembers() { value_.RemoveAllMembers(); }
bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool RemoveMember(const std::basic_string<Ch>& name) const { return value_.RemoveMember(name); }
Expand Down
Loading

0 comments on commit 1391e42

Please sign in to comment.