summaryrefslogtreecommitdiff
path: root/util/json-cpp.hpp
blob: 851a4f4b8c77b1cc9c048b6b5559c35d23165996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
//
// DO NOT EDIT !!! This file was generated with a script.
//
// JSON for C++
// https://github.com/ascheglov/json-cpp
// Version 0.1 alpha, rev. 170121e2dc099895064305e38bfb25d90a807ce3
// Generated 2014-03-27 17:16:47.104492 UTC
//
// Belongs to the public domain

#pragma once

//----------------------------------------------------------------------
// json-cpp.hpp begin

//----------------------------------------------------------------------
// json-cpp/parse.hpp begin

#include <memory>
#include <istream>
#include <iterator>
#include <string>
#include <type_traits>

//----------------------------------------------------------------------
// json-cpp/ParserError.hpp begin

#include <cassert>
#include <cstddef>
#include <exception>
#include <string>

#if defined _MSC_VER
#   define JSONCPP_INTERNAL_NOEXCEPT_ throw()
#else
#   define JSONCPP_INTERNAL_NOEXCEPT_ noexcept
#endif

namespace jsoncpp
{
    class ParserError : public std::exception
    {
    public:
        enum Type
        {
            NoError,
            Eof, UnexpectedCharacter,
            InvalidEscapeSequence, NoTrailSurrogate,
            UnexpectedType, UnknownField,
            NumberIsOutOfRange,
        };

        ParserError(Type type, std::size_t line, std::size_t column)
            : m_type{type}, m_line{line}, m_column{column}
        {
            assert(type != NoError);
        }

        virtual const char* what() const JSONCPP_INTERNAL_NOEXCEPT_ override
        {
            if (m_what.empty())
            {
                m_what = "JSON parser error at line ";
                m_what += std::to_string(m_line);
                m_what += ", column ";
                m_what += std::to_string(m_column);
                switch (m_type)
                {
                case Eof: m_what += ": unexpected end of file"; break;
                case UnexpectedCharacter: m_what += ": unexpected character"; break;
                case InvalidEscapeSequence: m_what += ": invalid escape sequence"; break;
                case NoTrailSurrogate: m_what += ": no UTF-16 trail surrogate"; break;
                case UnexpectedType: m_what += ": unexpected value type"; break;
                case UnknownField: m_what += ": unknown field name"; break;
                case NumberIsOutOfRange: m_what += ": number is out of range"; break;
                case NoError:
                default:
                    m_what += ": INTERNAL ERROR"; break;
                }
            }

            return m_what.c_str();
        }

        Type type() const { return m_type; }
        std::size_t line() const { return m_line; }
        std::size_t column() const { return m_column; }

    private:
        Type m_type;
        std::size_t m_line;
        std::size_t m_column;

        mutable std::string m_what;
    };
}

#undef JSONCPP_INTERNAL_NOEXCEPT_

// json-cpp/ParserError.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/Stream.hpp begin

namespace jsoncpp
{
    template<class Traits>
    class Stream;

    namespace details
    {
        template<typename CharT, class X>
        struct Traits2 {};

        template<class Traits>
        struct ParserTraits {};

        template<class Traits>
        struct GeneratorTraits {};
    }

    template<class X>
    using Parser = Stream<details::ParserTraits<X>>;

    template<class X>
    using Generator = Stream<details::GeneratorTraits<X>>;

    template<typename X, typename T>
    inline auto serialize(Stream<X>& stream, T& value) -> decltype(value.serialize(stream), void())
    {
        value.serialize(stream);
    }
}
// json-cpp/Stream.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/value_types.hpp begin

namespace jsoncpp
{
    // Helper masks
    const auto TypeIsNotFundamental = 0x40;
    const auto TypeIsCollection = 0x80;

    enum class Type
    {
        Undefined = 0, // Helper type for debugging variant-like types
        Null = 0x01,
        Boolean = 0x02,
        Number = 0x04,
        String = 0x08 | TypeIsNotFundamental,
        Array = 0x10 | TypeIsNotFundamental | TypeIsCollection,
        Object = 0x20 | TypeIsNotFundamental | TypeIsCollection,
    };
}
// json-cpp/value_types.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/details/parser_utility.hpp begin

#include <cassert>
#include <cstddef>
#include <utility>

namespace jsoncpp { namespace details
{
    template<typename CharT>
    struct CStrIterator
    {
        using this_type = CStrIterator<CharT>;

        CStrIterator()
        {
            static CharT null{0};
            m_ptr = &null;
        }

        CStrIterator(const CharT* ptr) : m_ptr{ptr} {}

        const CharT& operator*() { return *m_ptr; }
        const CharT* operator->() { return m_ptr; }

        this_type& operator++()
        {
            assert(!isEnd());
            ++m_ptr;
            return *this;
        }

        this_type operator++(int) { auto temp = *this; ++*this; return temp; }

        bool operator==(const this_type& rhs) const { return isEnd() == rhs.isEnd(); }
        bool operator!=(const this_type& rhs) const { return !this->operator==(rhs); }

    private:
        const CharT* m_ptr;

        bool isEnd() const { return *m_ptr == 0; }
    };

    class Diagnostics
    {
    public:
        void nextColumn() { ++m_column; }
        void newLine() { ++m_line; m_column = 0; }

        ParserError makeError(ParserError::Type type) const
        {
            return{type, m_line, m_column};
        }

    private:
        std::size_t m_column{0};
        std::size_t m_line{1};
    };

    template<typename InputIterator>
    struct Reader
    {
        using this_type = Reader<InputIterator>;

        Reader(InputIterator first, InputIterator last) : m_iter(first), m_end(last)
        {
            checkEnd();
        }

        char operator*() { return *m_iter; }
        this_type& operator++()
        {
            checkEnd();
            ++m_iter;
            m_diag.nextColumn();
            return *this;
        }

        void checkEnd()
        {
            if (m_iter == m_end)
                throw m_diag.makeError(ParserError::Eof);
        }

        char getNextChar()
        {
            auto prev = *m_iter;
            ++*this;
            return prev;
        }

        Diagnostics m_diag;
        InputIterator m_iter, m_end;
    };
}}

// json-cpp/details/parser_utility.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/details/number_parser.hpp begin

#include <cmath>

namespace jsoncpp { namespace details
{
    inline bool isDigit(char c) { return c >= '0' && c <= '9'; }

    template<typename Iterator>
    inline unsigned parseIntNumber(Iterator& iter)
    {
        auto intPart = 0U; // TBD: 0ULL ?

        do
        {
            intPart = intPart * 10 + (*iter - '0');

            ++iter;
        }
        while (isDigit(*iter));

        return intPart;
    }

    template<typename Iterator>
    inline double parseRealNumber(Iterator& iter)
    {
        double number = 0;

        if (*iter == '0')
        {
            ++iter;
        }
        else
        {
            number = parseIntNumber(iter);
        }

        // here `ch` is a peeked character, need to call eat()

        if (*iter == '.')
        {
            ++iter;

            auto mul = 0.1;
            while (isDigit(*iter))
            {
                number += (*iter - '0') * mul;
                mul /= 10;
                ++iter;
            }
        }

        // here `ch` is a peeked character, need to call eat()

        if (*iter == 'e' || *iter == 'E')
        {
            ++iter;

            auto negate = *iter == '-';
            if (negate || *iter == '+')
                ++iter;
            // FIXME: check `ch` for non-digit

            auto e = parseIntNumber(iter);

            if (negate)
                number /= std::pow(10, e);
            else
                number *= std::pow(10, e);
        }

        return number;
    }
}}
// json-cpp/details/number_parser.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/details/string_parser.hpp begin

#include <string>

namespace jsoncpp { namespace details
{
    inline char32_t utf16SurrogatePairToUtf32(char32_t lead, char32_t trail)
    {
        return 0x10000 | (lead - 0xD800) << 10 | (trail - 0xDC00);
    }

