From f2d1a18a825cba5079df2c7ea6bf5a4002fa8b1e Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 5 Nov 2011 12:45:30 +0000 Subject: [PATCH] Core: JSON git-svn-id: svn://ultimatepp.org/upp/trunk@4130 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/JSON.h | 79 +++++++++++++++++++++++++++++- uppsrc/Core/src.tpp/JSON$en-us.tpp | 53 ++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 uppsrc/Core/src.tpp/JSON$en-us.tpp diff --git a/uppsrc/Core/JSON.h b/uppsrc/Core/JSON.h index 492784809..1cabf9e10 100644 --- a/uppsrc/Core/JSON.h +++ b/uppsrc/Core/JSON.h @@ -1,5 +1,82 @@ Value ParseJSON(CParser& p); Value ParseJSON(const char *s); -String AsJSON(const Value& v, const String& indent, bool pretty = false); +inline String AsJSON(int i) { return IsNull(i) ? "null" : AsString(i); } +inline String AsJSON(double n) { return IsNull(n) ? "null" : AsString(n); } +inline String AsJSON(bool b) { return b ? "true" : "false"; } +inline String AsJSON(const String& s) { return AsCString(s); } +inline String AsJSON(const WString& s) { return AsCString(s.ToString()); } +inline String AsJSON(const char *s) { return AsCString(s); } + +String AsJSON(const Value& v, const String& indent, bool pretty); String AsJSON(const Value& v, bool pretty = false); + +class JsonArray; + +class Json { + String text; + + Json& CatRaw(const char *key, const String& val) { + if(text.GetCount()) + text << ','; + text << '\"' << key << "\":" << val; + return *this; + } + +public: + String ToString() const { return "{" + text + "}"; } + String operator~() const { return ToString(); } + operator String() const { return ToString(); } + + Json& operator()(const char *key, const Value& value) { return CatRaw(key, AsJSON(value)); } + Json& operator()(const char *key, int i) { return CatRaw(key, AsJSON(i)); } + Json& operator()(const char *key, double n) { return CatRaw(key, AsJSON(n)); } + Json& operator()(const char *key, bool b) { return CatRaw(key, AsJSON(b)); } + Json& operator()(const char *key, const String& s) { return CatRaw(key, AsJSON(s)); } + Json& operator()(const char *key, const WString& s) { return CatRaw(key, AsJSON(s)); } + Json& operator()(const char *key, const char *s) { return CatRaw(key, AsJSON(s)); } + Json& operator()(const char *key, const Json& object) { return CatRaw(key, ~object); } + Json& operator()(const char *key, const JsonArray& array); + + Json() {} + Json(const char *key, const Value& value) { CatRaw(key, AsJSON(value)); } + Json(const char *key, int i) { CatRaw(key, AsJSON(i)); } + Json(const char *key, double n) { CatRaw(key, AsJSON(n)); } + Json(const char *key, bool b) { CatRaw(key, AsJSON(b)); } + Json(const char *key, const String& s) { CatRaw(key, AsJSON(s)); } + Json(const char *key, const WString& s) { CatRaw(key, AsJSON(s)); } + Json(const char *key, const char *s) { CatRaw(key, AsJSON(s)); } +}; + +class JsonArray { + String text; + + JsonArray& CatRaw(const String& val) { + if(text.GetCount()) + text << ','; + text << val; + return *this; + } + +public: + String ToString() const { return "[" + text + "]"; } + String operator~() const { return ToString(); } + operator String() const { return ToString(); } + + JsonArray& operator<<(const Value& value) { return CatRaw(AsJSON(value)); } + JsonArray& operator<<(int i) { return CatRaw(AsJSON(i)); } + JsonArray& operator<<(double n) { return CatRaw(AsJSON(n)); } + JsonArray& operator<<(bool b) { return CatRaw(AsJSON(b)); } + JsonArray& operator<<(const String& s) { return CatRaw(AsJSON(s)); } + JsonArray& operator<<(const WString& s) { return CatRaw(AsJSON(s)); } + JsonArray& operator<<(const char *s) { return CatRaw(AsJSON(s)); } + JsonArray& operator<<(const Json& object) { return CatRaw(~object); } + JsonArray& operator<<(const JsonArray& array) { return CatRaw(~array); } + + JsonArray() {} +}; + +inline Json& Json::operator()(const char *key, const JsonArray& array) +{ + return CatRaw(key, array); +} diff --git a/uppsrc/Core/src.tpp/JSON$en-us.tpp b/uppsrc/Core/src.tpp/JSON$en-us.tpp new file mode 100644 index 000000000..3f49273b5 --- /dev/null +++ b/uppsrc/Core/src.tpp/JSON$en-us.tpp @@ -0,0 +1,53 @@ +topic ""; +[2 $$0,0#00000000000000000000000000000000:Default] +[i448;a25;kKO9;2 $$1,0#37138531426314131252341829483380:class] +[l288;2 $$2,2#27521748481378242620020725143825:desc] +[0 $$3,0#96390100711032703541132217272105:end] +[H6;0 $$4,0#05600065144404261032431302351956:begin] +[i448;a25;kKO9;2 $$5,0#37138531426314131252341829483370:item] +[l288;a4;*@5;1 $$6,6#70004532496200323422659154056402:requirement] +[l288;i1121;b17;O9;~~~.1408;2 $$7,0#10431211400427159095818037425705:param] +[i448;b42;O9;2 $$8,8#61672508125594000341940100500538:tparam] +[b42;2 $$9,9#13035079074754324216151401829390:normal] +[{_} +[ {{10000@(113.42.0) [s0;%% [*@7;4 JSON support]]}}&] +[s3; &] +[s5;:ParseJSON`(CParser`&`): [_^Value^ Value]_[* ParseJSON]([_^CParser^ CParser][@(0.0.255) `& +]_[*@3 p])&] +[s2;%% Parses JSON represented from [%-*@3 p]. It is possible to parse +only part of whole text (e.g. when to parse just single element +of array `- parser the stops at the end of element. Elements +of JSON are parsed into corresponding Value types, JSON objects +are represented by ValueMap, JSON arrays by ValueArray.&] +[s3;%% &] +[s4; &] +[s5;:ParseJSON`(const char`*`): [_^Value^ Value]_[* ParseJSON]([@(0.0.255) const]_[@(0.0.255) c +har]_`*[*@3 s])&] +[s2;%% Parses JSON text [%-*@3 s]. Elements of JSON are parsed into +corresponding Value types, JSON objects are represented by ValueMap, +JSON arrays by ValueArray.&] +[s3;%% &] +[s4; &] +[s5;:AsJSON`(int`): [_^String^ String]_[* AsJSON]([@(0.0.255) int]_[*@3 i])&] +[s5;:AsJSON`(double`): [_^String^ String]_[* AsJSON]([@(0.0.255) double]_[*@3 n])&] +[s5;:AsJSON`(bool`): [_^String^ String]_[* AsJSON]([@(0.0.255) bool]_[*@3 b])&] +[s5;:AsJSON`(const String`&`): [_^String^ String]_[* AsJSON]([@(0.0.255) const]_[_^String^ St +ring][@(0.0.255) `&]_[*@3 s])&] +[s2;%% Converts basic values to JSON representation.&] +[s3;%% &] +[s4; &] +[s5;:AsJSON`(const Value`&`,const String`&`,bool`): [_^String^ String]_[* AsJSON]([@(0.0.255) c +onst]_[_^Value^ Value][@(0.0.255) `&]_[*@3 v], [@(0.0.255) const]_[_^String^ String][@(0.0.255) `& +]_[*@3 indent], [@(0.0.255) bool]_[*@3 pretty])&] +[s2;%% Encodes [%-*@3 v] as JSON, interpreting ValueMap as JSON object, +ValueArray as JSON array. [%-*@3 indent] is prepended to each line. +If [%-*@3 pretty] is true, JSON is encoded in lines, indenting +each level of embedding, if it is false, JSON is as compact as +possible..&] +[s3;%% &] +[s4; &] +[s5;:AsJSON`(const Value`&`,bool`): [_^String^ String]_[* AsJSON]([@(0.0.255) const]_[_^Value^ V +alue][@(0.0.255) `&]_[*@3 v], [@(0.0.255) bool]_[*@3 pretty]_`=_[@(0.0.255) false])&] +[s2;%% Same as AsJSON([%-*@3 v], String(), [%-*@3 pretty]).&] +[s3;%% &] +[s0;%% ] \ No newline at end of file