From 9f1dcef6b1645a3c3bf34a2c0a85f75e3b3e61dd Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 9 Feb 2016 00:09:39 +0800 Subject: [PATCH 1/4] Add jsonx example --- example/CMakeLists.txt | 1 + example/jsonx/jsonx.cpp | 199 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 example/jsonx/jsonx.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index cfb55ddc3..f4829cc83 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 2.8) set(EXAMPLES capitalize condense + jsonx messagereader pretty prettyauto diff --git a/example/jsonx/jsonx.cpp b/example/jsonx/jsonx.cpp new file mode 100644 index 000000000..b6f301026 --- /dev/null +++ b/example/jsonx/jsonx.cpp @@ -0,0 +1,199 @@ +// JSON to JSONx conversion exmaple, using SAX API. +// JSONx is an IBM standard format to represent JSON as XML. +// https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html +// This example parses JSON text from stdin with validation, +// and convert to JSONx format to stdout. + +#include "rapidjson/reader.h" +#include "rapidjson/stringbuffer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" +#include + +using namespace rapidjson; + +// For simplicity, this example only read/write in UTF-8 encoding +template +class JsonxWriter { +public: + JsonxWriter(OutputStream& os) : os_(os), level_(0), hasName_(false) { + } + + bool Null() { + return WriteStartElement("null", true); + } + + bool Bool(bool b) { + return + WriteStartElement("boolean") && + WriteString(b ? "true" : "false") && + WriteEndElement("boolean"); + } + + bool Int(int i) { + char buffer[12]; + return WriteNumberElement(buffer, sprintf(buffer, "%d", i)); + } + + bool Uint(unsigned i) { + char buffer[11]; + return WriteNumberElement(buffer, sprintf(buffer, "%u", i)); + } + + bool Int64(int64_t i) { + char buffer[21]; + return WriteNumberElement(buffer, sprintf(buffer, "%" PRId64, i)); + } + + bool Uint64(uint64_t i) { + char buffer[21]; + return WriteNumberElement(buffer, sprintf(buffer, "%" PRIu64, i)); + } + + bool Double(double d) { + char buffer[30]; + return WriteNumberElement(buffer, sprintf(buffer, "%.17g", d)); + } + + bool String(const char* str, SizeType length, bool) { + return + WriteStartElement("string") && + WriteEscapedText(str, length) && + WriteEndElement("string"); + } + + bool StartObject() { + return WriteStartElement("object"); + } + + bool Key(const char* str, SizeType length, bool) { + // backup key to name_ + name_.Clear(); + for (SizeType i = 0; i < length; i++) + name_.Put(str[i]); + hasName_ = true; + return true; + } + + bool EndObject(SizeType) { + return WriteEndElement("object"); + } + + bool StartArray() { + return WriteStartElement("array"); + } + + bool EndArray(SizeType) { + return WriteEndElement("array"); + } + +private: + bool WriteString(const char* s) { + while (*s) + os_.Put(*s++); + return true; + } + + bool WriteEscapedAttributeValue(const char* s, size_t length) { + for (size_t i = 0; i < length; i++) { + switch (s[i]) { + case '&': WriteString("&"); break; + case '<': WriteString("<"); break; + case '"': WriteString("""); break; + default: os_.Put(s[i]); break; + } + } + return true; + } + + bool WriteEscapedText(const char* s, size_t length) { + for (size_t i = 0; i < length; i++) { + switch (s[i]) { + case '&': WriteString("&"); break; + case '<': WriteString("<"); break; + default: os_.Put(s[i]); break; + } + } + return true; + } + + bool WriteStartElement(const char* type, bool emptyElement = false) { + if (level_ == 0) + if (!WriteString("")) + return false; + + if (!WriteString(""); + else { + level_++; + return WriteString(">"); + } + } + + bool WriteEndElement(const char* type) { + if (!WriteString("")) + return false; + + // For the last end tag, flush the output stream. + if (--level_ == 0) + os_.Flush(); + + return true; + } + + bool WriteNumberElement(const char* buffer, int length) { + if (!WriteStartElement("number")) + return false; + for (int j = 0; j < length; j++) + os_.Put(buffer[j]); + return WriteEndElement("number"); + } + + OutputStream& os_; + StringBuffer name_; + unsigned level_; + bool hasName_; +}; + +int main(int, char*[]) { + // Prepare JSON reader and input stream. + Reader reader; + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare JSON writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + JsonxWriter writer(os); + + // JSON reader parse from the input stream and let writer generate the output. + if (!reader.Parse(is, writer)) { + fprintf(stderr, "\nError(%u): %s\n", static_cast(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} From c53a836a3c972dda987371127f0d7bbd4d14995a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 9 Feb 2016 00:53:49 +0800 Subject: [PATCH 2/4] Try fixing 64-bit printf macro --- example/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f4829cc83..c6b8449ff 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -19,6 +19,8 @@ set(EXAMPLES include_directories("../include/") +add_definitions(-D__STDC_FORMAT_MACROS) + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") From b5d939b71ba7e77abe92bca4ac12d9eb2996a249 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 9 Feb 2016 01:09:43 +0800 Subject: [PATCH 3/4] Fix a gcc warning --- example/jsonx/jsonx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/jsonx/jsonx.cpp b/example/jsonx/jsonx.cpp index b6f301026..2fb6c9f2f 100644 --- a/example/jsonx/jsonx.cpp +++ b/example/jsonx/jsonx.cpp @@ -17,7 +17,7 @@ using namespace rapidjson; template class JsonxWriter { public: - JsonxWriter(OutputStream& os) : os_(os), level_(0), hasName_(false) { + JsonxWriter(OutputStream& os) : os_(os), name_(), level_(0), hasName_(false) { } bool Null() { From 5af344b9d988132b23572ab997fcbf0c3229ae13 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 9 Feb 2016 01:27:22 +0800 Subject: [PATCH 4/4] Add comment for -D__STDC_FORMAT_MACROS --- example/jsonx/jsonx.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/example/jsonx/jsonx.cpp b/example/jsonx/jsonx.cpp index 2fb6c9f2f..c253ac096 100644 --- a/example/jsonx/jsonx.cpp +++ b/example/jsonx/jsonx.cpp @@ -3,6 +3,7 @@ // https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html // This example parses JSON text from stdin with validation, // and convert to JSONx format to stdout. +// Need compile with -D__STDC_FORMAT_MACROS for defining PRId64 and PRIu64 macros. #include "rapidjson/reader.h" #include "rapidjson/stringbuffer.h"