File: jsonrpcpp.hpp

package info (click to toggle)
snapcast 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,252 kB
  • sloc: cpp: 40,067; python: 3,260; sh: 455; makefile: 16
file content (1370 lines) | stat: -rw-r--r-- 33,501 bytes parent folder | download | duplicates (3)
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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
/***
       __  ____   __   __ _  ____  ____   ___  _    _
     _(  )/ ___) /  \ (  ( \(  _ \(  _ \ / __)( )  ( )
    / \) \\___ \(  O )/    / )   / ) __/( (__(_ _)(_ _)
    \____/(____/ \__/ \_)__)(__\_)(__)   \___)(_)  (_)
    version 1.4.1
    https://github.com/badaix/jsonrpcpp

    This file is part of jsonrpc++
    Copyright (C) 2017-2025 Johannes Pohl

    This software may be modified and distributed under the terms
    of the MIT license.  See the LICENSE file for details.
***/

/// http://patorjk.com/software/taag/#p=display&f=Graceful&t=JSONRPC%2B%2B

/// checked with clang-tidy:
/// run-clang-tidy-3.8.py -header-filter='jsonrpcpp.hpp'
/// -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace,-modernize-pass-by-value,-google-explicit-constructor'

#ifndef JSON_RPC_HPP
#define JSON_RPC_HPP

// nlohmann-json
#include "common/json.hpp"

// standard headers
#include <cstring>
#include <exception>
#include <string>
#include <vector>


using Json = nlohmann::json;

namespace jsonrpcpp
{

class Entity;
class Request;
class Notification;
class Parameter;
class Response;
class Error;
class Batch;

using entity_ptr = std::shared_ptr<Entity>;
using request_ptr = std::shared_ptr<Request>;
using notification_ptr = std::shared_ptr<Notification>;
using parameter_ptr = std::shared_ptr<Parameter>;
using response_ptr = std::shared_ptr<Response>;
using error_ptr = std::shared_ptr<Error>;
using batch_ptr = std::shared_ptr<Batch>;


class Entity
{
public:
    enum class entity_t : uint8_t
    {
        unknown,
        exception,
        id,
        error,
        response,
        request,
        notification,
        batch
    };

    Entity(entity_t type);
    virtual ~Entity() = default;
    Entity(const Entity&) = default;
    Entity& operator=(const Entity&) = default;

    bool is_exception() const;
    bool is_id() const;
    bool is_error() const;
    bool is_response() const;
    bool is_request() const;
    bool is_notification() const;
    bool is_batch() const;

    virtual std::string type_str() const;

    virtual Json to_json() const = 0;
    virtual void parse_json(const Json& json) = 0;

    virtual void parse(const std::string& json_str);
    virtual void parse(const char* json_str);

protected:
    entity_t entity;
};


class NullableEntity : public Entity
{
public:
    NullableEntity(entity_t type);
    NullableEntity(entity_t type, std::nullptr_t);
#ifdef _MSC_VER
    virtual operator bool() const
#else
    virtual explicit operator bool() const
#endif
    {
        return !isNull;
    }

protected:
    bool isNull;
};


class Id : public Entity
{
public:
    enum class value_t : uint8_t
    {
        null,
        string,
        integer
    };

    Id();
    Id(int id);
    Id(const char* id);
    Id(const std::string& id);
    Id(const Json& json_id);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    friend std::ostream& operator<<(std::ostream& out, const Id& id)
    {
        out << id.to_json();
        return out;
    }

    const value_t& type() const
    {
        return type_;
    }

    int int_id() const
    {
        return int_id_;
    }

    const std::string& string_id() const
    {
        return string_id_;
    }

    bool operator<(const Id& other) const
    {
        return (to_json() < other.to_json());
    }

protected:
    value_t type_;
    int int_id_;
    std::string string_id_;
};


class Parameter : public NullableEntity
{
public:
    enum class value_t : uint8_t
    {
        null,
        array,
        map
    };

    Parameter(std::nullptr_t);
    Parameter(const Json& json = nullptr);
    Parameter(const std::string& key1, const Json& value1, const std::string& key2 = "", const Json& value2 = nullptr, const std::string& key3 = "",
              const Json& value3 = nullptr, const std::string& key4 = "", const Json& value4 = nullptr);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    bool is_array() const;
    bool is_map() const;
    bool is_null() const;

