2 * Copyright 2009-2010 Cybozu Labs, Inc.
3 * Copyright 2011 Kazuho Oku
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * The views and conclusions contained in the software and documentation are
26 * those of the authors and should not be interpreted as representing official
27 * policies, either expressed or implied, of Cybozu Labs, Inc.
46 #define SNPRINTF _snprintf_s
48 #pragma warning(disable : 4244) // conversion from int to char
50 #define SNPRINTF snprintf
68 typedef std::vector<value> array;
69 typedef std::map<std::string, value> object;
82 value(int type, bool);
83 explicit value(bool b);
84 explicit value(double n);
85 explicit value(const std::string& s);
86 explicit value(const array& a);
87 explicit value(const object& o);
88 explicit value(const char* s);
89 value(const char* s, size_t len);
91 value(const value& x);
92 value& operator=(const value& x);
94 template <typename T> bool is() const;
95 template <typename T> const T& get() const;
96 template <typename T> T& get();
97 bool evaluate_as_boolean() const;
98 const value& get(size_t idx) const;
99 const value& get(const std::string& key) const;
100 bool contains(size_t idx) const;
101 bool contains(const std::string& key) const;
102 std::string to_str() const;
103 template <typename Iter> void serialize(Iter os) const;
104 std::string serialize() const;
106 template <typename T> value(const T*); // intentionally defined to block implicit conversion of pointer to bool
109 typedef value::array array;
110 typedef value::object object;
112 inline value::value() : type_(null_type) {}
114 inline value::value(int type, bool) : type_(type) {
116 #define INIT(p, v) case p##type: u_.p = v; break
117 INIT(boolean_, false);
119 INIT(string_, new std::string());
120 INIT(array_, new array());
121 INIT(object_, new object());
127 inline value::value(bool b) : type_(boolean_type) {
131 inline value::value(double n) : type_(number_type) {
135 inline value::value(const std::string& s) : type_(string_type) {
136 u_.string_ = new std::string(s);
139 inline value::value(const array& a) : type_(array_type) {
140 u_.array_ = new array(a);
143 inline value::value(const object& o) : type_(object_type) {
144 u_.object_ = new object(o);
147 inline value::value(const char* s) : type_(string_type) {
148 u_.string_ = new std::string(s);
151 inline value::value(const char* s, size_t len) : type_(string_type) {
152 u_.string_ = new std::string(s, len);
155 inline value::~value() {
157 #define DEINIT(p) case p##type: delete u_.p; break
166 inline value::value(const value& x) : type_(x.type_) {
168 #define INIT(p, v) case p##type: u_.p = v; break
169 INIT(string_, new std::string(*x.u_.string_));
170 INIT(array_, new array(*x.u_.array_));
171 INIT(object_, new object(*x.u_.object_));
179 inline value& value::operator=(const value& x) {
187 inline void value::swap(value& x) {
188 std::swap(type_, x.type_);
192 #define IS(ctype, jtype) \
193 template <> inline bool value::is<ctype>() const { \
194 return type_ == jtype##_type; \
200 IS(std::string, string)
205 #define GET(ctype, var) \
206 template <> inline const ctype& value::get<ctype>() const { \
207 assert("type mismatch! call vis<type>() before get<type>()" \
211 template <> inline ctype& value::get<ctype>() { \
212 assert("type mismatch! call is<type>() before get<type>()" \
216 GET(bool, u_.boolean_)
217 GET(double, u_.number_)
218 GET(std::string, *u_.string_)
219 GET(array, *u_.array_)
220 GET(object, *u_.object_)
223 inline bool value::evaluate_as_boolean() const {
230 return u_.number_ != 0;
232 return ! u_.string_->empty();
238 inline const value& value::get(size_t idx) const {
241 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
244 inline const value& value::get(const std::string& key) const {
246 assert(is<object>());
247 object::const_iterator i = u_.object_->find(key);
248 return i != u_.object_->end() ? i->second : s_null;
251 inline bool value::contains(size_t idx) const {
253 return idx < u_.array_->size();
256 inline bool value::contains(const std::string& key) const {
257 assert(is<object>());
258 object::const_iterator i = u_.object_->find(key);
259 return i != u_.object_->end();
262 inline std::string value::to_str() const {
264 case null_type: return "null";
265 case boolean_type: return u_.boolean_ ? "true" : "false";
269 SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_);
272 case string_type: return *u_.string_;
273 case array_type: return "array";
274 case object_type: return "object";
280 return std::string();
283 template <typename Iter> void copy(const std::string& s, Iter oi) {
284 std::copy(s.begin(), s.end(), oi);
287 template <typename Iter> void serialize_str(const std::string& s, Iter oi) {
289 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
291 #define MAP(val, sym) case val: copy(sym, oi); break
302 if ((unsigned char)*i < 0x20 || *i == 0x7f) {
304 SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff);
305 copy(buf, buf + 6, oi);
315 template <typename Iter> void value::serialize(Iter oi) const {
318 serialize_str(*u_.string_, oi);
322 for (array::const_iterator i = u_.array_->begin();
323 i != u_.array_->end();
325 if (i != u_.array_->begin()) {
335 for (object::const_iterator i = u_.object_->begin();
336 i != u_.object_->end();
338 if (i != u_.object_->begin()) {
341 serialize_str(i->first, oi);
343 i->second.serialize(oi);
354 inline std::string value::serialize() const {
356 serialize(std::back_inserter(s));
360 template <typename Iter> class input {
367 input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}
377 if (last_ch_ == '\n') {
380 last_ch_ = *cur_++ & 0xff;
384 if (last_ch_ != -1) {
389 Iter cur() const { return cur_; }
390 int line() const { return line_; }
394 if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) {
400 bool expect(int expect) {
402 if (getc() != expect) {
408 bool match(const std::string& pattern) {
409 for (std::string::const_iterator pi(pattern.begin());
421 template<typename Iter> inline int _parse_quadhex(input<Iter> &in) {
423 for (int i = 0; i < 4; i++) {
424 if ((hex = in.getc()) == -1) {
427 if ('0' <= hex && hex <= '9') {
429 } else if ('A' <= hex && hex <= 'F') {
431 } else if ('a' <= hex && hex <= 'f') {
437 uni_ch = uni_ch * 16 + hex;
442 template<typename String, typename Iter> inline bool _parse_codepoint(String& out, input<Iter>& in) {
444 if ((uni_ch = _parse_quadhex(in)) == -1) {
447 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
448 if (0xdc00 <= uni_ch) {
449 // a second 16-bit of a surrogate pair appeared
452 // first 16-bit of surrogate pair, get the next one
453 if (in.getc() != '\\' || in.getc() != 'u') {
457 int second = _parse_quadhex(in);
458 if (! (0xdc00 <= second && second <= 0xdfff)) {
461 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
465 out.push_back(uni_ch);
467 if (uni_ch < 0x800) {
468 out.push_back(0xc0 | (uni_ch >> 6));
470 if (uni_ch < 0x10000) {
471 out.push_back(0xe0 | (uni_ch >> 12));
473 out.push_back(0xf0 | (uni_ch >> 18));
474 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
476 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
478 out.push_back(0x80 | (uni_ch & 0x3f));
483 template<typename String, typename Iter> inline bool _parse_string(String& out, input<Iter>& in) {
489 } else if (ch == '"') {
491 } else if (ch == '\\') {
492 if ((ch = in.getc()) == -1) {
496 #define MAP(sym, val) case sym: out.push_back(val); break
507 if (! _parse_codepoint(out, in)) {
521 template <typename Context, typename Iter> inline bool _parse_array(Context& ctx, input<Iter>& in) {
522 if (! ctx.parse_array_start()) {
525 if (in.expect(']')) {
530 if (! ctx.parse_array_item(in, idx)) {
534 } while (in.expect(','));
535 return in.expect(']');
538 template <typename Context, typename Iter> inline bool _parse_object(Context& ctx, input<Iter>& in) {
539 if (! ctx.parse_object_start()) {
542 if (in.expect('}')) {
548 || ! _parse_string(key, in)
549 || ! in.expect(':')) {
552 if (! ctx.parse_object_item(in, key)) {
555 } while (in.expect(','));
556 return in.expect('}');
559 template <typename Iter> inline bool _parse_number(double& out, input<Iter>& in) {
563 if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.'
564 || ch == 'e' || ch == 'E') {
565 num_str.push_back(ch);
572 out = strtod(num_str.c_str(), &endp);
573 return endp == num_str.c_str() + num_str.size();
576 template <typename Context, typename Iter> inline bool _parse(Context& ctx, input<Iter>& in) {
580 #define IS(ch, text, op) case ch: \
581 if (in.match(text) && op) { \
586 IS('n', "ull", ctx.set_null());
587 IS('f', "alse", ctx.set_bool(false));
588 IS('t', "rue", ctx.set_bool(true));
591 return ctx.parse_string(in);
593 return _parse_array(ctx, in);
595 return _parse_object(ctx, in);
597 if (('0' <= ch && ch <= '9') || ch == '-') {
600 if (_parse_number(f, in)) {
613 class deny_parse_context {
615 bool set_null() { return false; }
616 bool set_bool(bool) { return false; }
617 bool set_number(double) { return false; }
618 template <typename Iter> bool parse_string(input<Iter>&) { return false; }
619 bool parse_array_start() { return false; }
620 template <typename Iter> bool parse_array_item(input<Iter>&, size_t) {
623 bool parse_object_start() { return false; }
624 template <typename Iter> bool parse_object_item(input<Iter>&, const std::string&) {
629 class default_parse_context {
633 default_parse_context(value* out) : out_(out) {}
638 bool set_bool(bool b) {
642 bool set_number(double f) {
646 template<typename Iter> bool parse_string(input<Iter>& in) {
647 *out_ = value(string_type, false);
648 return _parse_string(out_->get<std::string>(), in);
650 bool parse_array_start() {
651 *out_ = value(array_type, false);
654 template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
655 array& a = out_->get<array>();
656 a.push_back(value());
657 default_parse_context ctx(&a.back());
658 return _parse(ctx, in);
660 bool parse_object_start() {
661 *out_ = value(object_type, false);
664 template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string& key) {
665 object& o = out_->get<object>();
666 default_parse_context ctx(&o[key]);
667 return _parse(ctx, in);
670 default_parse_context(const default_parse_context&);
671 default_parse_context& operator=(const default_parse_context&);
674 class null_parse_context {
677 void push_back(int) {}
680 null_parse_context() {}
681 bool set_null() { return true; }
682 bool set_bool(bool) { return true; }
683 bool set_number(double) { return true; }
684 template <typename Iter> bool parse_string(input<Iter>& in) {
686 return _parse_string(s, in);
688 bool parse_array_start() { return true; }
689 template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
690 return _parse(*this, in);
692 bool parse_object_start() { return true; }
693 template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string&) {
694 return _parse(*this, in);
697 null_parse_context(const null_parse_context&);
698 null_parse_context& operator=(const null_parse_context&);
701 // obsolete, use the version below
702 template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) {
704 pos = parse(out, pos, last, &err);
708 template <typename Context, typename Iter> inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) {
709 input<Iter> in(first, last);
710 if (! _parse(ctx, in) && err != NULL) {
712 SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line());
716 if (ch == -1 || ch == '\n') {
718 } else if (ch >= ' ') {
726 template <typename Iter> inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) {
727 default_parse_context ctx(&out);
728 return _parse(ctx, first, last, err);
731 inline std::string parse(value& out, std::istream& is) {
733 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
734 std::istreambuf_iterator<char>(), &err);
738 template <typename T> struct last_error_t {
739 static std::string s;
741 template <typename T> std::string last_error_t<T>::s;
743 inline void set_last_error(const std::string& s) {
744 last_error_t<bool>::s = s;
747 inline const std::string& get_last_error() {
748 return last_error_t<bool>::s;
751 inline bool operator==(const value& x, const value& y) {
754 #define PICOJSON_CMP(type) \
756 return y.is<type>() && x.get<type>() == y.get<type>()
758 PICOJSON_CMP(double);
759 PICOJSON_CMP(std::string);
761 PICOJSON_CMP(object);
770 inline bool operator!=(const value& x, const value& y) {
776 template<> inline void swap(picojson::value& x, picojson::value& y)
782 inline std::istream& operator>>(std::istream& is, picojson::value& x)
784 picojson::set_last_error(std::string());
785 std::string err = picojson::parse(x, is);
787 picojson::set_last_error(err);
788 is.setstate(std::ios::failbit);
793 inline std::ostream& operator<<(std::ostream& os, const picojson::value& x)
795 x.serialize(std::ostream_iterator<char>(os));
805 #pragma warning(disable : 4127) // conditional expression is constant
810 static void plan(int num)
812 printf("1..%d\n", num);
815 static bool success = true;
817 static void ok(bool b, const char* name = "")
822 printf("%s %d - %s\n", b ? "ok" : "ng", n++, name);
825 template <typename T> void is(const T& x, const T& y, const char* name = "")
844 #define TEST(expr, expected) \
845 is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr)
847 TEST( (true), "true");
848 TEST( (false), "false");
850 TEST( (string("hello")), "\"hello\"");
851 TEST( ("hello"), "\"hello\"");
852 TEST( ("hello", 4), "\"hell\"");
856 for (int i = 0; i < 1024; i++) {
857 picojson::value vi(a);
858 std::stringstream ss;
862 double b = vo.get<double>();
863 if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) {
864 printf("ng i=%d a=%.18e b=%.18e\n", i, a, b);
872 #define TEST(in, type, cmp, serialize_test) { \
874 const char* s = in; \
875 string err = picojson::parse(v, s, s + strlen(s)); \
876 ok(err.empty(), in " no error"); \
877 ok(v.is<type>(), in " check type"); \
878 is<type>(v.get<type>(), cmp, in " correct output"); \
879 is(*s, '\0', in " read to eof"); \
880 if (serialize_test) { \
881 is(v.serialize(), string(in), in " serialize"); \
884 TEST("false", bool, false, true);
885 TEST("true", bool, true, true);
886 TEST("90.5", double, 90.5, false);
887 TEST("1.7976931348623157e+308", double, DBL_MAX, false);
888 TEST("\"hello\"", string, string("hello"), true);
889 TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"),
891 TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string,
892 string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false);
893 TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false);
896 #define TEST(type, expr) { \
898 const char *s = expr; \
899 string err = picojson::parse(v, s, s + strlen(s)); \
900 ok(err.empty(), "empty " #type " no error"); \
901 ok(v.is<picojson::type>(), "empty " #type " check type"); \
902 ok(v.get<picojson::type>().empty(), "check " #type " array size"); \
910 const char *s = "[1,true,\"hello\"]";
911 string err = picojson::parse(v, s, s + strlen(s));
912 ok(err.empty(), "array no error");
913 ok(v.is<picojson::array>(), "array check type");
914 is(v.get<picojson::array>().size(), size_t(3), "check array size");
915 ok(v.contains(0), "check contains array[0]");
916 ok(v.get(0).is<double>(), "check array[0] type");
917 is(v.get(0).get<double>(), 1.0, "check array[0] value");
918 ok(v.contains(1), "check contains array[1]");
919 ok(v.get(1).is<bool>(), "check array[1] type");
920 ok(v.get(1).get<bool>(), "check array[1] value");
921 ok(v.contains(2), "check contains array[2]");
922 ok(v.get(2).is<string>(), "check array[2] type");
923 is(v.get(2).get<string>(), string("hello"), "check array[2] value");
924 ok(!v.contains(3), "check not contains array[3]");
929 const char *s = "{ \"a\": true }";
930 string err = picojson::parse(v, s, s + strlen(s));
931 ok(err.empty(), "object no error");
932 ok(v.is<picojson::object>(), "object check type");
933 is(v.get<picojson::object>().size(), size_t(1), "check object size");
934 ok(v.contains("a"), "check contains property");
935 ok(v.get("a").is<bool>(), "check bool property exists");
936 is(v.get("a").get<bool>(), true, "check bool property value");
937 is(v.serialize(), string("{\"a\":true}"), "serialize object");
938 ok(!v.contains("z"), "check not contains property");
941 #define TEST(json, msg) do { \
943 const char *s = json; \
944 string err = picojson::parse(v, s, s + strlen(s)); \
945 is(err, string("syntax error at line " msg), msg); \
947 TEST("falsoa", "1 near: oa");
948 TEST("{]", "1 near: ]");
949 TEST("\n\bbell", "2 near: bell");
950 TEST("\"abc\nd\"", "1 near: ");
954 picojson::value v1, v2;
957 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
958 err = picojson::parse(v1, s, s + strlen(s));
959 s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }";
960 err = picojson::parse(v2, s, s + strlen(s));
961 ok((v1 == v2), "check == operator in deep comparison");
965 picojson::value v1, v2;
968 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
969 err = picojson::parse(v1, s, s + strlen(s));
970 s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }";
971 err = picojson::parse(v2, s, s + strlen(s));
972 ok((v1 != v2), "check != operator for array in deep comparison");
976 picojson::value v1, v2;
979 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
980 err = picojson::parse(v1, s, s + strlen(s));
981 s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }";
982 err = picojson::parse(v2, s, s + strlen(s));
983 ok((v1 != v2), "check != operator for object in deep comparison");
987 picojson::value v1, v2;
990 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
991 err = picojson::parse(v1, s, s + strlen(s));
992 picojson::object& o = v1.get<picojson::object>();
994 picojson::array& a = o["a"].get<picojson::array>();
995 picojson::array::iterator i;
996 i = std::remove(a.begin(), a.end(), picojson::value(std::string("three")));
998 s = "{ \"a\": [1,2], \"d\": 2 }";
999 err = picojson::parse(v2, s, s + strlen(s));
1000 ok((v1 == v2), "check erase()");
1003 ok(picojson::value(3.0).serialize() == "3",
1004 "integral number should be serialized as a integer");
1007 const char* s = "{ \"a\": [1,2], \"d\": 2 }";
1008 picojson::null_parse_context ctx;
1010 picojson::_parse(ctx, s, s + strlen(s), &err);
1011 ok(err.empty(), "null_parse_context");
1015 picojson::value v1, v2;
1016 v1 = picojson::value(true);
1018 ok(v1.is<picojson::null>(), "swap (null)");
1019 ok(v2.get<bool>() == true, "swap (bool)");
1021 v1 = picojson::value("a");
1022 v2 = picojson::value(1.0);
1024 ok(v1.get<double>() == 1.0, "swap (dobule)");
1025 ok(v2.get<string>() == "a", "swap (string)");
1027 v1 = picojson::value(picojson::object());
1028 v2 = picojson::value(picojson::array());
1030 ok(v1.is<picojson::array>(), "swap (array)");
1031 ok(v2.is<picojson::object>(), "swap (object)");
1034 return success ? 0 : 1;