json test

Committer:
tgw
Date:
Fri Jan 26 06:05:31 2018 +0000
Revision:
0:2ee762ea11b3
json

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tgw 0:2ee762ea11b3 1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
tgw 0:2ee762ea11b3 2 // Distributed under MIT license, or public domain if desired and
tgw 0:2ee762ea11b3 3 // recognized in your jurisdiction.
tgw 0:2ee762ea11b3 4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
tgw 0:2ee762ea11b3 5
tgw 0:2ee762ea11b3 6 // included by json_value.cpp
tgw 0:2ee762ea11b3 7
tgw 0:2ee762ea11b3 8 namespace Json {
tgw 0:2ee762ea11b3 9
tgw 0:2ee762ea11b3 10 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 11 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 12 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 13 // class ValueIteratorBase
tgw 0:2ee762ea11b3 14 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 15 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 16 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 17
tgw 0:2ee762ea11b3 18 ValueIteratorBase::ValueIteratorBase()
tgw 0:2ee762ea11b3 19 : current_(), isNull_(true) {
tgw 0:2ee762ea11b3 20 }
tgw 0:2ee762ea11b3 21
tgw 0:2ee762ea11b3 22 ValueIteratorBase::ValueIteratorBase(
tgw 0:2ee762ea11b3 23 const Value::ObjectValues::iterator& current)
tgw 0:2ee762ea11b3 24 : current_(current), isNull_(false) {}
tgw 0:2ee762ea11b3 25
tgw 0:2ee762ea11b3 26 Value& ValueIteratorBase::deref() const {
tgw 0:2ee762ea11b3 27 return current_->second;
tgw 0:2ee762ea11b3 28 }
tgw 0:2ee762ea11b3 29
tgw 0:2ee762ea11b3 30 void ValueIteratorBase::increment() {
tgw 0:2ee762ea11b3 31 ++current_;
tgw 0:2ee762ea11b3 32 }
tgw 0:2ee762ea11b3 33
tgw 0:2ee762ea11b3 34 void ValueIteratorBase::decrement() {
tgw 0:2ee762ea11b3 35 --current_;
tgw 0:2ee762ea11b3 36 }
tgw 0:2ee762ea11b3 37
tgw 0:2ee762ea11b3 38 ValueIteratorBase::difference_type
tgw 0:2ee762ea11b3 39 ValueIteratorBase::computeDistance(const SelfType& other) const {
tgw 0:2ee762ea11b3 40 #ifdef JSON_USE_CPPTL_SMALLMAP
tgw 0:2ee762ea11b3 41 return other.current_ - current_;
tgw 0:2ee762ea11b3 42 #else
tgw 0:2ee762ea11b3 43 // Iterator for null value are initialized using the default
tgw 0:2ee762ea11b3 44 // constructor, which initialize current_ to the default
tgw 0:2ee762ea11b3 45 // std::map::iterator. As begin() and end() are two instance
tgw 0:2ee762ea11b3 46 // of the default std::map::iterator, they can not be compared.
tgw 0:2ee762ea11b3 47 // To allow this, we handle this comparison specifically.
tgw 0:2ee762ea11b3 48 if (isNull_ && other.isNull_) {
tgw 0:2ee762ea11b3 49 return 0;
tgw 0:2ee762ea11b3 50 }
tgw 0:2ee762ea11b3 51
tgw 0:2ee762ea11b3 52 // Usage of std::distance is not portable (does not compile with Sun Studio 12
tgw 0:2ee762ea11b3 53 // RogueWave STL,
tgw 0:2ee762ea11b3 54 // which is the one used by default).
tgw 0:2ee762ea11b3 55 // Using a portable hand-made version for non random iterator instead:
tgw 0:2ee762ea11b3 56 // return difference_type( std::distance( current_, other.current_ ) );
tgw 0:2ee762ea11b3 57 difference_type myDistance = 0;
tgw 0:2ee762ea11b3 58 for (Value::ObjectValues::iterator it = current_; it != other.current_;
tgw 0:2ee762ea11b3 59 ++it) {
tgw 0:2ee762ea11b3 60 ++myDistance;
tgw 0:2ee762ea11b3 61 }
tgw 0:2ee762ea11b3 62 return myDistance;
tgw 0:2ee762ea11b3 63 #endif
tgw 0:2ee762ea11b3 64 }
tgw 0:2ee762ea11b3 65
tgw 0:2ee762ea11b3 66 bool ValueIteratorBase::isEqual(const SelfType& other) const {
tgw 0:2ee762ea11b3 67 if (isNull_) {
tgw 0:2ee762ea11b3 68 return other.isNull_;
tgw 0:2ee762ea11b3 69 }
tgw 0:2ee762ea11b3 70 return current_ == other.current_;
tgw 0:2ee762ea11b3 71 }
tgw 0:2ee762ea11b3 72
tgw 0:2ee762ea11b3 73 void ValueIteratorBase::copy(const SelfType& other) {
tgw 0:2ee762ea11b3 74 current_ = other.current_;
tgw 0:2ee762ea11b3 75 isNull_ = other.isNull_;
tgw 0:2ee762ea11b3 76 }
tgw 0:2ee762ea11b3 77
tgw 0:2ee762ea11b3 78 Value ValueIteratorBase::key() const {
tgw 0:2ee762ea11b3 79 const Value::CZString czstring = (*current_).first;
tgw 0:2ee762ea11b3 80 if (czstring.data()) {
tgw 0:2ee762ea11b3 81 if (czstring.isStaticString())
tgw 0:2ee762ea11b3 82 return Value(StaticString(czstring.data()));
tgw 0:2ee762ea11b3 83 return Value(czstring.data(), czstring.data() + czstring.length());
tgw 0:2ee762ea11b3 84 }
tgw 0:2ee762ea11b3 85 return Value(czstring.index());
tgw 0:2ee762ea11b3 86 }
tgw 0:2ee762ea11b3 87
tgw 0:2ee762ea11b3 88 UInt ValueIteratorBase::index() const {
tgw 0:2ee762ea11b3 89 const Value::CZString czstring = (*current_).first;
tgw 0:2ee762ea11b3 90 if (!czstring.data())
tgw 0:2ee762ea11b3 91 return czstring.index();
tgw 0:2ee762ea11b3 92 return Value::UInt(-1);
tgw 0:2ee762ea11b3 93 }
tgw 0:2ee762ea11b3 94
tgw 0:2ee762ea11b3 95 JSONCPP_STRING ValueIteratorBase::name() const {
tgw 0:2ee762ea11b3 96 char const* keey;
tgw 0:2ee762ea11b3 97 char const* end;
tgw 0:2ee762ea11b3 98 keey = memberName(&end);
tgw 0:2ee762ea11b3 99 if (!keey) return JSONCPP_STRING();
tgw 0:2ee762ea11b3 100 return JSONCPP_STRING(keey, end);
tgw 0:2ee762ea11b3 101 }
tgw 0:2ee762ea11b3 102
tgw 0:2ee762ea11b3 103 char const* ValueIteratorBase::memberName() const {
tgw 0:2ee762ea11b3 104 const char* cname = (*current_).first.data();
tgw 0:2ee762ea11b3 105 return cname ? cname : "";
tgw 0:2ee762ea11b3 106 }
tgw 0:2ee762ea11b3 107
tgw 0:2ee762ea11b3 108 char const* ValueIteratorBase::memberName(char const** end) const {
tgw 0:2ee762ea11b3 109 const char* cname = (*current_).first.data();
tgw 0:2ee762ea11b3 110 if (!cname) {
tgw 0:2ee762ea11b3 111 *end = NULL;
tgw 0:2ee762ea11b3 112 return NULL;
tgw 0:2ee762ea11b3 113 }
tgw 0:2ee762ea11b3 114 *end = cname + (*current_).first.length();
tgw 0:2ee762ea11b3 115 return cname;
tgw 0:2ee762ea11b3 116 }
tgw 0:2ee762ea11b3 117
tgw 0:2ee762ea11b3 118 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 119 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 120 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 121 // class ValueConstIterator
tgw 0:2ee762ea11b3 122 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 123 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 124 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 125
tgw 0:2ee762ea11b3 126 ValueConstIterator::ValueConstIterator() {}
tgw 0:2ee762ea11b3 127
tgw 0:2ee762ea11b3 128 ValueConstIterator::ValueConstIterator(
tgw 0:2ee762ea11b3 129 const Value::ObjectValues::iterator& current)
tgw 0:2ee762ea11b3 130 : ValueIteratorBase(current) {}
tgw 0:2ee762ea11b3 131
tgw 0:2ee762ea11b3 132 ValueConstIterator::ValueConstIterator(ValueIterator const& other)
tgw 0:2ee762ea11b3 133 : ValueIteratorBase(other) {}
tgw 0:2ee762ea11b3 134
tgw 0:2ee762ea11b3 135 ValueConstIterator& ValueConstIterator::
tgw 0:2ee762ea11b3 136 operator=(const ValueIteratorBase& other) {
tgw 0:2ee762ea11b3 137 copy(other);
tgw 0:2ee762ea11b3 138 return *this;
tgw 0:2ee762ea11b3 139 }
tgw 0:2ee762ea11b3 140
tgw 0:2ee762ea11b3 141 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 142 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 143 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 144 // class ValueIterator
tgw 0:2ee762ea11b3 145 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 146 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 147 // //////////////////////////////////////////////////////////////////
tgw 0:2ee762ea11b3 148
tgw 0:2ee762ea11b3 149 ValueIterator::ValueIterator() {}
tgw 0:2ee762ea11b3 150
tgw 0:2ee762ea11b3 151 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
tgw 0:2ee762ea11b3 152 : ValueIteratorBase(current) {}
tgw 0:2ee762ea11b3 153
tgw 0:2ee762ea11b3 154 ValueIterator::ValueIterator(const ValueConstIterator& other)
tgw 0:2ee762ea11b3 155 : ValueIteratorBase(other) {
tgw 0:2ee762ea11b3 156 throwRuntimeError("ConstIterator to Iterator should never be allowed.");
tgw 0:2ee762ea11b3 157 }
tgw 0:2ee762ea11b3 158
tgw 0:2ee762ea11b3 159 ValueIterator::ValueIterator(const ValueIterator& other)
tgw 0:2ee762ea11b3 160 : ValueIteratorBase(other) {}
tgw 0:2ee762ea11b3 161
tgw 0:2ee762ea11b3 162 ValueIterator& ValueIterator::operator=(const SelfType& other) {
tgw 0:2ee762ea11b3 163 copy(other);
tgw 0:2ee762ea11b3 164 return *this;
tgw 0:2ee762ea11b3 165 }
tgw 0:2ee762ea11b3 166
tgw 0:2ee762ea11b3 167 } // namespace Json