    Json get(const std::string& key) const;
    Json get(size_t idx) const;
    bool has(const std::string& key) const;
    bool has(size_t idx) const;

    void add(const std::string& key, const Json& value);

    template <typename T>
    T get(const std::string& key) const
    {
        return get(key).get<T>();
    }

    template <typename T>
    T get(size_t idx) const
    {
        return get(idx).get<T>();
    }

    template <typename T>
    T get(const std::string& key, const T& default_value) const
    {
        if (!has(key))
            return default_value;
        return get<T>(key);
    }

    template <typename T>
    T get(size_t idx, const T& default_value) const
    {
        if (!has(idx))
            return default_value;
        return get<T>(idx);
    }

    value_t type;
    std::vector<Json> param_array;
    std::map<std::string, Json> param_map;
};


class Error : public NullableEntity
{
public:
    Error(const Json& json = nullptr);
    Error(std::nullptr_t);
    Error(const std::string& message, int code, const Json& data = nullptr);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    int code() const
    {
        return code_;
    }

    const std::string& message() const
    {
        return message_;
    }

    const Json& data() const
    {
        return data_;
    }

protected:
    int code_;
    std::string message_;
    Json data_;
};


/// JSON-RPC 2.0 request
/**
 * Simple jsonrpc 2.0 parser with getters
 * Currently no named parameters are supported, but only array parameters
 */
class Request : public Entity
{
public:
    Request(const Json& json = nullptr);
    Request(const Id& id, const std::string& method, const Parameter& params = nullptr);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    const std::string& method() const
    {
        return method_;
    }

    const Parameter& params() const
    {
        return params_;
    }

    const Id& id() const
    {
        return id_;
    }

protected:
    std::string method_;
    Parameter params_;
    Id id_;
};


class RpcException : public std::exception
{
public:
    RpcException(const char* text);
    RpcException(const std::string& text);

    const char* what() const noexcept override;

protected:
    std::runtime_error m_;
};


class RpcEntityException : public RpcException, public Entity
{
public:
    RpcEntityException(const Error& error);
    RpcEntityException(const std::string& text);
    Json to_json() const override = 0;

    const Error& error() const
    {
        return error_;
    }

protected:
    void parse_json(const Json& json) override;
    Error error_;
};


class ParseErrorException : public RpcEntityException
{
public:
    ParseErrorException(const Error& error);
    ParseErrorException(const std::string& data);
    Json to_json() const override;
};


//	-32600	Invalid Request	The JSON sent is not a valid Request object.
//	-32601	Method not found	The method does not exist / is not available.
//	-32602	Invalid params	Invalid method parameter(s).
//	-32603	Internal error	Internal JSON-RPC error.

class RequestException : public RpcEntityException
{
public:
    RequestException(const Error& error, const Id& requestId = Id());
    Json to_json() const override;

    const Id& id() const
    {
        return id_;
    }

protected:
    Id id_;
};


class InvalidRequestException : public RequestException
{
public:
    InvalidRequestException(const Id& requestId = Id());
    InvalidRequestException(const Request& request);
    InvalidRequestException(const char* data, const Id& requestId = Id());
    InvalidRequestException(const std::string& data, const Id& requestId = Id());
};


class MethodNotFoundException : public RequestException
{
public:
    MethodNotFoundException(const Id& requestId = Id());
    MethodNotFoundException(const Request& request);
    MethodNotFoundException(const char* data, const Id& requestId = Id());
    MethodNotFoundException(const std::string& data, const Id& requestId = Id());
};


class InvalidParamsException : public RequestException
{
public:
    InvalidParamsException(const Id& requestId = Id());
    InvalidParamsException(const Request& request);
    InvalidParamsException(const char* data, const Id& requestId = Id());
    InvalidParamsException(const std::string& data, const Id& requestId = Id());
};


class InternalErrorException : public RequestException
{
public:
    InternalErrorException(const Id& requestId = Id());
    InternalErrorException(const Request& request);
    InternalErrorException(const char* data, const Id& requestId = Id());
    InternalErrorException(const std::string& data, const Id& requestId = Id());
};


class Response : public Entity
{
public:
    Response(const Json& json = nullptr);
    Response(const Id& id, const Json& result);
    Response(const Id& id, const Error& error);
    Response(const Request& request, const Json& result);
    Response(const Request& request, const Error& error);
    Response(const RequestException& exception);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    const Id& id() const
    {
        return id_;
    }

