From d60a5090f959b20572a8f740c57281a9a20f7194 Mon Sep 17 00:00:00 2001 From: Joe DeCapo <679017+JrGoodle@users.noreply.github.com> Date: Tue, 2 May 2023 18:59:15 -0500 Subject: [PATCH] +rapidjson (#1804) * +rapidjson * don't mind me; i'm just weird about extra invocations --------- Co-authored-by: Jacob Heider --- projects/rapidjson.org/capitalize.cpp | 69 +++++++++++++++++++++++++++ projects/rapidjson.org/package.yml | 34 +++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 projects/rapidjson.org/capitalize.cpp create mode 100644 projects/rapidjson.org/package.yml diff --git a/projects/rapidjson.org/capitalize.cpp b/projects/rapidjson.org/capitalize.cpp new file mode 100644 index 00000000..8d65b4f9 --- /dev/null +++ b/projects/rapidjson.org/capitalize.cpp @@ -0,0 +1,69 @@ +// https://github.com/Tencent/rapidjson/blob/949c771b03de448bdedea80c44a4a5f65284bfeb/example/capitalize/capitalize.cpp#L4 + +// JSON condenser example + +// This example parses JSON from stdin with validation, +// and re-output the JSON content to stdout with all string capitalized, and without whitespace. + +#include "rapidjson/reader.h" +#include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" +#include +#include + +using namespace rapidjson; + +template +struct CapitalizeFilter { + CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {} + + bool Null() { return out_.Null(); } + bool Bool(bool b) { return out_.Bool(b); } + bool Int(int i) { return out_.Int(i); } + bool Uint(unsigned u) { return out_.Uint(u); } + bool Int64(int64_t i) { return out_.Int64(i); } + bool Uint64(uint64_t u) { return out_.Uint64(u); } + bool Double(double d) { return out_.Double(d); } + bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); } + bool String(const char* str, SizeType length, bool) { + buffer_.clear(); + for (SizeType i = 0; i < length; i++) + buffer_.push_back(static_cast(std::toupper(str[i]))); + return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string + } + bool StartObject() { return out_.StartObject(); } + bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); } + bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); } + bool StartArray() { return out_.StartArray(); } + bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); } + + OutputHandler& out_; + std::vector buffer_; + +private: + CapitalizeFilter(const CapitalizeFilter&); + CapitalizeFilter& operator=(const CapitalizeFilter&); +}; + +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)); + Writer writer(os); + + // JSON reader parse from the input stream and let writer generate the output. + CapitalizeFilter > filter(writer); + if (!reader.Parse(is, filter)) { + fprintf(stderr, "\nError(%u): %s\n", static_cast(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/projects/rapidjson.org/package.yml b/projects/rapidjson.org/package.yml new file mode 100644 index 00000000..43c9e14e --- /dev/null +++ b/projects/rapidjson.org/package.yml @@ -0,0 +1,34 @@ +distributable: + url: https://github.com/Tencent/rapidjson/archive/refs/tags/v{{version}}.tar.gz + strip-components: 1 + +versions: + github: Tencent/rapidjson/tags + strip: /^v/ + +build: + dependencies: + cmake.org: '*' + tea.xyz/gx/cc: c99 + tea.xyz/gx/make: '*' + doxygen.nl: 1 + script: | + cmake . $ARGS + sed -i.bak \ + -e "s/-march=native//" \ + -e "s/-Werror//" \ + CMakeLists.txt + rm CMakeLists.txt.bak + make . install + env: + ARGS: + - -DCMAKE_BUILD_TYPE=Release + - -DCMAKE_INSTALL_PREFIX={{prefix}} + - -DRAPIDJSON_BUILD_EXAMPLES=OFF + +test: + dependencies: + tea.xyz/gx/cc: c99 + script: | + c++ ./capitalize.cpp -o capitalize + test $(echo '{"a":"b"}' | ./capitalize) = '{"A":"B"}'