diff options
author | Rudi Farkas <rudi.farkas@gmail.com> | 2013-11-03 20:33:33 +0400 |
---|---|---|
committer | Rudi Farkas <rudi.farkas@gmail.com> | 2013-11-03 20:33:33 +0400 |
commit | bcc51eee96c8354bd41944105525d7b6d988d910 (patch) | |
tree | 0bcf13844df5c79731e1bac36838e21261317002 /src | |
parent | ad821678e314ddf37f5fe81f57b85fc16fb25b12 (diff) |
Added the Prettyprint extension:
added arg to public JSONValue::Stringify(bool const prettyprint =
false),
moved code from old public JSONValue::Stringify()
to new private StringifyImpl(size_t const indentDepth) and added pretty
printing code;
main(): added option -ex3 / example3() to demo compact vs. pretty
printed JSON string;
main(): added option -p to apply pretty printing to JSON from stdin.
Diffstat (limited to 'src')
-rw-r--r-- | src/JSONValue.cpp | 58 | ||||
-rwxr-xr-x | src/JSONValue.h | 6 | ||||
-rw-r--r-- | src/demo/example.cpp | 35 | ||||
-rw-r--r-- | src/demo/functions.h | 1 | ||||
-rw-r--r-- | src/demo/nix-main.cpp | 21 |
5 files changed, 102 insertions, 19 deletions
diff --git a/src/JSONValue.cpp b/src/JSONValue.cpp index 3e6f8a0..fd4d947 100644 --- a/src/JSONValue.cpp +++ b/src/JSONValue.cpp @@ -660,12 +660,33 @@ JSONValue* JSONValue::Child(const wchar_t* name) * * @access public * + * @param bool prettyprint Enable prettyprint + * * @return std::wstring Returns the JSON string */ -std::wstring JSONValue::Stringify() const +std::wstring JSONValue::Stringify(bool const prettyprint) const +{ + size_t const indentDepth = prettyprint ? 1 : 0; + return StringifyImpl(indentDepth); +} + + +/** + * Creates a JSON encoded string for the value with all necessary characters escaped + * + * @access private + * + * @param size_t indentDepth The prettyprint indentation depth (0 : no prettyprint) + * + * @return std::wstring Returns the JSON string + */ +std::wstring JSONValue::StringifyImpl(size_t const indentDepth) const { std::wstring ret_string; - + size_t const indentDepth1 = indentDepth ? indentDepth + 1 : 0; + std::wstring const indentStr = Indent(indentDepth); + std::wstring const indentStr1 = Indent(indentDepth1); + switch (type) { case JSONType_Null: @@ -696,35 +717,35 @@ std::wstring JSONValue::Stringify() const case JSONType_Array: { - ret_string = L"["; + ret_string = indentDepth ? L"[\n" + indentStr1 : L"["; JSONArray::const_iterator iter = array_value.begin(); while (iter != array_value.end()) { - ret_string += (*iter)->Stringify(); - + ret_string += (*iter)->StringifyImpl(indentDepth1); + // Not at the end - add a separator if (++iter != array_value.end()) ret_string += L","; } - ret_string += L"]"; + ret_string += indentDepth ? L"\n" + indentStr + L"]" : L"]"; break; } case JSONType_Object: { - ret_string = L"{"; + ret_string = indentDepth ? L"{\n" + indentStr1 : L"{"; JSONObject::const_iterator iter = object_value.begin(); while (iter != object_value.end()) { ret_string += StringifyString((*iter).first); ret_string += L":"; - ret_string += (*iter).second->Stringify(); - + ret_string += (*iter).second->StringifyImpl(indentDepth1); + // Not at the end - add a separator if (++iter != object_value.end()) ret_string += L","; } - ret_string += L"}"; + ret_string += indentDepth ? L"\n" + indentStr + L"}" : L"}"; break; } } @@ -801,3 +822,20 @@ std::wstring JSONValue::StringifyString(const std::wstring &str) str_out += L"\""; return str_out; } + +/** +* Creates the indentation string for the depth given +* +* @access private +* +* @param size_t indent The prettyprint indentation depth (0 : no indentation) +* +* @return std::wstring Returns the string +*/ +std::wstring JSONValue::Indent(size_t depth) +{ + const size_t indent_step = 2; + depth ? --depth : 0; + std::wstring indentStr(depth * indent_step, ' '); + return indentStr; +} diff --git a/src/JSONValue.h b/src/JSONValue.h index f84389f..e679354 100755 --- a/src/JSONValue.h +++ b/src/JSONValue.h @@ -67,14 +67,16 @@ class JSONValue bool HasChild(const wchar_t* name) const; JSONValue *Child(const wchar_t* name); - std::wstring Stringify() const; + std::wstring Stringify(bool const prettyprint = false) const; protected: static JSONValue *Parse(const wchar_t **data); private: static std::wstring StringifyString(const std::wstring &str); - + std::wstring StringifyImpl(size_t const indentDepth) const; + static std::wstring Indent(size_t depth); + JSONType type; std::wstring string_value; bool bool_value; diff --git a/src/demo/example.cpp b/src/demo/example.cpp index 72d5b59..bfdcbc0 100644 --- a/src/demo/example.cpp +++ b/src/demo/example.cpp @@ -140,3 +140,38 @@ void example2() // Clean up delete value; } + +// Example 3 : compact vs. prettyprint +void example3() +{ + const wchar_t* EXAMPLE3 = + L"{\ + \"SelectedTab\":\"Math\",\ + \"Widgets\":[\ + {\"WidgetPosition\":[0,369,800,582],\"WidgetIndex\":1,\"WidgetType\":\"WidgetCheckbox.1\"},\ + {\"WidgetPosition\":[235,453,283,489],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F2.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":2,\"WidgetType\":\"WidgetCheckbox.1\"},\ + {\"WidgetPosition\":[235,494,283,530],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F3.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":3,\"WidgetType\":\"WidgetCheckbox.1\"},\ + {\"WidgetPosition\":[235,536,283,572],\"IsWidgetVisible\":-1,\"Caption\":\"On\",\"EnableCaption\":-1,\"Name\":\"F4.View\",\"CaptionPosition\":2,\"ControlWidth\":25,\"ControlHeight\":36,\"Font\":0,\"Value\":\"Off\",\"WidgetIndex\":4,\"WidgetType\":\"WidgetCheckbox.1\"},\ + {\"WidgetPosition\":[287,417,400,439],\"IsWidgetVisible\":-1,\"Caption\":\"\",\"EnableCaption\":0,\"Name\":\"F1.Equation\",\"CaptionPosition\":1,\"ControlWidth\":113,\"ControlHeight\":22,\"Font\":0,\"AlignText\":0,\"EnableBorder\":0,\"CaptionOnly\":0,\"Value\":\"FFT(C1)\",\"WidgetIndex\":9,\"WidgetType\":\"WidgetStaticText.1\"},\ + {\"WidgetPosition\":[191,409,230,445],\"IsWidgetVisible\":0,\"Caption\":\"F1\",\"EnableCaption\":0,\"Name\":\"F1.MeasureOpGui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Measurement To Graph\",\"Value\":\"Amplitude\",\"WidgetIndex\":17,\"WidgetType\":\"WidgetProcessorCombobox.1\"},\ + {\"WidgetPosition\":[191,409,230,445],\"IsWidgetVisible\":-1,\"Caption\":\"F1\",\"EnableCaption\":0,\"Name\":\"F1.Operator1gui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Math Operator\",\"Value\":\"FFT\",\"WidgetIndex\":25,\"WidgetType\":\"WidgetProcessorCombobox.1\"},\ + {\"WidgetPosition\":[191,452,230,487],\"IsWidgetVisible\":-1,\"Caption\":\"F2\",\"EnableCaption\":0,\"Name\":\"F2.Operator1gui\",\"CaptionPosition\":1,\"ControlWidth\":39,\"ControlHeight\":36,\"Font\":0,\"ButtonOnly\":-1,\"PickerTitle\":\"Select Math Operator\",\"Value\":\"Zoom\",\"WidgetIndex\":26,\"WidgetType\":\"WidgetProcessorCombobox.1\"}\ + ]\ + }"; + + // Parse example data + JSONValue *value = JSON::Parse(EXAMPLE3); + if (value) + { + print_out(L"-----------\r\n"); + print_out(value->Stringify().c_str()); + print_out(L"\r\n"); + print_out(L"-----------\r\n"); + print_out(value->Stringify(true).c_str()); + print_out(L"\r\n"); + print_out(L"-----------\r\n"); + } + + // Clean up + delete value; +} diff --git a/src/demo/functions.h b/src/demo/functions.h index 82951c1..e16ca13 100644 --- a/src/demo/functions.h +++ b/src/demo/functions.h @@ -6,6 +6,7 @@ void print_out(const wchar_t *output); // Example functions void example1(); void example2(); +void example3(); // Test case runner void run_tests(); diff --git a/src/demo/nix-main.cpp b/src/demo/nix-main.cpp index fda1b46..596e094 100644 --- a/src/demo/nix-main.cpp +++ b/src/demo/nix-main.cpp @@ -67,7 +67,7 @@ int main(int argc, char **argv) } // Parse + echo? - else if (mode == "-e") + else if (mode == "-e" || mode == "-p") { // Get the stdin data cin >> noskipws; @@ -80,24 +80,29 @@ int main(int argc, char **argv) if ((value = JSON::Parse(results.c_str())) == NULL) wcout << L"Code entered is *NOT* valid."; else - wcout << value->Stringify(); + { + bool const prettyprint = (mode == "-p"); + wcout << value->Stringify(prettyprint); + } wcout << endl; if (value) delete value; } - - // Example 1? + + // Example ? else if (mode == "-ex1") { example1(); } - - // Example 2? else if (mode == "-ex2") { example2(); } - + else if (mode == "-ex3") + { + example3(); + } + // Test cases? else if (mode == "-t") { @@ -112,8 +117,10 @@ int main(int argc, char **argv) wcout << L"\t-v\tVerify JSON string is *valid* via stdin" << endl; wcout << L"\t-f\tVerify JSON string is *invalid* via stdin" << endl; wcout << L"\t-e\tVerify JSON string via stdin and echo it back using Stringify()" << endl; + wcout << L"\t-p\tVerify JSON string via stdin and prettyprint it using Stringify(true)" << endl; wcout << L"\t-ex1\tRun example 1 - Example of how to extract data from the JSONValue object" << endl; wcout << L"\t-ex2\tRun example 2 - Building a JSONValue from nothing" << endl; + wcout << L"\t-ex3\tRun example 3 - Compact vs. prettyprint" << endl; wcout << L"\t-t\tRun test cases" << endl; wcout << endl; wcout << L"Only one option can be used at a time." << endl; |