-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebc1d76
commit cf55e8f
Showing
3 changed files
with
117 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,33 @@ | ||
#include "JsonHandler.h" | ||
|
||
#include "rapidjson/reader.h" | ||
#include "rapidjson/stringbuffer.h" | ||
#include "rapidjson/document.h" | ||
#include "rapidjson/error/en.h" | ||
|
||
JsonHandler::JsonHandler(const ParseOptions& options) | ||
: m_parseOptions(options) | ||
{ | ||
} | ||
|
||
auto JsonHandler::GetCompressedJson(const std::string& jsonText)-> const Result | ||
JsonHandler::JsonHandler(const ParseOptions &options) | ||
: m_parseOptions(options) | ||
{ | ||
return ParseJson(jsonText); | ||
} | ||
|
||
auto JsonHandler::FormatJson(const std::string& jsonText, LE le, LF lf, char indentChar, unsigned indentLen)-> const Result | ||
auto JsonHandler::GetCompressedJson(const std::string &jsonText) -> const Result | ||
{ | ||
Result retVal{}; | ||
|
||
rapidjson::StringBuffer sb; | ||
rapidjson::PrettyWriter<rapidjson::StringBuffer> pw(sb); | ||
rapidjson::StringStream ss(jsonText.c_str()); | ||
rapidjson::Reader reader; | ||
|
||
pw.SetLineEnding(le); | ||
pw.SetFormatOptions(lf); | ||
pw.SetIndent(indentChar, indentLen); | ||
rapidjson::StringBuffer sb; | ||
rapidjson::Writer<rapidjson::StringBuffer> handler(sb); | ||
|
||
if (reader.Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseCommentsFlag | | ||
rapidjson::kParseEscapedApostropheFlag | rapidjson::kParseNanAndInfFlag | rapidjson::kParseTrailingCommasFlag>(ss, pw) | ||
&& sb.GetString()) | ||
{ | ||
retVal.success = true; | ||
retVal.response = sb.GetString(); | ||
retVal.error_str.clear(); | ||
retVal.error_code = retVal.error_pos = -1; | ||
} | ||
else | ||
{ | ||
retVal.success = false; | ||
retVal.response.clear(); | ||
retVal.error_str = rapidjson::GetParseError_En(reader.GetParseErrorCode()); | ||
retVal.error_pos = static_cast<int>(reader.GetErrorOffset()); | ||
retVal.error_code = reader.GetParseErrorCode(); | ||
} | ||
|
||
return retVal; | ||
return ParseJson(jsonText, sb, handler); | ||
} | ||
|
||
|
||
auto JsonHandler::ValidateJson(const std::string& jsonText)-> const Result | ||
auto JsonHandler::FormatJson(const std::string &jsonText, LE le, LF lf, char indentChar, unsigned indentLen) -> const Result | ||
{ | ||
return ParseJson(jsonText); | ||
rapidjson::StringBuffer sb; | ||
rapidjson::PrettyWriter<rapidjson::StringBuffer> handler(sb); | ||
handler.SetLineEnding(le); | ||
handler.SetFormatOptions(lf); | ||
handler.SetIndent(indentChar, indentLen); | ||
|
||
return ParseJson(jsonText, sb, handler); | ||
} | ||
|
||
auto JsonHandler::ParseJson(const std::string& jsonText) -> const Result | ||
auto JsonHandler::ValidateJson(const std::string &jsonText) -> const Result | ||
{ | ||
Result retVal{}; | ||
rapidjson::StringBuffer sb; | ||
rapidjson::Writer<rapidjson::StringBuffer> handler(sb); | ||
|
||
rapidjson::StringBuffer sb; | ||
rapidjson::Writer<rapidjson::StringBuffer> pw(sb); | ||
rapidjson::StringStream ss(jsonText.c_str()); | ||
rapidjson::Reader reader; | ||
|
||
if (reader.Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseCommentsFlag | | ||
rapidjson::kParseEscapedApostropheFlag | rapidjson::kParseNanAndInfFlag | rapidjson::kParseTrailingCommasFlag>(ss, pw) | ||
&& sb.GetString()) | ||
{ | ||
retVal.success = true; | ||
retVal.response = sb.GetString(); | ||
retVal.error_str.clear(); | ||
retVal.error_code = retVal.error_pos = -1; | ||
} | ||
else | ||
{ | ||
retVal.success = false; | ||
retVal.response.clear(); | ||
retVal.error_str = rapidjson::GetParseError_En(reader.GetParseErrorCode()); | ||
retVal.error_pos = static_cast<int>(reader.GetErrorOffset()); | ||
retVal.error_code = reader.GetParseErrorCode(); | ||
} | ||
|
||
return retVal; | ||
return ParseJson(jsonText, sb, handler); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,92 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include "rapidjson/prettywriter.h" | ||
|
||
#include <rapidjson/reader.h> | ||
#include <rapidjson/writer.h> | ||
#include <rapidjson/prettywriter.h> | ||
#include <rapidjson/stringbuffer.h> | ||
#include <rapidjson/error/en.h> | ||
#include "Define.h" | ||
|
||
struct Result | ||
{ | ||
bool success = false; | ||
int error_pos = -1; | ||
int error_code = -1; | ||
std::string error_str; | ||
std::string response; | ||
bool success = false; | ||
int error_pos = -1; | ||
int error_code = -1; | ||
std::string error_str; | ||
std::string response; | ||
}; | ||
|
||
using LE = rapidjson::LineEndingOption; | ||
using LF = rapidjson::PrettyFormatOptions; | ||
|
||
class JsonHandler | ||
{ | ||
ParseOptions m_parseOptions{}; | ||
ParseOptions m_parseOptions {}; | ||
|
||
public: | ||
JsonHandler(const ParseOptions& options); | ||
~JsonHandler() = default; | ||
JsonHandler(const ParseOptions &options); | ||
~JsonHandler() = default; | ||
|
||
auto GetCompressedJson(const std::string& jsonText)->const Result; | ||
auto FormatJson(const std::string& jsonText, LE le, LF lf, char indentChar, unsigned indentLen)->const Result; | ||
auto ValidateJson(const std::string& jsonText)->const Result; | ||
auto GetCompressedJson(const std::string &jsonText) -> const Result; | ||
auto FormatJson(const std::string &jsonText, LE le, LF lf, char indentChar, unsigned indentLen) -> const Result; | ||
auto ValidateJson(const std::string &jsonText) -> const Result; | ||
|
||
private: | ||
auto ParseJson(const std::string& jsonText)->const Result; | ||
template <typename Handler> | ||
auto ParseJson(const std::string &jsonText, rapidjson::StringBuffer &sb, Handler &handler) -> const Result; | ||
}; | ||
|
||
template <typename Handler> | ||
inline auto JsonHandler::ParseJson(const std::string &jsonText, rapidjson::StringBuffer &sb, Handler &handler) -> const Result | ||
{ | ||
Result retVal {}; | ||
|
||
bool success = false; | ||
rapidjson::Reader reader; | ||
rapidjson::StringStream ss(jsonText.c_str()); | ||
|
||
// TODO: Find some better way | ||
constexpr auto flgBase = rapidjson::kParseFullPrecisionFlag | rapidjson::kParseEscapedApostropheFlag | rapidjson::kParseNanAndInfFlag; | ||
constexpr auto flgBase_commemt = flgBase | rapidjson::kParseCommentsFlag; | ||
constexpr auto flgBase_comma = flgBase | rapidjson::kParseTrailingCommasFlag; | ||
constexpr auto flgBase_Both = flgBase_comma | flgBase_commemt; | ||
|
||
if (m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTraillingComma) | ||
{ | ||
success = reader.Parse<flgBase_Both>(ss, handler) && sb.GetString(); | ||
} | ||
|
||
else if (!m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTraillingComma) | ||
{ | ||
success = reader.Parse<flgBase_comma>(ss, handler) && sb.GetString(); | ||
} | ||
|
||
else if (m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTraillingComma) | ||
{ | ||
success = reader.Parse<flgBase_commemt>(ss, handler) && sb.GetString(); | ||
} | ||
|
||
else if (!m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTraillingComma) | ||
{ | ||
success = reader.Parse<flgBase>(ss, handler) && sb.GetString(); | ||
} | ||
|
||
if (success) | ||
{ | ||
retVal.success = true; | ||
retVal.response = sb.GetString(); | ||
retVal.error_code = retVal.error_pos = -1; | ||
retVal.error_str.clear(); | ||
} | ||
else | ||
{ | ||
retVal.success = false; | ||
retVal.error_str = rapidjson::GetParseError_En(reader.GetParseErrorCode()); | ||
retVal.error_pos = static_cast<int>(reader.GetErrorOffset()); | ||
retVal.error_code = reader.GetParseErrorCode(); | ||
retVal.response.clear(); | ||
} | ||
|
||
return retVal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters