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()) {
526 if (in.expect(']')) {
527 return ctx.parse_array_stop(idx);
530 if (! ctx.parse_array_item(in, idx)) {
534 } while (in.expect(','));
535 return in.expect(']') && ctx.parse_array_stop(idx);
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_array_stop(size_t) { return false; }
624 bool parse_object_start() { return false; }
625 template <typename Iter> bool parse_object_item(input<Iter>&, const std::string&) {
630 class default_parse_context {
634 default_parse_context(value* out) : out_(out) {}
639 bool set_bool(bool b) {
643 bool set_number(double f) {
647 template<typename Iter> bool parse_string(input<Iter>& in) {
648 *out_ = value(string_type, false);
649 return _parse_string(out_->get<std::string>(), in);
651 bool parse_array_start() {
652 *out_ = value(array_type, false);
655 template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
656 array& a = out_->get<array>();
657 a.push_back(value());
658 default_parse_context ctx(&a.back());
659 return _parse(ctx, in);
661 bool parse_array_stop(size_t) { return true; }
662 bool parse_object_start() {
663 *out_ = value(object_type, false);
666 template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string& key) {
667 object& o = out_->get<object>();
668 default_parse_context ctx(&o[key]);
669 return _parse(ctx, in);
672 default_parse_context(const default_parse_context&);
673 default_parse_context& operator=(const default_parse_context&);
676 class null_parse_context {
679 void push_back(int) {}
682 null_parse_context() {}
683 bool set_null() { return true; }
684 bool set_bool(bool) { return true; }
685 bool set_number(double) { return true; }
686 template <typename Iter> bool parse_string(input<Iter>& in) {
688 return _parse_string(s, in);
690 bool parse_array_start() { return true; }
691 template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
692 return _parse(*this, in);
694 bool parse_array_stop(size_t) { return true; }
695 bool parse_object_start() { return true; }
696 template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string&) {
697 return _parse(*this, in);
700 null_parse_context(const null_parse_context&);
701 null_parse_context& operator=(const null_parse_context&);
704 // obsolete, use the version below
705 template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) {
707 pos = parse(out, pos, last, &err);
711 template <typename Context, typename Iter> inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) {
712 input<Iter> in(first, last);
713 if (! _parse(ctx, in) && err != NULL) {
715 SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line());
719 if (ch == -1 || ch == '\n') {
721 } else if (ch >= ' ') {
729 template <typename Iter> inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) {
730 default_parse_context ctx(&out);
731 return _parse(ctx, first, last, err);
734 inline std::string parse(value& out, std::istream& is) {
736 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
737 std::istreambuf_iterator<char>(), &err);
741 template <typename T> struct last_error_t {
742 static std::string s;
744 template <typename T> std::string last_error_t<T>::s;
746 inline void set_last_error(const std::string& s) {
747 last_error_t<bool>::s = s;
750 inline const std::string& get_last_error() {
751 return last_error_t<bool>::s;
754 inline bool operator==(const value& x, const value& y) {
757 #define PICOJSON_CMP(type) \
759 return y.is<type>() && x.get<type>() == y.get<type>()
761 PICOJSON_CMP(double);
762 PICOJSON_CMP(std::string);
764 PICOJSON_CMP(object);
773 inline bool operator!=(const value& x, const value& y) {
779 template<> inline void swap(picojson::value& x, picojson::value& y)
785 inline std::istream& operator>>(std::istream& is, picojson::value& x)
787 picojson::set_last_error(std::string());
788 std::string err = picojson::parse(x, is);
790 picojson::set_last_error(err);
791 is.setstate(std::ios::failbit);
796 inline std::ostream& operator<<(std::ostream& os, const picojson::value& x)
798 x.serialize(std::ostream_iterator<char>(os));
808 #pragma warning(disable : 4127) // conditional expression is constant
813 static void plan(int num)
815 printf("1..%d\n", num);
818 static bool success = true;
820 static void ok(bool b, const char* name = "")
825 printf("%s %d - %s\n", b ? "ok" : "ng", n++, name);
828 template <typename T> void is(const T& x, const T& y, const char* name = "")
847 #define TEST(expr, expected) \
848 is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr)
850 TEST( (true), "true");
851 TEST( (false), "false");
853 TEST( (string("hello")), "\"hello\"");
854 TEST( ("hello"), "\"hello\"");
855 TEST( ("hello", 4), "\"hell\"");
859 for (int i = 0; i < 1024; i++) {
860 picojson::value vi(a);
861 std::stringstream ss;
865 double b = vo.get<double>();
866 if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) {
867 printf("ng i=%d a=%.18e b=%.18e\n", i, a, b);
875 #define TEST(in, type, cmp, serialize_test) { \
877 const char* s = in; \
878 string err = picojson::parse(v, s, s + strlen(s)); \
879 ok(err.empty(), in " no error"); \
880 ok(v.is<type>(), in " check type"); \
881 is<type>(v.get<type>(), cmp, in " correct output"); \
882 is(*s, '\0', in " read to eof"); \
883 if (serialize_test) { \
884 is(v.serialize(), string(in), in " serialize"); \
887 TEST("false", bool, false, true);
888 TEST("true", bool, true, true);
889 TEST("90.5", double, 90.5, false);
890 TEST("1.7976931348623157e+308", double, DBL_MAX, false);
891 TEST("\"hello\"", string, string("hello"), true);
892 TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"),
894 TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string,
895 string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false);
896 TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false);
899 #define TEST(type, expr) { \
901 const char *s = expr; \
902 string err = picojson::parse(v, s, s + strlen(s)); \
903 ok(err.empty(), "empty " #type " no error"); \
904 ok(v.is<picojson::type>(), "empty " #type " check type"); \
905 ok(v.get<picojson::type>().empty(), "check " #type " array size"); \
913 const char *s = "[1,true,\"hello\"]";
914 string err = picojson::parse(v, s, s + strlen(s));
915 ok(err.empty(), "array no error");
916 ok(v.is<picojson::array>(), "array check type");
917 is(v.get<picojson::array>().size(), size_t(3), "check array size");
918 ok(v.contains(0), "check contains array[0]");
919 ok(v.get(0).is<double>(), "check array[0] type");
920 is(v.get(0).get<double>(), 1.0, "check array[0] value");
921 ok(v.contains(1), "check contains array[1]");
922 ok(v.get(1).is<bool>(), "check array[1] type");
923 ok(v.get(1).get<bool>(), "check array[1] value");
924 ok(v.contains(2), "check contains array[2]");
925 ok(v.get(2).is<string>(), "check array[2] type");
926 is(v.get(2).get<string>(), string("hello"), "check array[2] value");
927 ok(!v.contains(3), "check not contains array[3]");
932 const char *s = "{ \"a\": true }";
933 string err = picojson::parse(v, s, s + strlen(s));
934 ok(err.empty(), "object no error");
935 ok(v.is<picojson::object>(), "object check type");
936 is(v.get<picojson::object>().size(), size_t(1), "check object size");
937 ok(v.contains("a"), "check contains property");
938 ok(v.get("a").is<bool>(), "check bool property exists");
939 is(v.get("a").get<bool>(), true, "check bool property value");
940 is(v.serialize(), string("{\"a\":true}"), "serialize object");
941 ok(!v.contains("z"), "check not contains property");
944 #define TEST(json, msg) do { \
946 const char *s = json; \
947 string err = picojson::parse(v, s, s + strlen(s)); \
948 is(err, string("syntax error at line " msg), msg); \
950 TEST("falsoa", "1 near: oa");
951 TEST("{]", "1 near: ]");
952 TEST("\n\bbell", "2 near: bell");
953 TEST("\"abc\nd\"", "1 near: ");
957 picojson::value v1, v2;
960 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
961 err = picojson::parse(v1, s, s + strlen(s));
962 s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }";
963 err = picojson::parse(v2, s, s + strlen(s));
964 ok((v1 == v2), "check == operator in deep comparison");
968 picojson::value v1, v2;
971 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
972 err = picojson::parse(v1, s, s + strlen(s));
973 s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }";
974 err = picojson::parse(v2, s, s + strlen(s));
975 ok((v1 != v2), "check != operator for array in deep comparison");
979 picojson::value v1, v2;
982 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
983 err = picojson::parse(v1, s, s + strlen(s));
984 s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }";
985 err = picojson::parse(v2, s, s + strlen(s));
986 ok((v1 != v2), "check != operator for object in deep comparison");
990 picojson::value v1, v2;
993 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
994 err = picojson::parse(v1, s, s + strlen(s));
995 picojson::object& o = v1.get<picojson::object>();
997 picojson::array& a = o["a"].get<picojson::array>();
998 picojson::array::iterator i;
999 i = std::remove(a.begin(), a.end(), picojson::value(std::string("three")));
1000 a.erase(i, a.end());
1001 s = "{ \"a\": [1,2], \"d\": 2 }";
1002 err = picojson::parse(v2, s, s + strlen(s));
1003 ok((v1 == v2), "check erase()");
1006 ok(picojson::value(3.0).serialize() == "3",
1007 "integral number should be serialized as a integer");
1010 const char* s = "{ \"a\": [1,2], \"d\": 2 }";
1011 picojson::null_parse_context ctx;
1013 picojson::_parse(ctx, s, s + strlen(s), &err);
1014 ok(err.empty(), "null_parse_context");
1018 picojson::value v1, v2;
1019 v1 = picojson::value(true);
1021 ok(v1.is<picojson::null>(), "swap (null)");
1022 ok(v2.get<bool>() == true, "swap (bool)");
1024 v1 = picojson::value("a");
1025 v2 = picojson::value(1.0);
1027 ok(v1.get<double>() == 1.0, "swap (dobule)");
1028 ok(v2.get<string>() == "a", "swap (string)");
1030 v1 = picojson::value(picojson::object());
1031 v2 = picojson::value(picojson::array());
1033 ok(v1.is<picojson::array>(), "swap (array)");
1034 ok(v2.is<picojson::object>(), "swap (object)");
1037 return success ? 0 : 1;