    inline void utf32ToUtf8(char32_t c, std::string& str)
    {
        auto add = [&str](char32_t c){ str.push_back(static_cast<char>(c)); };

        if (c < 0x80)
        {
            add(c);
        }
        else if (c < 0x800)
        {
            add(0xC0 | c >> 6);
            add(0x80 | (c & 0x3f));
        }
        else if (c < 0x10000)
        {
            add(0xE0 | c >> 12);
            add(0x80 | ((c >> 6) & 0x3f));
            add(0x80 | (c & 0x3f));
        }
        else if (c < 0x200000)
        {
            add(0xF0 | c >> 18);
            add(0x80 | ((c >> 12) & 0x3f));
            add(0x80 | ((c >> 6) & 0x3f));
            add(0x80 | (c & 0x3f));
        }
        else if (c < 0x4000000)
        {
            add(0xF8 | c >> 24);
            add(0x80 | ((c >> 18) & 0x3f));
            add(0x80 | ((c >> 12) & 0x3f));
            add(0x80 | ((c >> 6) & 0x3f));
            add(0x80 | (c & 0x3f));
        }
        else
        {
            add(0xFC | c >> 30);
            add(0x80 | ((c >> 24) & 0x3f));
            add(0x80 | ((c >> 18) & 0x3f));
            add(0x80 | ((c >> 12) & 0x3f));
            add(0x80 | ((c >> 6) & 0x3f));
            add(0x80 | (c & 0x3f));
        }
    }

    enum class CharType { Raw, CodePoint, UTF16Pair };

    template<typename CharT, std::size_t CharSize>
    inline void addToStr(std::basic_string<CharT>& str, CharType type, char32_t c1, char32_t c2);

    template<>
    inline void addToStr<char, 1>(std::basic_string<char>& str, CharType type, char32_t c1, char32_t c2)
    {
        if (type == CharType::Raw)
        {
            str.push_back(static_cast<char>(c1));
        }
        else if (type == CharType::CodePoint)
        {
            utf32ToUtf8(c1, str);
        }
        else
        {
            auto c32 = utf16SurrogatePairToUtf32(c1, c2);
            utf32ToUtf8(c32, str);
        }
    }

    template<>
    inline void addToStr<wchar_t, 2>(std::basic_string<wchar_t>& str, CharType type, char32_t c1, char32_t c2)
    {
        str.push_back(static_cast<wchar_t>(c1));
        if (type == CharType::UTF16Pair)
            str.push_back(static_cast<wchar_t>(c2));
    }

    template<>
    inline void addToStr<wchar_t, 4>(std::basic_string<wchar_t>& str, CharType type, char32_t c1, char32_t c2)
    {
        auto c = (type == CharType::UTF16Pair) ? utf16SurrogatePairToUtf32(c1, c2) : c1;
        str.push_back(static_cast<wchar_t>(c));
    }

    template<typename Iterator>
    inline int parseHexDigit(Iterator& iter, ParserError::Type& err)
    {
        auto ch = *iter;
        ++iter;
        if (ch >= '0' && ch <= '9') return ch - '0';
        if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
        if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
        
        err = ParserError::InvalidEscapeSequence;
        return 0;
    }

    template<typename Iterator>
    inline char32_t parseUTF16CodeUnit(Iterator& iter, ParserError::Type& err)
    {
        auto n = parseHexDigit(iter, err) << 12;
        n |= parseHexDigit(iter, err) << 8;
        n |= parseHexDigit(iter, err) << 4;
        n |= parseHexDigit(iter, err);
        return static_cast<char32_t>(n);
    }

    template<typename Iterator, typename CharT>
    inline ParserError::Type parseStringImpl(Iterator& iter, std::basic_string<CharT>& str)
    {
        str.clear();
        auto add = [&str](CharType type, char32_t c1, char32_t c2)
        {
            addToStr<CharT, sizeof(CharT)>(str, type, c1, c2);
        };

        for (;;)
        {
            auto ch = static_cast<char32_t>(*iter);
            ++iter;
            if (ch == '"')
                return ParserError::NoError;

            if (ch == '\\')
            {
                ch = static_cast<char32_t>(*iter);
                ++iter;
                switch (ch)
                {
                case '\\': case '"': case '/':
                    break;

                case 'b': ch = '\b'; break;
                case 'f': ch = '\f'; break;
                case 'n': ch = '\n'; break;
                case 'r': ch = '\r'; break;
                case 't': ch = '\t'; break;

                case 'u':
                    {
                        ParserError::Type err{ParserError::NoError};
                        auto codeUnit = parseUTF16CodeUnit(iter, err);
                        if (err != ParserError::NoError)
                            return err;

                        if (codeUnit >= 0xD800 && codeUnit < 0xDC00)
                        {
                            if (*iter != '\\') return ParserError::NoTrailSurrogate;
                            ++iter;
                            if (*iter != 'u') return ParserError::NoTrailSurrogate;
                            ++iter;

                            auto trailSurrogate = parseUTF16CodeUnit(iter, err);
                            if (err != ParserError::NoError)
                                return err;

                            add(CharType::UTF16Pair, codeUnit, trailSurrogate);
                        }
                        else
                        {
                            add(CharType::CodePoint, codeUnit, 0);
                        }
                    }
                    continue;

                default:
                    return ParserError::InvalidEscapeSequence;
                }
            }

            add(CharType::Raw, ch, 0);
        }
    }
}}

// json-cpp/details/string_parser.hpp end
//----------------------------------------------------------------------

namespace jsoncpp
{
    template<typename CharT, typename InputIterator>
    class Stream<details::ParserTraits<details::Traits2<CharT, InputIterator>>>
    {
    public:
        using this_type = Parser<details::Traits2<CharT, InputIterator>>;

        explicit Stream(InputIterator first, InputIterator last)
            : m_reader{first, last}
        {
            nextValue();
        }

        Type getType() const { return m_type; }
        bool getBoolean() const { return m_boolean; }
        double getNumber() const { return m_number; }
        const std::string& getFieldName() const { return m_fieldName; }

        void checkType(Type type) const
        {
            if (getType() != type)
                throw makeError(ParserError::UnexpectedType);
        }

        bool isListEnd(char terminator)
        {
            eatWhitespace();
            if (*m_reader != terminator)
                return false;

            ++m_reader;
            return true;
        }

        void eatListSeparator()
        {
            eatWhitespace();
            check(',');
            eatWhitespace();
        }

        void nextNameValuePair()
        {
            eatWhitespace();
            check('"');
            parseString(m_fieldName);
            eatWhitespace();
            check(':');
            nextValue();
        }

        void nextValue()
        {
            eatWhitespace();
            m_type = nextValueImpl();
        }

        template<typename DstCharT>
        void parseString(std::basic_string<DstCharT>& str)
        {
            auto err = parseStringImpl(m_reader, str);
            if (err != ParserError::NoError)
                throw m_reader.m_diag.makeError(err);
        }

        ParserError makeError(ParserError::Type type) const
        {
            return m_reader.m_diag.makeError(type);
        }

    private:
        Type nextValueImpl()
        {
            switch (*m_reader)
            {
            case '{': ++m_reader; return Type::Object;
            case '[': ++m_reader; return Type::Array;
            case 't': ++m_reader; checkLiteral("true"); m_boolean = true; return Type::Boolean;
            case 'f': ++m_reader; checkLiteral("false"); m_boolean = false; return Type::Boolean;
            case 'n': ++m_reader; checkLiteral("null"); return Type::Null;
            case '"': ++m_reader; return Type::String;

            case '0': case '1': case '2': case '3': case '4':
            case '5': case '6': case '7': case '8': case '9':
                m_number = parseRealNumber(m_reader);
                return Type::Number;

            case '-':
                ++m_reader;
                m_number = -parseRealNumber(m_reader);
                return Type::Number;
            }

            throw unexpectedCharacter();
        }

        ParserError unexpectedCharacter() const
        {
            return makeError(ParserError::UnexpectedCharacter);
        }

        void check(char expectedChar)
        {
            if (*m_reader != expectedChar)
                throw unexpectedCharacter();

            ++m_reader;
        }

        template<std::size_t N>
        void checkLiteral(const char(&literal)[N])
        {
            static_assert(N > 2, "");
            for (auto i = 1; i != N - 1; ++i, ++m_reader)
                if (*m_reader != literal[i])
                    throw unexpectedCharacter();
        }

        void eatWhitespace()
        {
            for (;; ++m_reader)
            {
                switch (*m_reader)
                {
                case '/':
                    ++m_reader;
                    check('/');
                    while (*m_reader != '\n')
                        ++m_reader;

                    // no break here
                case '\n':
                    m_reader.m_diag.newLine();
                    break;

                case ' ': case '\t': case '\r':
                    break;

                default:
                    return;
                }
            }
        }

        details::Reader<InputIterator> m_reader;

        Type m_type;
        double m_number;
        bool m_boolean;
        std::string m_fieldName;
    };

    template<class X>
    inline void serialize(Parser<X>& parser, bool& value)
    {
        parser.checkType(Type::Boolean);
        value = parser.getBoolean();
    }

    template<class X, typename T>
    inline typename std::enable_if<std::is_arithmetic<T>::value>::type
        serialize(Parser<X>& parser, T& value)
    {
        parser.checkType(Type::Number);
        auto number = parser.getNumber();
        value = static_cast<T>(number);
        if (value != number)
            throw parser.makeError(ParserError::NumberIsOutOfRange);
    }

    template<class X, typename DstCharT>
    inline void serialize(Parser<X>& parser, std::basic_string<DstCharT>& value)
    {
        parser.checkType(Type::String);
        parser.parseString(value);
    }

    namespace details
    {
        template<class X, typename Callback>
        inline void parseList(Parser<X>& parser, Type type, char terminator, Callback&& callback)
        {
            parser.checkType(type);

            while (!parser.isListEnd(terminator))
            {
                callback();

                if (parser.isListEnd(terminator))
                    return;

                parser.eatListSeparator();
            }
        }
    }

    template<class X, typename Callback>
    inline void parseObject(Parser<X>& parser, Callback&& callback)
    {
        details::parseList(parser, Type::Object, '}', [&]
        {
            parser.nextNameValuePair();
            callback(parser.getFieldName());
        });
    }

    template<class X, typename Callback>
    void parseArray(Parser<X>& parser, Callback&& callback)
    {
        details::parseList(parser, Type::Array, ']', [&]
        {
            parser.nextValue();
            callback();
        });
    }

    template<typename CharT, class T, typename InputIterator>
    inline void parse(T& object, InputIterator first, InputIterator last)
    {
        Parser<details::Traits2<CharT, InputIterator>> stream{first, last};
        serialize(stream, object);
    }

    template<typename T, typename CharT>
    inline void parse(T& object, const CharT* str)
    {
        details::CStrIterator<CharT> first{str}, last;
        parse<CharT>(object, first, last);
    }

    template<typename T, typename CharT>
    inline void parse(T& object, std::basic_string<CharT>& str)
    {
        parse<CharT>(object, std::begin(str), std::end(str));
    }

    template<typename T, typename CharT>
    inline void parse(T& object, std::basic_istream<CharT>& stream)
    {
        std::istreambuf_iterator<CharT> first{stream}, last;
        parse<CharT>(object, first, last);
    }
}

// json-cpp/parse.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/std_types.hpp begin

#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//----------------------------------------------------------------------
// json-cpp/generate.hpp begin

#include <sstream>
#include <string>

//----------------------------------------------------------------------
// json-cpp/details/string_writer.hpp begin

#include <string>

namespace jsoncpp { namespace details
{
    template<typename SrcCharT, typename Sink>
    inline void writeString(const std::basic_string<SrcCharT>& str, Sink&& sink)
    {
        sink('"');
        for (auto iter = std::begin(str), last = std::end(str); iter != last; ++iter)
        {
            switch (char32_t ch = static_cast<unsigned char>(*iter))
            {
            case '"': sink('\\'); sink('"'); break;
            case '\\': sink('\\'); sink('\\'); break;
            case '\b': sink('\\'); sink('b'); break;
            case '\f': sink('\\'); sink('f'); break;
            case '\n': sink('\\'); sink('n'); break;
            case '\r': sink('\\'); sink('r'); break;
            case '\t': sink('\\'); sink('t'); break;
            default:
                if (ch < '\x20')
                {
                    const auto table = "0123456789ABCDEF";
                    unsigned n = static_cast<unsigned char>(ch);
                    sink('\\');
                    sink('u');
                    sink('0');
                    sink('0');
                    sink(table[n >> 4]);
                    sink(table[n & 15]);
                }
                else
                {
                    sink(static_cast<char>(ch));
                }
            }
        }
        sink('"');
    }
}}

// json-cpp/details/string_writer.hpp end
//----------------------------------------------------------------------

namespace jsoncpp
{
    template<typename CharT, typename Sink>
    class Stream<details::GeneratorTraits<details::Traits2<CharT, Sink>>>
    {
    public:
        using this_type = Generator<details::Traits2<CharT, Sink>>;

        explicit Stream(Sink& sink) : m_sink(&sink) {}

        void objectBegin()
        {
            (*m_sink) << "{";
        }

        void fieldName(const char* name)
        {
            (*m_sink) << '"' << name << "\": ";
            // TODO: use writeString (?)
        }

        template<typename StrCharT>
        void fieldName(const std::basic_string<StrCharT>& name)
        {
            (*m_sink) << '"' << name << "\": ";
            // TODO: use writeString (?)
        }

        void separator()
        {
            (*m_sink) << ", ";
        }

        void objectEnd()
        {
            (*m_sink) << '}';
        }

        void arrayBegin()
        {
            (*m_sink) << '[';
        }

        void arrayEnd()
        {
            (*m_sink) << ']';
        }

        friend void serialize(this_type& stream, std::nullptr_t)
        {
            (*stream.m_sink) << "null";
        }

        friend void serialize(this_type& stream, bool value)
        {
            (*stream.m_sink) << (value ? "true" : "false");
        }

        template<typename T>
        friend typename std::enable_if<std::is_arithmetic<T>::value>::type serialize(this_type& stream, T& value)
        {
            (*stream.m_sink) << value;
        }

        template<typename SrcCharT>
        friend void serialize(this_type& stream, const std::basic_string<SrcCharT>& value)
        {
            details::writeString(value, [&stream](char c){ stream.m_sink->put(c); });
        }

    private:
        Sink* m_sink;
    };

    template<class X, typename Pointer>
    inline void writePointer(Generator<X>& generator, Pointer& ptr)
    {
        if (ptr)
        {
            serialize(generator, *ptr);
        }
        else
        {
            serialize(generator, nullptr);
        }
    }

    template<class X, typename Range>
    inline void writeRange(Generator<X>& generator, Range& range)
    {
        generator.arrayBegin();

        auto iter = std::begin(range);
        const auto& last = std::end(range);
        if (iter != last)
        {
            for (;;)
            {
                serialize(generator, *iter);

                ++iter;
                if (iter == last)
                    break;

                generator.separator();
            }
        }

        generator.arrayEnd();
    }

    template<class T>
    inline std::string to_string(const T& object)
    {
        std::ostringstream rawStream;
        Generator<details::Traits2<char, std::ostream>> stream{rawStream};
        serialize(stream, const_cast<T&>(object));
        return rawStream.str();
    }
}

// json-cpp/generate.hpp end
//----------------------------------------------------------------------

namespace jsoncpp
{
    template<class X, typename T>
    inline void serialize(Parser<X>& parser, std::shared_ptr<T>& obj)
    {
        if (parser.getType() != jsoncpp::Type::Null)
        {
            obj = std::make_shared<T>();
            serialize(parser, *obj);
        }
        else
        {
            obj.reset();
        }
    }

    template<class X, typename T>
    inline void serialize(Generator<X>& generator, std::shared_ptr<T>& obj)
    {
        writePointer(generator, obj);
    }

    template<class X, typename T>
    inline void serialize(Parser<X>& parser, std::unique_ptr<T>& obj)
    {
        if (parser.getType() != jsoncpp::Type::Null)
        {
            obj->reset(new T());
            serialize(parser, *obj);
        }
        else
        {
            obj.reset();
        }
    }

    template<class X, typename T>
    inline void serialize(Generator<X>& generator, std::unique_ptr<T>& obj)
    {
        writePointer(generator, obj);
    }

    namespace details
    {
        template<class X, typename C>
        inline void serializeContainer(Parser<X>& parser, C& c)
        {
            c.clear();

            parseArray(parser, [&]
            {
                c.emplace_back();
                serialize(parser, c.back());
            });
        }

        template<class X, typename C>
        inline void serializeContainer(Generator<X>& generator, C& c)
        {
            writeRange(generator, c);
        }

        template<class X, typename C>
        inline void serializeSet(Parser<X>& parser, C& c)
        {
            c.clear();

            parseArray(parser, [&]
            {
                typename C::value_type value;
                serialize(parser, value);
                c.insert(value);
            });
        }

        template<class X, typename C>
        inline void serializeSet(Generator<X>& generator, C& c)
        {
            writeRange(generator, c);
        }

        template<class X, typename C>
        inline void serializeStrMap(Parser<X>& parser, C& c)
        {
            c.clear();

            parseObject(parser, [&](const std::string& name)
            {
                serialize(parser, c[name]);
            });
        }

        template<class X, typename C>
        inline void serializeStrMap(Generator<X>& generator, C& c)
        {
            generator.objectBegin();
            
            auto iter = std::begin(c);
            const auto& last = std::end(c);
            if (iter != last)
            {
                for (;;)
                {
                    generator.fieldName(iter->first);
                    serialize(generator, iter->second);

                    ++iter;
                    if (iter == last)
                        break;

                    generator.separator();
                }
            }

            generator.objectEnd();
        }
    }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::vector<T>& arr)
    { details::serializeContainer(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::list<T>& arr)
    { details::serializeContainer(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::forward_list<T>& arr)
    { details::serializeContainer(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::deque<T>& arr)
    { details::serializeContainer(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::set<T>& arr)
    { details::serializeSet(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::unordered_set<T>& arr)
    { details::serializeSet(stream, arr); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::map<std::string, T>& t)
    { details::serializeStrMap(stream, t); }

    template<class X, typename T>
    inline void serialize(Stream<X>& stream, std::unordered_map<std::string, T>& t)
    { details::serializeStrMap(stream, t); }
}
// json-cpp/std_types.hpp end
//----------------------------------------------------------------------

//----------------------------------------------------------------------
// json-cpp/serialization_helpers.hpp begin

#include <array>
#include <unordered_map>

namespace jsoncpp
{
    namespace details
    {
        template<class X, typename T>
        inline void writeField(Generator<X>& generator, const char* name, T& value)
        {
            generator.fieldName(name);
            serialize(generator, value);
        }

        template<class X, typename T, typename... F>
        inline void writeField(Generator<X>& generator, const char* name, T& value, F&&... fieldsDef)
        {
            writeField(generator, name, value);
            generator.separator();
            writeField(generator, fieldsDef...);
        }

        template<typename ParserT>
        class FieldsTable
        {
        public:
            template<typename... F>
            FieldsTable(F&&... fieldsDef)
            {
                m_map.reserve(sizeof...(fieldsDef) / 2);
                add(1, fieldsDef...);
            }

            struct FieldInfo
            {
                template<typename T>
                FieldInfo(T&, std::size_t idx)
                {
                    m_fieldIdx = idx;
                    m_parseFn = [](ParserT& parser, void* fieldPtr)
                    {
                        serialize(parser, static_cast<T&>(*reinterpret_cast<T*>(fieldPtr)));
                    };
                }

                std::size_t m_fieldIdx;
                void(*m_parseFn)(ParserT& parser, void* fieldPtr);
            };

            const FieldInfo* find(const std::string& name) const
            {
                auto it = m_map.find(name);
                return it == m_map.end() ? nullptr : &it->second;
            }

        private:
            template<typename T, typename... F>
            void add(std::size_t idx, const char* name, T& value, F&&... otherFields)
            {
                m_map.emplace(name, FieldInfo(value, idx));
                add(idx + 2, otherFields...);
            }

            void add(std::size_t /*idx*/) {}

            std::unordered_map<std::string, FieldInfo> m_map;
        };

        inline void* makePtrs(const char*) { return nullptr; }

        template<typename T>
        inline void* makePtrs(T& obj) { return &obj; }
    }

    template<class Cls, class X, typename... F>
    inline void fields(Cls&, Parser<X>& parser, F&&... fieldsDef)
    {
        std::array<void*, sizeof...(fieldsDef)> ptrs{details::makePtrs(fieldsDef)...};

        static const details::FieldsTable<Parser<X>> table{fieldsDef...};
        
        auto&& handler = [&](const std::string& fieldName)
        {
            auto fieldInfo = table.find(fieldName);
            if (fieldInfo == nullptr)
                throw parser.makeError(ParserError::UnknownField);

            auto fieldPtr = ptrs[fieldInfo->m_fieldIdx];
            fieldInfo->m_parseFn(parser, fieldPtr);
        };

        parseObject(parser, handler);
    }

    template<class Cls, class X, typename... F>
    inline void fields(Cls&, Generator<X>& generator, F&&... fieldsDef)
    {
        generator.objectBegin();
        details::writeField(generator, fieldsDef...);
        generator.objectEnd();
    }
}

// json-cpp/serialization_helpers.hpp end
//----------------------------------------------------------------------

// json-cpp.hpp end
//----------------------------------------------------------------------