    const Json& result() const
    {
        return result_;
    }

    const Error& error() const
    {
        return error_;
    }

protected:
    Id id_;
    Json result_;
    Error error_;
};


class Notification : public Entity
{
public:
    Notification(const Json& json = nullptr);
    Notification(const char* method, const Parameter& params = nullptr);
    Notification(const std::string& method, const Parameter& params);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    const std::string& method() const
    {
        return method_;
    }

    const Parameter& params() const
    {
        return params_;
    }

protected:
    std::string method_;
    Parameter params_;
};


typedef std::function<void(const Parameter& params)> notification_callback;
typedef std::function<jsonrpcpp::response_ptr(const Id& id, const Parameter& params)> request_callback;

class Parser
{
public:
    Parser() = default;
    virtual ~Parser() = default;

    entity_ptr parse(const std::string& json_str);
    entity_ptr parse_json(const Json& json);

    void register_notification_callback(const std::string& notification, notification_callback callback);
    void register_request_callback(const std::string& request, request_callback callback);

    static entity_ptr do_parse(const std::string& json_str);
    static entity_ptr do_parse_json(const Json& json);
    static bool is_request(const std::string& json_str);
    static bool is_request(const Json& json);
    static bool is_notification(const std::string& json_str);
    static bool is_notification(const Json& json);
    static bool is_response(const std::string& json_str);
    static bool is_response(const Json& json);
    static bool is_batch(const std::string& json_str);
    static bool is_batch(const Json& json);

private:
    std::map<std::string, notification_callback> notification_callbacks_;
    std::map<std::string, request_callback> request_callbacks_;
};


class Batch : public Entity
{
public:
    std::vector<entity_ptr> entities;

    Batch(const Json& json = nullptr);

    Json to_json() const override;
    void parse_json(const Json& json) override;

    template <typename T>
    void add(const T& entity)
    {
        entities.push_back(std::make_shared<T>(entity));
    }

