forked from NPP-JSONViewer/JSON-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonHandler.h
More file actions
116 lines (94 loc) · 4.37 KB
/
JsonHandler.h
File metadata and controls
116 lines (94 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once
#include <string>
#include <rapidjson/reader.h>
#include <rapidjson/writer.h>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/error/en.h>
#include "Define.h"
#include "TrackingStream.h"
namespace rj = rapidjson;
struct Result
{
bool success = false;
int error_pos = -1;
int error_code = -1;
std::string error_str;
std::string response;
};
using LE = rj::LineEndingOption;
using LF = rj::PrettyFormatOptions;
// Parse flags for all JSON operations. Historically flgBaseWriter used kParseFullPrecisionFlag
// instead of kParseNumbersAsStringsFlag because upstream RapidJSON's RawNumber() would quote
// numbers as strings. Our fork fixes that (RawNumber now writes via WriteRawValue without quotes),
// so both flag sets are now identical. They are kept separate to allow independent tuning of the
// DOM-based path (flgBaseReader: sort-by-key) vs the SAX streaming path (flgBaseWriter: format/compress)
// if needed in the future.
constexpr auto flgBaseReader = rj::kParseEscapedApostropheFlag | rj::kParseNanAndInfFlag | rj::kParseNumbersAsStringsFlag;
constexpr auto flgBaseWriter = rj::kParseEscapedApostropheFlag | rj::kParseNanAndInfFlag | rj::kParseNumbersAsStringsFlag;
class JsonHandler
{
ParseOptions m_parseOptions {};
public:
explicit 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 SortJsonByKey(const std::string& jsonText, LE le, LF lf, char indentChar, unsigned indentLen) -> const Result;
auto ValidateJson(const std::string& jsonText) -> const Result;
template <unsigned format, typename Handler>
auto ParseJson(const std::string& jsonText, rj::StringBuffer& sb, Handler& handler, TrackingStreamSharedPtr pTS = nullptr) -> const Result;
private:
void SortJsonObject(rj::Value& jsonObject, rj::Document::AllocatorType& allocator) const;
void SortJsonRecursively(rj::Value& jsonValue, rj::Document::AllocatorType& allocator) const;
auto SortJsonText(const std::string& jsonString) const -> std::string;
};
template <unsigned flgBase, typename Handler>
inline auto JsonHandler::ParseJson(const std::string& jsonText, rj::StringBuffer& sb, Handler& handler, TrackingStreamSharedPtr pTS) -> const Result
{
Result retVal {};
bool success = false;
rj::Reader reader;
std::shared_ptr<rj::StringStream> pSS = nullptr;
if (!pTS)
{
pSS = std::make_shared<rj::StringStream>(jsonText.c_str());
}
// TODO: Find some better way
constexpr auto flgBase_comment = flgBase | rj::kParseCommentsFlag;
constexpr auto flgBase_comma = flgBase | rj::kParseTrailingCommasFlag;
constexpr auto flgBase_Both = flgBase_comma | flgBase_comment;
if (m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTrailingComma)
{
success = pTS ? reader.Parse<flgBase_Both>(*pTS, handler) && sb.GetString() : reader.Parse<flgBase_Both>(*pSS, handler) && sb.GetString();
}
else if (!m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTrailingComma)
{
success = pTS ? reader.Parse<flgBase_comma>(*pTS, handler) && sb.GetString() : reader.Parse<flgBase_comma>(*pSS, handler) && sb.GetString();
}
else if (m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTrailingComma)
{
success = pTS ? reader.Parse<flgBase_comment>(*pTS, handler) && sb.GetString() : reader.Parse<flgBase_comment>(*pSS, handler) && sb.GetString();
}
else if (!m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTrailingComma)
{
success = pTS ? reader.Parse<flgBase>(*pTS, handler) && sb.GetString() : reader.Parse<flgBase>(*pSS, 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 = rj::GetParseError_En(reader.GetParseErrorCode());
retVal.error_pos = static_cast<int>(reader.GetErrorOffset());
retVal.error_code = reader.GetParseErrorCode();
retVal.response.clear();
}
return retVal;
}