diff --git a/src/json.cc b/src/json.cc index 825d5905..6dd35f43 100644 --- a/src/json.cc +++ b/src/json.cc @@ -521,84 +521,6 @@ std::string jsonEscape(const std::string& s) return t; } -std::string encode(const ValueBase* vlb) -{ - class JsonValueBaseVisitor:public ValueBaseVisitor { - private: - std::ostringstream out_; - public: - virtual void visit(const String& string) - { - const std::string& s = string.s(); - std::string t = jsonEscape(s); - out_ << '"'; - out_.write(t.data(), t.size()); - out_ << '"'; - } - - virtual void visit(const Integer& integer) - { - out_ << integer.i(); - } - - virtual void visit(const Bool& boolValue) - { - out_ << (boolValue.val() ? "true" : "false"); - } - - virtual void visit(const Null& nullValue) - { - out_ << "null"; - } - - virtual void visit(const List& list) - { - out_ << '['; - List::ValueType::const_iterator i = list.begin(); - if(!list.empty()) { - (*i)->accept(*this); - } - ++i; - for(List::ValueType::const_iterator eoi = list.end(); i != eoi; ++i){ - out_ << ','; - (*i)->accept(*this); - } - out_ << ']'; - } - - virtual void visit(const Dict& dict) - { - out_ << '{'; - Dict::ValueType::const_iterator i = dict.begin(); - if(!dict.empty()) { - std::string key = jsonEscape((*i).first); - out_ << '"'; - out_.write(key.data(), key.size()); - out_ << "\":"; - (*i).second->accept(*this); - } - ++i; - for(Dict::ValueType::const_iterator eoi = dict.end(); i != eoi; ++i){ - out_ << ','; - std::string key = jsonEscape((*i).first); - out_ << '"'; - out_.write(key.data(), key.size()); - out_ << "\":"; - (*i).second->accept(*this); - } - out_ << '}'; - } - - std::string getResult() const - { - return out_.str(); - } - }; - JsonValueBaseVisitor visitor; - vlb->accept(visitor); - return visitor.getResult(); -} - // Serializes JSON object or array. std::string encode(const SharedHandle& json) { diff --git a/src/json.h b/src/json.h index 932fe3a9..3a106fdd 100644 --- a/src/json.h +++ b/src/json.h @@ -127,7 +127,6 @@ OutputStream& encode(OutputStream& out, const SharedHandle& vlb) } // Serializes JSON object or array. -std::string encode(const ValueBase* json); std::string encode(const SharedHandle& json); struct JsonGetParam { diff --git a/test/JsonTest.cc b/test/JsonTest.cc index 8c81a44c..971982ff 100644 --- a/test/JsonTest.cc +++ b/test/JsonTest.cc @@ -413,43 +413,43 @@ void JsonTest::testDecode_error() void JsonTest::testEncode() { { - Dict dict; - dict["name"] = String::g("aria2"); - dict["loc"] = Integer::g(80000); + SharedHandle dict = Dict::g(); + dict->put("name", String::g("aria2")); + dict->put("loc", Integer::g(80000)); SharedHandle files = List::g(); files->append(String::g("aria2c")); - dict["files"] = files; + dict->put("files", files); SharedHandle attrs = Dict::g(); attrs->put("license", String::g("GPL")); - dict["attrs"] = attrs; + dict->put("attrs", attrs); CPPUNIT_ASSERT_EQUAL(std::string("{\"attrs\":{\"license\":\"GPL\"}," "\"files\":[\"aria2c\"]," "\"loc\":80000," "\"name\":\"aria2\"}"), - json::encode(&dict)); + json::encode(dict)); } { - List list; - list.append("\"\\/\b\f\n\r\t"); + SharedHandle list = List::g(); + list->append("\"\\/\b\f\n\r\t"); CPPUNIT_ASSERT_EQUAL(std::string("[\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"]"), - json::encode(&list)); + json::encode(list)); } { - List list; + SharedHandle list = List::g(); std::string s; s += 0x1Fu; - list.append(s); + list->append(s); CPPUNIT_ASSERT_EQUAL(std::string("[\"\\u001F\"]"), - json::encode(&list)); + json::encode(list)); } { - List list; - list.append(Bool::gTrue()); - list.append(Bool::gFalse()); - list.append(Null::g()); + SharedHandle list = List::g(); + list->append(Bool::gTrue()); + list->append(Bool::gFalse()); + list->append(Null::g()); CPPUNIT_ASSERT_EQUAL(std::string("[true,false,null]"), - json::encode(&list)); + json::encode(list)); } }