    void add_ptr(const entity_ptr& entity)
    {
        entities.push_back(entity);
    }
};



/////////////////////////// Entity implementation /////////////////////////////

inline Entity::Entity(entity_t type) : entity(type)
{
}

inline bool Entity::is_exception() const
{
    return (entity == entity_t::exception);
}

inline bool Entity::is_id() const
{
    return (entity == entity_t::id);
}

inline bool Entity::is_error() const
{
    return (entity == entity_t::error);
}

inline bool Entity::is_response() const
{
    return (entity == entity_t::response);
}

inline bool Entity::is_request() const
{
    return (entity == entity_t::request);
}

inline bool Entity::is_notification() const
{
    return (entity == entity_t::notification);
}

inline bool Entity::is_batch() const
{
    return (entity == entity_t::batch);
}

inline void Entity::parse(const char* json_str)
{
    // http://www.jsonrpc.org/specification
    //	code	message	meaning
    //	-32700	Parse error	Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
    //	-32600	Invalid Request	The JSON sent is not a valid Request object.
    //	-32601	Method not found	The method does not exist / is not available.
    //	-32602	Invalid params	Invalid method parameter(s).
    //	-32603	Internal error	Internal JSON-RPC error.
    //	-32000 to -32099	Server error	Reserved for implementation-defined server-errors.
    try
    {
        parse_json(Json::parse(json_str));
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw ParseErrorException(e.what());
    }
}

inline void Entity::parse(const std::string& json_str)
{
    parse(json_str.c_str());
}

inline std::string Entity::type_str() const
{
    switch (entity)
    {
        case entity_t::unknown:
            return "unknown";
        case entity_t::id:
            return "id";
        case entity_t::exception:
            return "exception";
        case entity_t::error:
            return "error";
        case entity_t::response:
            return "response";
        case entity_t::request:
            return "request";
        case entity_t::notification:
            return "notification";
        case entity_t::batch:
            return "batch";
        default:
            return "unknown";
    }
}


/////////////////////////// NullableEntity implementation /////////////////////

inline NullableEntity::NullableEntity(entity_t type) : Entity(type), isNull(false)
{
}

inline NullableEntity::NullableEntity(entity_t type, std::nullptr_t) : Entity(type), isNull(true)
{
}


/////////////////////////// Id implementation /////////////////////////////////

inline Id::Id() : Entity(entity_t::id), type_(value_t::null), int_id_(0), string_id_("")
{
}

inline Id::Id(int id) : Entity(entity_t::id), type_(value_t::integer), int_id_(id), string_id_("")
{
}

inline Id::Id(const char* id) : Entity(entity_t::id), type_(value_t::string), int_id_(0), string_id_(id)
{
}

inline Id::Id(const std::string& id) : Id(id.c_str())
{
}

inline Id::Id(const Json& json_id) : Id()
{
    Id::parse_json(json_id);
}

inline void Id::parse_json(const Json& json)
{
    if (json.is_null())
    {
        type_ = value_t::null;
    }
    else if (json.is_number_integer())
    {
        int_id_ = json.get<int>();
        type_ = value_t::integer;
    }
    else if (json.is_string())
    {
        string_id_ = json.get<std::string>();
        type_ = value_t::string;
    }
    else
        throw std::invalid_argument("id must be integer, string or null");
}

inline Json Id::to_json() const
{
    if (type_ == value_t::null)
        return nullptr;
    if (type_ == value_t::string)
        return string_id_;
    if (type_ == value_t::integer)
        return int_id_;

    return nullptr;
}


//////////////////////// Error implementation /////////////////////////////////

inline Parameter::Parameter(std::nullptr_t) : NullableEntity(entity_t::id, nullptr), type(value_t::null)
{
}

inline Parameter::Parameter(const Json& json) : NullableEntity(entity_t::id), type(value_t::null)
{
    if (json != nullptr)
        Parameter::parse_json(json);
}

inline Parameter::Parameter(const std::string& key1, const Json& value1, const std::string& key2, const Json& value2, const std::string& key3,
                            const Json& value3, const std::string& key4, const Json& value4)
    : NullableEntity(entity_t::id), type(value_t::map)
{
    param_map[key1] = value1;
    if (!key2.empty())
        add(key2, value2);
    if (!key3.empty())
        add(key3, value3);
    if (!key4.empty())
        add(key4, value4);
}

inline void Parameter::add(const std::string& key, const Json& value)
{
    param_map[key] = value;
}

inline void Parameter::parse_json(const Json& json)
{
    if (json.is_null())
    {
        param_array.clear();
        param_map.clear();
        type = value_t::null;
        isNull = true;
    }
    else if (json.is_array())
    {
        param_array = json.get<std::vector<Json>>();
        param_map.clear();
        type = value_t::array;
    }
    else
    {
        param_map = json.get<std::map<std::string, Json>>();
        param_array.clear();
        type = value_t::map;
    }
}

inline Json Parameter::to_json() const
{
    if (type == value_t::array)
        return param_array;
    if (type == value_t::map)
        return param_map;

    return nullptr;
}

inline bool Parameter::is_array() const
{
    return type == value_t::array;
}

inline bool Parameter::is_map() const
{
    return type == value_t::map;
}

inline bool Parameter::is_null() const
{
    return type == value_t::null;
}

inline bool Parameter::has(const std::string& key) const
{
    if (type != value_t::map)
        return false;
    return (param_map.find(key) != param_map.end());
}

inline Json Parameter::get(const std::string& key) const
{
    if (has(key))
        return param_map.at(key);
    throw RpcException("Parameter '" + key + "' not found");
}

inline bool Parameter::has(size_t idx) const
{
    if (type != value_t::array)
        return false;
    return (param_array.size() > idx);
}

inline Json Parameter::get(size_t idx) const
{
    return param_array.at(idx);
}


//////////////////////// Error implementation /////////////////////////////////

inline Error::Error(const Json& json) : Error("Internal error", -32603, nullptr)
{
    if (json != nullptr)
        Error::parse_json(json);
}

inline Error::Error(std::nullptr_t) : NullableEntity(entity_t::error, nullptr), code_(0), message_(""), data_(nullptr)
{
}

inline Error::Error(const std::string& message, int code, const Json& data) : NullableEntity(entity_t::error), code_(code), message_(message), data_(data)
{
}

inline void Error::parse_json(const Json& json)
{
    try
    {
        if (json.count("code") == 0)
            throw RpcException("code is missing");
        code_ = json["code"];
        if (json.count("message") == 0)
            throw RpcException("message is missing");
        message_ = json["message"].get<std::string>();
        if (json.count("data") != 0u)
            data_ = json["data"];
        else
            data_ = nullptr;
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw RpcException(e.what());
    }
}

inline Json Error::to_json() const
{
    Json j = {{"code", code_}, {"message", message_}};

    if (!data_.is_null())
        j["data"] = data_;
    return j;
}


////////////////////// Request implementation /////////////////////////////////

inline Request::Request(const Json& json) : Entity(entity_t::request), method_(""), id_()
{
    if (json != nullptr)
        Request::parse_json(json);
}

inline Request::Request(const Id& id, const std::string& method, const Parameter& params) : Entity(entity_t::request), method_(method), params_(params), id_(id)
{
}

inline void Request::parse_json(const Json& json)
{
    try
    {
        if (json.count("id") == 0)
            throw InvalidRequestException("id is missing");

        try
        {
            id_ = Id(json["id"]);
        }
        catch (const std::exception& e)
        {
            throw InvalidRequestException(e.what());
        }

        if (json.count("jsonrpc") == 0)
            throw InvalidRequestException("jsonrpc is missing", id_);
        std::string jsonrpc = json["jsonrpc"].get<std::string>();
        if (jsonrpc != "2.0")
            throw InvalidRequestException("invalid jsonrpc value: " + jsonrpc, id_);

        if (json.count("method") == 0)
            throw InvalidRequestException("method is missing", id_);
        if (!json["method"].is_string())
            throw InvalidRequestException("method must be a string value", id_);
        method_ = json["method"].get<std::string>();
        if (method_.empty())
            throw InvalidRequestException("method must not be empty", id_);

        if (json.count("params") != 0u)
            params_.parse_json(json["params"]);
        else
            params_ = nullptr;
    }
    catch (const RequestException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw InternalErrorException(e.what(), id_);
    }
}

inline Json Request::to_json() const
{
    Json json = {{"jsonrpc", "2.0"}, {"method", method_}, {"id", id_.to_json()}};

    if (params_)
        json["params"] = params_.to_json();

    return json;
}


inline RpcException::RpcException(const char* text) : m_(text)
{
}

inline RpcException::RpcException(const std::string& text) : RpcException(text.c_str())
{
}

inline const char* RpcException::what() const noexcept
{
    return m_.what();
}


inline RpcEntityException::RpcEntityException(const Error& error) : RpcException(error.message()), Entity(entity_t::exception), error_(error)
{
}

inline void RpcEntityException::parse_json(const Json& /*json*/)
{
}


inline ParseErrorException::ParseErrorException(const Error& error) : RpcEntityException(error)
{
}

inline ParseErrorException::ParseErrorException(const std::string& data) : ParseErrorException(Error("Parse error", -32700, data))
{
}

inline Json ParseErrorException::to_json() const
{
    Json response = {{"jsonrpc", "2.0"}, {"error", error_.to_json()}, {"id", nullptr}};

    return response;
}


inline RequestException::RequestException(const Error& error, const Id& requestId) : RpcEntityException(error), id_(requestId)
{
}

inline Json RequestException::to_json() const
{
    Json response = {{"jsonrpc", "2.0"}, {"error", error_.to_json()}, {"id", id_.to_json()}};

    return response;
}


inline InvalidRequestException::InvalidRequestException(const Id& requestId) : RequestException(Error("Invalid request", -32600), requestId)
{
}

inline InvalidRequestException::InvalidRequestException(const Request& request) : InvalidRequestException(request.id())
{
}

inline InvalidRequestException::InvalidRequestException(const char* data, const Id& requestId)
    : RequestException(Error("Invalid request", -32600, data), requestId)
{
}

inline InvalidRequestException::InvalidRequestException(const std::string& data, const Id& requestId) : InvalidRequestException(data.c_str(), requestId)
{
}


inline MethodNotFoundException::MethodNotFoundException(const Id& requestId) : RequestException(Error("Method not found", -32601), requestId)
{
}

inline MethodNotFoundException::MethodNotFoundException(const Request& request) : MethodNotFoundException(request.id())
{
}

inline MethodNotFoundException::MethodNotFoundException(const char* data, const Id& requestId)
    : RequestException(Error("Method not found", -32601, data), requestId)
{
}

inline MethodNotFoundException::MethodNotFoundException(const std::string& data, const Id& requestId) : MethodNotFoundException(data.c_str(), requestId)
{
}


inline InvalidParamsException::InvalidParamsException(const Id& requestId) : RequestException(Error("Invalid params", -32602), requestId)
{
}

inline InvalidParamsException::InvalidParamsException(const Request& request) : InvalidParamsException(request.id())
{
}

inline InvalidParamsException::InvalidParamsException(const char* data, const Id& requestId)
    : RequestException(Error("Invalid params", -32602, data), requestId)
{
}

inline InvalidParamsException::InvalidParamsException(const std::string& data, const Id& requestId) : InvalidParamsException(data.c_str(), requestId)
{
}


inline InternalErrorException::InternalErrorException(const Id& requestId) : RequestException(Error("Internal error", -32603), requestId)
{
}

inline InternalErrorException::InternalErrorException(const Request& request) : InternalErrorException(request.id())
{
}

inline InternalErrorException::InternalErrorException(const char* data, const Id& requestId)
    : RequestException(Error("Internal error", -32603, data), requestId)
{
}

inline InternalErrorException::InternalErrorException(const std::string& data, const Id& requestId) : InternalErrorException(data.c_str(), requestId)
{
}


///////////////////// Response implementation /////////////////////////////////

inline Response::Response(const Json& json) : Entity(entity_t::response)
{
    if (json != nullptr)
        Response::parse_json(json);
}

inline Response::Response(const Id& id, const Json& result) : Entity(entity_t::response), id_(id), result_(result), error_(nullptr)
{
}

inline Response::Response(const Id& id, const Error& error) : Entity(entity_t::response), id_(id), result_(), error_(error)
{
}

inline Response::Response(const Request& request, const Json& result) : Response(request.id(), result)
{
}

inline Response::Response(const Request& request, const Error& error) : Response(request.id(), error)
{
}

inline Response::Response(const RequestException& exception) : Response(exception.id(), exception.error())
{
}

inline void Response::parse_json(const Json& json)
{
    try
    {
        error_ = nullptr;
        result_ = nullptr;
        if (json.count("jsonrpc") == 0)
            throw RpcException("jsonrpc is missing");
        std::string jsonrpc = json["jsonrpc"].get<std::string>();
        if (jsonrpc != "2.0")
            throw RpcException("invalid jsonrpc value: " + jsonrpc);
        if (json.count("id") == 0)
            throw RpcException("id is missing");
        id_ = Id(json["id"]);
        if (json.count("result") != 0u)
            result_ = json["result"];
        else if (json.count("error") != 0u)
            error_ = json["error"];
        else
            throw RpcException("response must contain result or error");
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw RpcException(e.what());
    }
}

inline Json Response::to_json() const
{
    Json j = {{"jsonrpc", "2.0"}, {"id", id_.to_json()}};

    if (error_)
        j["error"] = error_.to_json();
    else
        j["result"] = result_;

    return j;
}


///////////////// Notification implementation /////////////////////////////////

inline Notification::Notification(const Json& json) : Entity(entity_t::notification)
{
    if (json != nullptr)
        Notification::parse_json(json);
}

inline Notification::Notification(const char* method, const Parameter& params) : Entity(entity_t::notification), method_(method), params_(params)
{
}

inline Notification::Notification(const std::string& method, const Parameter& params) : Notification(method.c_str(), params)
{
}

inline void Notification::parse_json(const Json& json)
{
    try
    {
        if (json.count("jsonrpc") == 0)
            throw RpcException("jsonrpc is missing");
        std::string jsonrpc = json["jsonrpc"].get<std::string>();
        if (jsonrpc != "2.0")
            throw RpcException("invalid jsonrpc value: " + jsonrpc);

        if (json.count("method") == 0)
            throw RpcException("method is missing");
        if (!json["method"].is_string())
            throw RpcException("method must be a string value");
        method_ = json["method"].get<std::string>();
        if (method_.empty())
            throw RpcException("method must not be empty");

        if (json.count("params") != 0u)
            params_.parse_json(json["params"]);
        else
            params_ = nullptr;
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw RpcException(e.what());
    }
}

inline Json Notification::to_json() const
{
    Json json = {{"jsonrpc", "2.0"}, {"method", method_}};

    if (params_)
        json["params"] = params_.to_json();

    return json;
}


//////////////////////// Batch implementation /////////////////////////////////

inline Batch::Batch(const Json& json) : Entity(entity_t::batch)
{
    if (json != nullptr)
        Batch::parse_json(json);
}

inline void Batch::parse_json(const Json& json)
{
    //	cout << "Batch::parse: " << json.dump() << "\n";
    entities.clear();
    for (const auto& it : json)
    {
        //		cout << "x: " << it->dump() << "\n";
        entity_ptr entity(nullptr);
        try
        {
            entity = Parser::do_parse_json(it);
            if (!entity)
                entity = std::make_shared<Error>("Invalid Request", -32600);
        }
        catch (const RequestException& e)
        {
            entity = std::make_shared<RequestException>(e);
        }
        catch (const std::exception& e)
        {
            entity = std::make_shared<Error>(e.what(), -32600);
        }
        entities.push_back(entity);
    }
    if (entities.empty())
        throw InvalidRequestException();
}

inline Json Batch::to_json() const
{
    Json result;
    for (const auto& j : entities)
        result.push_back(j->to_json());
    return result;
}


//////////////////////// Parser implementation ////////////////////////////////

inline void Parser::register_notification_callback(const std::string& notification, notification_callback callback)
{
    if (callback)
        notification_callbacks_[notification] = callback;
}

inline void Parser::register_request_callback(const std::string& request, request_callback callback)
{
    if (callback)
        request_callbacks_[request] = callback;
}

inline entity_ptr Parser::parse(const std::string& json_str)
{
    // std::cout << "parse: " << json_str << "\n";
    entity_ptr entity = do_parse(json_str);
    if (entity && entity->is_notification())
    {
        notification_ptr notification = std::dynamic_pointer_cast<jsonrpcpp::Notification>(entity);
        if (notification_callbacks_.find(notification->method()) != notification_callbacks_.end())
        {
            notification_callback callback = notification_callbacks_[notification->method()];
            if (callback)
                callback(notification->params());
        }
    }
    else if (entity && entity->is_request())
    {
        request_ptr request = std::dynamic_pointer_cast<jsonrpcpp::Request>(entity);
        if (request_callbacks_.find(request->method()) != request_callbacks_.end())
        {
            request_callback callback = request_callbacks_[request->method()];
            if (callback)
            {
                jsonrpcpp::response_ptr response = callback(request->id(), request->params());
                if (response)
                    return response;
            }
        }
    }
    return entity;
}

inline entity_ptr Parser::parse_json(const Json& json)
{
    return do_parse_json(json);
}

inline entity_ptr Parser::do_parse(const std::string& json_str)
{
    try
    {
        return do_parse_json(Json::parse(json_str));
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw ParseErrorException(e.what());
    }
    catch (...)
    {
        return nullptr;
    }
}

inline entity_ptr Parser::do_parse_json(const Json& json)
{
    try
    {
        if (is_request(json))
            return std::make_shared<Request>(json);
        if (is_notification(json))
            return std::make_shared<Notification>(json);
        if (is_response(json))
            return std::make_shared<Response>(json);
        if (is_batch(json))
            return std::make_shared<Batch>(json);
    }
    catch (const RpcException&)
    {
        throw;
    }
    catch (const std::exception& e)
    {
        throw RpcException(e.what());
    }

    return nullptr;
}

inline bool Parser::is_request(const std::string& json_str)
{
    try
    {
        return is_request(Json::parse(json_str));
    }
    catch (const std::exception&)
    {
        return false;
    }
}

inline bool Parser::is_request(const Json& json)
{
    return ((json.count("method") != 0u) && (json.count("id") != 0u));
}

inline bool Parser::is_notification(const std::string& json_str)
{
    try
    {
        return is_notification(Json::parse(json_str));
    }
    catch (const std::exception&)
    {
        return false;
    }
}

inline bool Parser::is_notification(const Json& json)
{
    return ((json.count("method") != 0u) && (json.count("id") == 0));
}

inline bool Parser::is_response(const std::string& json_str)
{
    try
    {
        return is_response(Json::parse(json_str));
    }
    catch (const std::exception&)
    {
        return false;
    }
}

inline bool Parser::is_response(const Json& json)
{
    return (((json.count("result") != 0u) || (json.count("error") != 0u)) && (json.count("id") != 0u));
}

inline bool Parser::is_batch(const std::string& json_str)
{
    try
    {
        return is_batch(Json::parse(json_str));
    }
    catch (const std::exception&)
    {
        return false;
    }
}

inline bool Parser::is_batch(const Json& json)
{
    return (json.is_array());
}

} // namespace jsonrpcpp

#endif