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
19 changes: 19 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.10)
project(test)

set(CMAKE_CXX_STANDARD 11)
set(PROJECT_WTD "${CMAKE_SOURCE_DIR}/..")
set(EXAMPLE_PATH "${PROJECT_WTD}/example")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0")

include_directories(${EXAMPLE_PATH}/include)
include_directories(${PROJECT_WTD}/include)
include_directories(${PROJECT_WTD}/include/rapidjson)

file(GLOB test-src ${CMAKE_SOURCE_DIR}/*.cpp)
set(test-src ${test-src} "${PROJECT_WTD}/include/slothjson.cpp")
foreach(test-src-item ${test-src})
message(STATUS ${test-src-item})
endforeach()

add_executable(test ${test-src})
18 changes: 18 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Run ##
```shell
cd build
./autogen
./test
```

## Notice ##

cmake needed, and the version number should not less than 3.10.

Check your cmake version with the command:

```shell
cmake --version
```

if the version number of cmake less than 3.10, just modify the version number at [CMakeLists.txt](CMakeLists.txt).
3 changes: 3 additions & 0 deletions example/build/autogen
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -rf *.txt *.cmake CMakeFiles Makefile
cmake -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON ..
make
24 changes: 24 additions & 0 deletions example/conf/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"structs":
[
{
"type" : "json_t",
"members" :
[
["bool", "error", "true"],
["string", "message", "404 not found"],
["int16_t", "code", "0"],
["string", "nonce", "0.6268985954069382"],
["int64_t", "timestamp", "1532269760476"]
]
},
{
"type" : "root_t",
"members" :
[
["string", "json"],
["string", "signature"]
]
}
]
}
84 changes: 84 additions & 0 deletions example/include/sample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
////////////////////////////////////////////////////////////////////////////////
// NOTE : Generated by slothjson. It is NOT supposed to modify this file.
////////////////////////////////////////////////////////////////////////////////
#ifndef __sample_20180724103727_h__
#define __sample_20180724103727_h__

#include "slothjson.h"

namespace slothjson {

struct json_t
{
bool error;
std::string message;
int16_t code;
std::string nonce;
int64_t timestamp;

void skip_error() { __skip_error = true; }
void skip_message() { __skip_message = true; }
void skip_code() { __skip_code = true; }
void skip_nonce() { __skip_nonce = true; }
void skip_timestamp() { __skip_timestamp = true; }

bool json_has_error() const { return __json_has_error; }
bool json_has_message() const { return __json_has_message; }
bool json_has_code() const { return __json_has_code; }
bool json_has_nonce() const { return __json_has_nonce; }
bool json_has_timestamp() const { return __json_has_timestamp; }

json_t();
json_t& operator=(const json_t& obj_val);
bool operator==(const json_t& obj_val) const;
bool encode(allocator_t& alloc, rapidjson::Value& json_val) const;
bool decode(const rapidjson::Value& json_val);

private:
bool __skip_error;
bool __skip_message;
bool __skip_code;
bool __skip_nonce;
bool __skip_timestamp;

bool __json_has_error;
bool __json_has_message;
bool __json_has_code;
bool __json_has_nonce;
bool __json_has_timestamp;
};

bool encode(const json_t& obj_val, allocator_t& alloc, rapidjson::Value& json_val);
bool decode(const rapidjson::Value& json_val, json_t& obj_val);

struct root_t
{
std::string json;
std::string signature;

void skip_json() { __skip_json = true; }
void skip_signature() { __skip_signature = true; }

bool json_has_json() const { return __json_has_json; }
bool json_has_signature() const { return __json_has_signature; }

root_t();
root_t& operator=(const root_t& obj_val);
bool operator==(const root_t& obj_val) const;
bool encode(allocator_t& alloc, rapidjson::Value& json_val) const;
bool decode(const rapidjson::Value& json_val);

private:
bool __skip_json;
bool __skip_signature;

bool __json_has_json;
bool __json_has_signature;
};

bool encode(const root_t& obj_val, allocator_t& alloc, rapidjson::Value& json_val);
bool decode(const rapidjson::Value& json_val, root_t& obj_val);

} // namespace slothjson

#endif // __sample_20180724103727_h__
154 changes: 154 additions & 0 deletions example/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
////////////////////////////////////////////////////////////////////////////////
// NOTE : Generated by slothjson. It is NOT supposed to modify this file.
////////////////////////////////////////////////////////////////////////////////
#include "sample.h"

namespace slothjson {

json_t::json_t()
{
error = true;
__skip_error = false;
__json_has_error = false;

message = "404 not found";
__skip_message = false;
__json_has_message = false;

code = 0;
__skip_code = false;
__json_has_code = false;

nonce = "0.6268985954069382";
__skip_nonce = false;
__json_has_nonce = false;

timestamp = 1532269760476;
__skip_timestamp = false;
__json_has_timestamp = false;
}

json_t& json_t::operator=(const json_t& obj_val)
{
this->error = obj_val.error;
this->message = obj_val.message;
this->code = obj_val.code;
this->nonce = obj_val.nonce;
this->timestamp = obj_val.timestamp;
return *this;
}

bool json_t::operator==(const json_t& obj_val) const
{
if (!(this->error == obj_val.error)) return false;
if (!(this->message == obj_val.message)) return false;
if (!(this->code == obj_val.code)) return false;
if (!(this->nonce == obj_val.nonce)) return false;
if (!(this->timestamp == obj_val.timestamp)) return false;
return true;
}

bool json_t::encode(allocator_t& alloc, rapidjson::Value& json_val) const
{
do
{
json_val.SetObject();
if (!__skip_error && !encode_field(error, "error", alloc, json_val)) break;
if (!__skip_message && !encode_field(message, "message", alloc, json_val)) break;
if (!__skip_code && !encode_field(code, "code", alloc, json_val)) break;
if (!__skip_nonce && !encode_field(nonce, "nonce", alloc, json_val)) break;
if (!__skip_timestamp && !encode_field(timestamp, "timestamp", alloc, json_val)) break;

return true;
} while (0);

return false;
}

bool json_t::decode(const rapidjson::Value& json_val)
{
do
{
if (!decode_field(json_val, "error", error, __json_has_error)) break;
if (!decode_field(json_val, "message", message, __json_has_message)) break;
if (!decode_field(json_val, "code", code, __json_has_code)) break;
if (!decode_field(json_val, "nonce", nonce, __json_has_nonce)) break;
if (!decode_field(json_val, "timestamp", timestamp, __json_has_timestamp)) break;

return true;
} while (0);

return false;
}

bool encode(const json_t& obj_val, allocator_t& alloc, rapidjson::Value& json_val)
{
return obj_val.encode(alloc, json_val);
}

bool decode(const rapidjson::Value& json_val, json_t& obj_val)
{
return obj_val.decode(json_val);
}

root_t::root_t()
{
__skip_json = false;
__json_has_json = false;

__skip_signature = false;
__json_has_signature = false;
}

root_t& root_t::operator=(const root_t& obj_val)
{
this->json = obj_val.json;
this->signature = obj_val.signature;
return *this;
}

bool root_t::operator==(const root_t& obj_val) const
{
if (!(this->json == obj_val.json)) return false;
if (!(this->signature == obj_val.signature)) return false;
return true;
}

bool root_t::encode(allocator_t& alloc, rapidjson::Value& json_val) const
{
do
{
json_val.SetObject();
if (!__skip_json && !encode_field(json, "json", alloc, json_val)) break;
if (!__skip_signature && !encode_field(signature, "signature", alloc, json_val)) break;

return true;
} while (0);

return false;
}

bool root_t::decode(const rapidjson::Value& json_val)
{
do
{
if (!decode_field(json_val, "json", json, __json_has_json)) break;
if (!decode_field(json_val, "signature", signature, __json_has_signature)) break;

return true;
} while (0);

return false;
}

bool encode(const root_t& obj_val, allocator_t& alloc, rapidjson::Value& json_val)
{
return obj_val.encode(alloc, json_val);
}

bool decode(const rapidjson::Value& json_val, root_t& obj_val)
{
return obj_val.decode(json_val);
}

} // namespace slothjson
53 changes: 53 additions & 0 deletions example/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by kang on 2018/7/22.
//
#include "sample.h"
#include <iostream>

using namespace std;

int main(int argc, char **argv){
string jIn = R"({
"json":"{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\":1532525070907}",
"signature":"Q5s1tDJVXWcNXp"
})";
slothjson::root_t rootObj;
slothjson::root_t in;
slothjson::json_t jsonObj;
string jOut;
bool ret;

ret = slothjson::decode(jIn, rootObj);
if (!ret){
cout <<__func__ <<":"<<__LINE__<<", decode() failed!"<<endl;
return -1;
}

ret = slothjson::decode(rootObj.json, jsonObj);
if (!ret){
cout <<__func__ <<":"<<__LINE__<<", decode() failed!"<<endl;
return -1;
}

ret = slothjson::encode<false>(jsonObj, in.json);
if (!ret){
cout <<__func__ <<":"<<__LINE__<<", encode() failed!"<<endl;
return -1;
}

ret = slothjson::encode<false>(rootObj.signature, in.signature);
if (!ret){
cout <<__func__ <<":"<<__LINE__<<", encode() failed!"<<endl;
return -1;
}

ret = slothjson::encode<false>(in, jOut);
if (!ret){
cout <<__func__ <<":"<<__LINE__<<", encode() failed!"<<endl;
return -1;
}

cout <<jOut <<endl;

return 0;
}