File indexing completed on 2024-05-20 22:39:45
0001 #ifndef CPPTL_JSON_H_INCLUDED
0002 #define CPPTL_JSON_H_INCLUDED
0003
0004 #include "forwards.h"
0005 #include <string>
0006 #include <vector>
0007
0008 #ifndef JSON_USE_CPPTL_SMALLMAP
0009 #include <map>
0010 #else
0011 #include <cpptl/smallmap.h>
0012 #endif
0013 #ifdef JSON_USE_CPPTL
0014 #include <cpptl/forwards.h>
0015 #endif
0016
0017
0018
0019
0020 namespace jsoncollector {
0021 namespace Json {
0022
0023
0024
0025 enum ValueType {
0026 nullValue = 0,
0027 intValue,
0028 uintValue,
0029 realValue,
0030 stringValue,
0031 booleanValue,
0032 arrayValue,
0033 objectValue
0034 };
0035
0036 enum CommentPlacement {
0037 commentBefore = 0,
0038 commentAfterOnSameLine,
0039 commentAfter,
0040 numberOfCommentPlacement
0041 };
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 class JSON_API StaticString {
0063 public:
0064 explicit StaticString(const char *czstring) : str_(czstring) {}
0065
0066 operator const char *() const { return str_; }
0067
0068 const char *c_str() const { return str_; }
0069
0070 private:
0071 const char *str_;
0072 };
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 class JSON_API Value {
0102 friend class ValueIteratorBase;
0103 #ifdef JSON_VALUE_USE_INTERNAL_MAP
0104 friend class ValueInternalLink;
0105 friend class ValueInternalMap;
0106 #endif
0107 public:
0108 typedef std::vector<std::string> Members;
0109 typedef ValueIterator iterator;
0110 typedef ValueConstIterator const_iterator;
0111 typedef Json::UInt UInt;
0112 typedef Json::Int Int;
0113 typedef UInt ArrayIndex;
0114
0115 static const Value null;
0116 static const Int minInt;
0117 static const Int maxInt;
0118 static const UInt maxUInt;
0119
0120 private:
0121 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
0122 #ifndef JSON_VALUE_USE_INTERNAL_MAP
0123 class CZString {
0124 public:
0125 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
0126 CZString(int index);
0127 CZString(const char *cstr, DuplicationPolicy allocate);
0128 CZString(const CZString &other);
0129 ~CZString();
0130 CZString &operator=(const CZString &other);
0131 bool operator<(const CZString &other) const;
0132 bool operator==(const CZString &other) const;
0133 int index() const;
0134 const char *c_str() const;
0135 bool isStaticString() const;
0136
0137 private:
0138 void swap(CZString &other);
0139 const char *cstr_;
0140 int index_;
0141 };
0142
0143 public:
0144 #ifndef JSON_USE_CPPTL_SMALLMAP
0145 typedef std::map<CZString, Value> ObjectValues;
0146 #else
0147 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
0148 #endif
0149 #endif
0150 #endif
0151
0152 public:
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168 Value(ValueType type = nullValue);
0169 Value(Int value);
0170 Value(UInt value);
0171 Value(double value);
0172 Value(const char *value);
0173 Value(const char *beginValue, const char *endValue);
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 Value(const StaticString &value);
0185 Value(const std::string &value);
0186 #ifdef JSON_USE_CPPTL
0187 Value(const CppTL::ConstString &value);
0188 #endif
0189 Value(bool value);
0190 Value(const Value &other);
0191 ~Value();
0192
0193 Value &operator=(const Value &other);
0194
0195
0196
0197 void swap(Value &other);
0198
0199 ValueType type() const;
0200
0201 bool operator<(const Value &other) const;
0202 bool operator<=(const Value &other) const;
0203 bool operator>=(const Value &other) const;
0204 bool operator>(const Value &other) const;
0205
0206 bool operator==(const Value &other) const;
0207 bool operator!=(const Value &other) const;
0208
0209 int compare(const Value &other);
0210
0211 const char *asCString() const;
0212 std::string asString() const;
0213 #ifdef JSON_USE_CPPTL
0214 CppTL::ConstString asConstString() const;
0215 #endif
0216 Int asInt() const;
0217 UInt asUInt() const;
0218 double asDouble() const;
0219 bool asBool() const;
0220
0221 bool isNull() const;
0222 bool isBool() const;
0223 bool isInt() const;
0224 bool isUInt() const;
0225 bool isIntegral() const;
0226 bool isDouble() const;
0227 bool isNumeric() const;
0228 bool isString() const;
0229 bool isArray() const;
0230 bool isObject() const;
0231
0232 bool isConvertibleTo(ValueType other) const;
0233
0234
0235 UInt size() const;
0236
0237
0238
0239 bool empty() const;
0240
0241
0242 bool operator!() const;
0243
0244
0245
0246
0247 void clear();
0248
0249
0250
0251
0252
0253
0254 void resize(UInt size);
0255
0256
0257
0258
0259
0260
0261 Value &operator[](UInt index);
0262
0263
0264
0265 const Value &operator[](UInt index) const;
0266
0267
0268 Value get(UInt index, const Value &defaultValue) const;
0269
0270 bool isValidIndex(UInt index) const;
0271
0272
0273
0274 Value &append(const Value &value);
0275
0276
0277 Value &operator[](const char *key);
0278
0279 const Value &operator[](const char *key) const;
0280
0281 Value &operator[](const std::string &key);
0282
0283 const Value &operator[](const std::string &key) const;
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 Value &operator[](const StaticString &key);
0296 #ifdef JSON_USE_CPPTL
0297
0298 Value &operator[](const CppTL::ConstString &key);
0299
0300 const Value &operator[](const CppTL::ConstString &key) const;
0301 #endif
0302
0303 Value get(const char *key, const Value &defaultValue) const;
0304
0305 Value get(const std::string &key, const Value &defaultValue) const;
0306 #ifdef JSON_USE_CPPTL
0307
0308 Value get(const CppTL::ConstString &key, const Value &defaultValue) const;
0309 #endif
0310
0311
0312
0313
0314
0315
0316 Value removeMember(const char *key);
0317
0318 Value removeMember(const std::string &key);
0319
0320
0321 bool isMember(const char *key) const;
0322
0323 bool isMember(const std::string &key) const;
0324 #ifdef JSON_USE_CPPTL
0325
0326 bool isMember(const CppTL::ConstString &key) const;
0327 #endif
0328
0329
0330
0331
0332
0333
0334 Members getMemberNames() const;
0335
0336
0337
0338
0339
0340
0341
0342 void setComment(const char *comment, CommentPlacement placement);
0343
0344 void setComment(const std::string &comment, CommentPlacement placement);
0345 bool hasComment(CommentPlacement placement) const;
0346
0347 std::string getComment(CommentPlacement placement) const;
0348
0349 std::string toStyledString() const;
0350
0351 const_iterator begin() const;
0352 const_iterator end() const;
0353
0354 iterator begin();
0355 iterator end();
0356
0357 private:
0358 Value &resolveReference(const char *key, bool isStatic);
0359
0360 #ifdef JSON_VALUE_USE_INTERNAL_MAP
0361 inline bool isItemAvailable() const { return itemIsUsed_ == 0; }
0362
0363 inline void setItemUsed(bool isUsed = true) { itemIsUsed_ = isUsed ? 1 : 0; }
0364
0365 inline bool isMemberNameStatic() const { return memberNameIsStatic_ == 0; }
0366
0367 inline void setMemberNameIsStatic(bool isStatic) { memberNameIsStatic_ = isStatic ? 1 : 0; }
0368 #endif
0369
0370 private:
0371 struct CommentInfo {
0372 CommentInfo();
0373 ~CommentInfo();
0374
0375 void setComment(const char *text);
0376
0377 char *comment_;
0378 };
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389 union ValueHolder {
0390 Int int_;
0391 UInt uint_;
0392 double real_;
0393 bool bool_;
0394 char *string_;
0395 #ifdef JSON_VALUE_USE_INTERNAL_MAP
0396 ValueInternalArray *array_;
0397 ValueInternalMap *map_;
0398 #else
0399 ObjectValues *map_;
0400 #endif
0401 } value_;
0402 ValueType type_ : 8;
0403 int allocated_ : 1;
0404 #ifdef JSON_VALUE_USE_INTERNAL_MAP
0405 unsigned int itemIsUsed_ : 1;
0406 int memberNameIsStatic_ : 1;
0407 #endif
0408 CommentInfo *comments_;
0409 };
0410
0411
0412
0413 class PathArgument {
0414 public:
0415 friend class Path;
0416
0417 PathArgument();
0418 PathArgument(UInt index);
0419 PathArgument(const char *key);
0420 PathArgument(const std::string &key);
0421
0422 private:
0423 enum Kind { kindNone = 0, kindIndex, kindKey };
0424 std::string key_;
0425 UInt index_;
0426 Kind kind_;
0427 };
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 class Path {
0441 public:
0442 Path(const std::string &path,
0443 const PathArgument &a1 = PathArgument(),
0444 const PathArgument &a2 = PathArgument(),
0445 const PathArgument &a3 = PathArgument(),
0446 const PathArgument &a4 = PathArgument(),
0447 const PathArgument &a5 = PathArgument());
0448
0449 const Value &resolve(const Value &root) const;
0450 Value resolve(const Value &root, const Value &defaultValue) const;
0451
0452 Value &make(Value &root) const;
0453
0454 private:
0455 typedef std::vector<const PathArgument *> InArgs;
0456 typedef std::vector<PathArgument> Args;
0457
0458 void makePath(const std::string &path, const InArgs &in);
0459 void addPathInArg(const std::string &path,
0460 const InArgs &in,
0461 InArgs::const_iterator &itInArg,
0462 PathArgument::Kind kind);
0463 void invalidPath(const std::string &path, int location);
0464
0465 Args args_;
0466 };
0467
0468
0469
0470
0471
0472
0473
0474
0475 class ValueAllocator {
0476 public:
0477 enum { unknown = (unsigned)-1 };
0478
0479 virtual ~ValueAllocator();
0480
0481 virtual char *makeMemberName(const char *memberName) const = 0;
0482 virtual void releaseMemberName(char *memberName) const = 0;
0483 virtual char *duplicateStringValue(const char *value, unsigned int length = unknown) const = 0;
0484 virtual void releaseStringValue(char *value) const = 0;
0485 };
0486
0487 #ifdef JSON_VALUE_USE_INTERNAL_MAP
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532 class JSON_API ValueMapAllocator {
0533 public:
0534 virtual ~ValueMapAllocator();
0535 virtual ValueInternalMap *newMap() = 0;
0536 virtual ValueInternalMap *newMapCopy(const ValueInternalMap &other) = 0;
0537 virtual void destructMap(ValueInternalMap *map) = 0;
0538 virtual ValueInternalLink *allocateMapBuckets(unsigned int size) = 0;
0539 virtual void releaseMapBuckets(ValueInternalLink *links) = 0;
0540 virtual ValueInternalLink *allocateMapLink() = 0;
0541 virtual void releaseMapLink(ValueInternalLink *link) = 0;
0542 };
0543
0544
0545
0546
0547 class JSON_API ValueInternalLink {
0548 public:
0549 enum { itemPerLink = 6 };
0550 enum InternalFlags { flagAvailable = 0, flagUsed = 1 };
0551
0552 ValueInternalLink();
0553
0554 ~ValueInternalLink();
0555
0556 Value items_[itemPerLink];
0557 char *keys_[itemPerLink];
0558 ValueInternalLink *previous_;
0559 ValueInternalLink *next_;
0560 };
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574 class JSON_API ValueInternalMap {
0575 friend class ValueIteratorBase;
0576 friend class Value;
0577
0578 public:
0579 typedef unsigned int HashKey;
0580 typedef unsigned int BucketIndex;
0581
0582 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
0583 struct IteratorState {
0584 IteratorState() : map_(0), link_(0), itemIndex_(0), bucketIndex_(0) {}
0585 ValueInternalMap *map_;
0586 ValueInternalLink *link_;
0587 BucketIndex itemIndex_;
0588 BucketIndex bucketIndex_;
0589 };
0590 #endif
0591
0592 ValueInternalMap();
0593 ValueInternalMap(const ValueInternalMap &other);
0594 ValueInternalMap &operator=(const ValueInternalMap &other);
0595 ~ValueInternalMap();
0596
0597 void swap(ValueInternalMap &other);
0598
0599 BucketIndex size() const;
0600
0601 void clear();
0602
0603 bool reserveDelta(BucketIndex growth);
0604
0605 bool reserve(BucketIndex newItemCount);
0606
0607 const Value *find(const char *key) const;
0608
0609 Value *find(const char *key);
0610
0611 Value &resolveReference(const char *key, bool isStatic);
0612
0613 void remove(const char *key);
0614
0615 void doActualRemove(ValueInternalLink *link, BucketIndex index, BucketIndex bucketIndex);
0616
0617 ValueInternalLink *&getLastLinkInBucket(BucketIndex bucketIndex);
0618
0619 Value &setNewItem(const char *key, bool isStatic, ValueInternalLink *link, BucketIndex index);
0620
0621 Value &unsafeAdd(const char *key, bool isStatic, HashKey hashedKey);
0622
0623 HashKey hash(const char *key) const;
0624
0625 int compare(const ValueInternalMap &other) const;
0626
0627 private:
0628 void makeBeginIterator(IteratorState &it) const;
0629 void makeEndIterator(IteratorState &it) const;
0630 static bool equals(const IteratorState &x, const IteratorState &other);
0631 static void increment(IteratorState &iterator);
0632 static void incrementBucket(IteratorState &iterator);
0633 static void decrement(IteratorState &iterator);
0634 static const char *key(const IteratorState &iterator);
0635 static const char *key(const IteratorState &iterator, bool &isStatic);
0636 static Value &value(const IteratorState &iterator);
0637 static int distance(const IteratorState &x, const IteratorState &y);
0638
0639 private:
0640 ValueInternalLink *buckets_;
0641 ValueInternalLink *tailLink_;
0642 BucketIndex bucketsSize_;
0643 BucketIndex itemCount_;
0644 };
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657 class JSON_API ValueInternalArray {
0658 friend class Value;
0659 friend class ValueIteratorBase;
0660
0661 public:
0662 enum { itemsPerPage = 8 };
0663 typedef Value::ArrayIndex ArrayIndex;
0664 typedef unsigned int PageIndex;
0665
0666 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
0667 struct IteratorState
0668 {
0669 IteratorState() : array_(0), currentPageIndex_(0), currentItemIndex_(0) {}
0670 ValueInternalArray *array_;
0671 Value **currentPageIndex_;
0672 unsigned int currentItemIndex_;
0673 };
0674 #endif
0675
0676 ValueInternalArray();
0677 ValueInternalArray(const ValueInternalArray &other);
0678 ValueInternalArray &operator=(const ValueInternalArray &other);
0679 ~ValueInternalArray();
0680 void swap(ValueInternalArray &other);
0681
0682 void clear();
0683 void resize(ArrayIndex newSize);
0684
0685 Value &resolveReference(ArrayIndex index);
0686
0687 Value *find(ArrayIndex index) const;
0688
0689 ArrayIndex size() const;
0690
0691 int compare(const ValueInternalArray &other) const;
0692
0693 private:
0694 static bool equals(const IteratorState &x, const IteratorState &other);
0695 static void increment(IteratorState &iterator);
0696 static void decrement(IteratorState &iterator);
0697 static Value &dereference(const IteratorState &iterator);
0698 static Value &unsafeDereference(const IteratorState &iterator);
0699 static int distance(const IteratorState &x, const IteratorState &y);
0700 static ArrayIndex indexOf(const IteratorState &iterator);
0701 void makeBeginIterator(IteratorState &it) const;
0702 void makeEndIterator(IteratorState &it) const;
0703 void makeIterator(IteratorState &it, ArrayIndex index) const;
0704
0705 void makeIndexValid(ArrayIndex index);
0706
0707 Value **pages_;
0708 ArrayIndex size_;
0709 PageIndex pageCount_;
0710 };
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771 class JSON_API ValueArrayAllocator {
0772 public:
0773 virtual ~ValueArrayAllocator();
0774 virtual ValueInternalArray *newArray() = 0;
0775 virtual ValueInternalArray *newArrayCopy(const ValueInternalArray &other) = 0;
0776 virtual void destructArray(ValueInternalArray *array) = 0;
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788 virtual void reallocateArrayPageIndex(Value **&indexes,
0789 ValueInternalArray::PageIndex &indexCount,
0790 ValueInternalArray::PageIndex minNewIndexCount) = 0;
0791 virtual void releaseArrayPageIndex(Value **indexes, ValueInternalArray::PageIndex indexCount) = 0;
0792 virtual Value *allocateArrayPage() = 0;
0793 virtual void releaseArrayPage(Value *value) = 0;
0794 };
0795 #endif
0796
0797
0798
0799
0800 class ValueIteratorBase {
0801 public:
0802 typedef unsigned int size_t;
0803 typedef int difference_type;
0804 typedef ValueIteratorBase SelfType;
0805
0806 ValueIteratorBase();
0807 #ifndef JSON_VALUE_USE_INTERNAL_MAP
0808 explicit ValueIteratorBase(const Value::ObjectValues::iterator ¤t);
0809 #else
0810 ValueIteratorBase(const ValueInternalArray::IteratorState &state);
0811 ValueIteratorBase(const ValueInternalMap::IteratorState &state);
0812 #endif
0813
0814 bool operator==(const SelfType &other) const { return isEqual(other); }
0815
0816 bool operator!=(const SelfType &other) const { return !isEqual(other); }
0817
0818 difference_type operator-(const SelfType &other) const { return computeDistance(other); }
0819
0820
0821 Value key() const;
0822
0823
0824 UInt index() const;
0825
0826
0827 const char *memberName() const;
0828
0829 protected:
0830 Value &deref() const;
0831
0832 void increment();
0833
0834 void decrement();
0835
0836 difference_type computeDistance(const SelfType &other) const;
0837
0838 bool isEqual(const SelfType &other) const;
0839
0840 void copy(const SelfType &other);
0841
0842 private:
0843 #ifndef JSON_VALUE_USE_INTERNAL_MAP
0844 Value::ObjectValues::iterator current_;
0845
0846 bool isNull_;
0847 #else
0848 union {
0849 ValueInternalArray::IteratorState array_;
0850 ValueInternalMap::IteratorState map_;
0851 } iterator_;
0852 bool isArray_;
0853 #endif
0854 };
0855
0856
0857
0858
0859 class ValueConstIterator : public ValueIteratorBase {
0860 friend class Value;
0861
0862 public:
0863 typedef unsigned int size_t;
0864 typedef int difference_type;
0865 typedef const Value &reference;
0866 typedef const Value *pointer;
0867 typedef ValueConstIterator SelfType;
0868
0869 ValueConstIterator();
0870
0871 private:
0872
0873
0874 #ifndef JSON_VALUE_USE_INTERNAL_MAP
0875 explicit ValueConstIterator(const Value::ObjectValues::iterator ¤t);
0876 #else
0877 ValueConstIterator(const ValueInternalArray::IteratorState &state);
0878 ValueConstIterator(const ValueInternalMap::IteratorState &state);
0879 #endif
0880 public:
0881 SelfType &operator=(const ValueIteratorBase &other);
0882
0883 SelfType operator++(int) {
0884 SelfType temp(*this);
0885 ++*this;
0886 return temp;
0887 }
0888
0889 SelfType operator--(int) {
0890 SelfType temp(*this);
0891 --*this;
0892 return temp;
0893 }
0894
0895 SelfType &operator--() {
0896 decrement();
0897 return *this;
0898 }
0899
0900 SelfType &operator++() {
0901 increment();
0902 return *this;
0903 }
0904
0905 reference operator*() const { return deref(); }
0906 };
0907
0908
0909
0910 class ValueIterator : public ValueIteratorBase {
0911 friend class Value;
0912
0913 public:
0914 typedef unsigned int size_t;
0915 typedef int difference_type;
0916 typedef Value &reference;
0917 typedef Value *pointer;
0918 typedef ValueIterator SelfType;
0919
0920 ValueIterator();
0921 ValueIterator(const ValueConstIterator &other);
0922 ValueIterator(const ValueIterator &other);
0923
0924 private:
0925
0926
0927 #ifndef JSON_VALUE_USE_INTERNAL_MAP
0928 explicit ValueIterator(const Value::ObjectValues::iterator ¤t);
0929 #else
0930 ValueIterator(const ValueInternalArray::IteratorState &state);
0931 ValueIterator(const ValueInternalMap::IteratorState &state);
0932 #endif
0933 public:
0934 SelfType &operator=(const SelfType &other);
0935
0936 SelfType operator++(int) {
0937 SelfType temp(*this);
0938 ++*this;
0939 return temp;
0940 }
0941
0942 SelfType operator--(int) {
0943 SelfType temp(*this);
0944 --*this;
0945 return temp;
0946 }
0947
0948 SelfType &operator--() {
0949 decrement();
0950 return *this;
0951 }
0952
0953 SelfType &operator++() {
0954 increment();
0955 return *this;
0956 }
0957
0958 reference operator*() const { return deref(); }
0959 };
0960
0961 }
0962 }
0963 #endif