Skip to content

Commit

Permalink
Add minify method JsonMinifier API, make private reset and string vis…
Browse files Browse the repository at this point in the history
…itors methods
  • Loading branch information
st235 committed Jun 23, 2024
1 parent 9dd972d commit 5f35240
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
20 changes: 10 additions & 10 deletions include/json_beautifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ class JsonBeautifier: public JsonVisitor {
// Empty on purpose.
}

std::string beautify(JsonValue* json);

virtual void visitNull(const JsonNull* node) override;
virtual void visitBoolean(const JsonBoolean* node) override;
virtual void visitNumber(const JsonNumber* node) override;
virtual void visitString(const JsonString* node) override;
virtual void visitObject(const JsonObject* node) override;
virtual void visitArray(const JsonArray* node) override;

virtual ~JsonBeautifier() = default;
private:
void writeSpacing() {
for (uint16_t i = 0; i < _depth * _spacing; i++) {
_stream << _space_symbol;
}
}

void reset() {
_depth = 0;
_stream.str(std::string());
_stream.clear();
}

std::string beautify(JsonValue* json);

inline std::string beautifiedJson() const {
return _stream.str();
}

virtual ~JsonBeautifier() = default;
private:
void writeSpacing() {
for (uint16_t i = 0; i < _depth * _spacing; i++) {
_stream << _space_symbol;
}
}

uint16_t _depth;
uint8_t _spacing;
char _space_symbol;
Expand Down
8 changes: 6 additions & 2 deletions include/json_minifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@

namespace json {

class JsonValue;

class JsonMinifier: public JsonVisitor {
public:
JsonMinifier():
_stream() {
// Empty on purpose.
}

std::string minify(JsonValue* json);

virtual void visitNull(const JsonNull* node) override;
virtual void visitBoolean(const JsonBoolean* node) override;
virtual void visitNumber(const JsonNumber* node) override;
virtual void visitString(const JsonString* node) override;
virtual void visitObject(const JsonObject* node) override;
virtual void visitArray(const JsonArray* node) override;

virtual ~JsonMinifier() = default;
private:
void reset() {
_stream.str(std::string());
_stream.clear();
Expand All @@ -30,8 +36,6 @@ class JsonMinifier: public JsonVisitor {
return _stream.str();
}

virtual ~JsonMinifier() = default;
private:
std::stringstream _stream;
};

Expand Down
3 changes: 1 addition & 2 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ std::unique_ptr<JsonValue> FromJson(const std::string& json) {

std::string ToJson(JsonValue* json) {
JsonMinifier minifier;
json->accept(&minifier);
return minifier.minifiedJson();
return minifier.minify(json);
}

} // namespace json
7 changes: 7 additions & 0 deletions src/json_minifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

namespace json {

std::string JsonMinifier::minify(JsonValue* json) {
json->accept(this);
const auto& minified_json = minifiedJson();
reset();
return minified_json;
}

void JsonMinifier::visitNull(const JsonNull* node) {
_stream << "null";
}
Expand Down

0 comments on commit 5f35240

Please sign in to comment.