void Value::resize ( UInt newSize ) { JSON_ASSERT ( type_ == nullValue || type_ == arrayValue ); if ( type_ == nullValue ) *this = Value ( arrayValue ); #ifndef JSON_VALUE_USE_INTERNAL_MAP UInt oldSize = size (); if ( newSize == 0 ) clear (); else if ( newSize > oldSize ) (*this)[ newSize - 1 ]; else { for ( UInt index = newSize; index < oldSize; ++index ) value_.map_->erase ( index ); assert ( size () == newSize ); } #else value_.array_->resize ( newSize ); #endif }
void Value::clear () { JSON_ASSERT ( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); switch ( type_ ) { #ifndef JSON_VALUE_USE_INTERNAL_MAP case arrayValue: case objectValue: value_.map_->clear (); break; #else case arrayValue: value_.array_->clear (); break; case objectValue: value_.map_->clear (); break; #endif default: break; } }
Value Value::removeMember( const char* key ) { JSON_ASSERT( type_ == nullValue || type_ == objectValue ); if ( type_ == nullValue ) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP CZString actualKey( key, CZString::noDuplication ); ObjectValues::iterator it = value_.map_->find( actualKey ); if ( it == value_.map_->end() ) return null; Value old(it->second); value_.map_->erase(it); return old; #else Value *value = value_.map_->find( key ); if (value){ Value old(*value); value_.map_.remove( key ); return old; } else { return null; } #endif }
void ungetc() { if (m_last_ch != -1) { JSON_ASSERT(!m_ungot); m_ungot = true; } }
std::string valueToString( UInt value ) { char buffer[32]; char *current = buffer + sizeof(buffer); uintToString( value, current ); JSON_ASSERT( current >= buffer ); return current; }
Json& Json::Add(const std::string& key, const Json& value){ if(key.size() > 0 && value._kind != kNull){ if(_kind != kObject) JSON_ASSERT("json of object expected"); ((Object*) _data)->insert(make_pair(key, new Json(value))); } return *this; }
void Value::CommentInfo::setComment(const char *text) { if (comment_) valueAllocator()->releaseStringValue(comment_); JSON_ASSERT(text); JSON_ASSERT_MESSAGE(text[0] == '\0' || text[0] == '/', "Comments must start with /"); // It seems that /**/ style comments are acceptable as well. comment_ = valueAllocator()->duplicateStringValue(text); }
void * JSONMemory::json_realloc(void * ptr, size_t siz){ if (myrealloc){ #ifdef JSON_DEBUG //in debug mode, see if the malloc was successful void * result = myrealloc(ptr, siz); JSON_ASSERT(result, JSON_TEXT("out of memory")); return result; #else return myrealloc(ptr, (unsigned long)siz); #endif } #ifdef JSON_DEBUG //in debug mode, see if the malloc was successful void * result = realloc(ptr, siz); JSON_ASSERT(result, JSON_TEXT("out of memory")); return result; #else return realloc(ptr, siz); #endif }
JSONNode::json_iterator JSONNode::find_nocase(const json_string & name_t){ JSON_CHECK_INTERNAL(); JSON_ASSERT(type() == JSON_NODE, JSON_TEXT("finding a non-iteratable node")); makeUniqueInternal(); if (JSONNode ** res = internal -> at_nocase(name_t)){ return ptr_to_json_iterator(res); } return end(); }
Json* Json::Parser::ParseFalse(){ std::string sbool; for(int i = 0; i < 4; i++) sbool += NextChar(); if(sbool != "false"){ JSON_ASSERT("expect \"false\" "); return NULL; } else return new Json(false, kFalse); }
void Value::setComment(String comment, CommentPlacement placement) { if (!comment.empty() && (comment.back() == '\n')) { // Always discard trailing newline, to aid indentation. comment.pop_back(); } JSON_ASSERT(!comment.empty()); JSON_ASSERT_MESSAGE( comment[0] == '\0' || comment[0] == '/', "in Json::Value::setComment(): Comments must start with /"); comments_.set(placement, std::move(comment)); }
bool Value::CZString::operator==(const CZString& other) const { if (!cstr_) return index_ == other.index_; //return strcmp(cstr_, other.cstr_) == 0; // Assume both are strings. unsigned this_len = this->storage_.length_; unsigned other_len = other.storage_.length_; if (this_len != other_len) return false; JSON_ASSERT(this->cstr_ && other.cstr_); int comp = memcmp(this->cstr_, other.cstr_, this_len); return comp == 0; }
Json& Json::Remove(const size_t & n){ if(_kind != kArray) JSON_ASSERT("json of array expected"); Array & arr = *(Array*)_data; if(n >= 0 && n < arr.size()){ Array::iterator it = arr.begin() + n; delete *it; arr.erase(it); } return *this; }
Json* Json::Parser::ParseTrue(){ std::string sbool; sbool += ch; for(int i = 0; i < 3; i++) sbool += NextChar(); if(sbool != "true"){ JSON_ASSERT("expect \"true\" "); return NULL; } else return new Json(true, kTrue); }
void Value::CommentInfo::setComment(const char* text, size_t len) { if (comment_) { releaseStringValue(comment_, 0u); comment_ = 0; } JSON_ASSERT(text != 0); JSON_ASSERT_MESSAGE( text[0] == '\0' || text[0] == '/', "in Json::Value::setComment(): Comments must start with /"); // It seems that /**/ style comments are acceptable as well. comment_ = duplicateStringValue(text, len); }
Json& Json::Remove(const std::string & key){ if(_kind != kObject) JSON_ASSERT("json of object expected"); Object& obj = *(Object*)_data; Object::iterator it = obj.find(key); if (it != obj.end()) { delete it->second; obj.erase(it); } return *this; }
std::string valueToString( Int value ) { char buffer[32]; char *current = buffer + sizeof(buffer); bool isNegative = value < 0; if ( isNegative ) value = -value; uintToString( UInt(value), current ); if ( isNegative ) *--current = '-'; JSON_ASSERT( current >= buffer ); return current; }
struct watchman_watch_list * watchman_watch_list(struct watchman_connection *conn, struct watchman_error *error) { struct watchman_watch_list *res = NULL; struct watchman_watch_list *result = NULL; if (watchman_send_simple_command(conn, error, "watch-list", NULL)) { return NULL; } json_t *obj = watchman_read(conn, error); if (!obj) { return NULL; } JSON_ASSERT(json_is_object, obj, "Got bogus value from watch-list %s"); json_t *roots = json_object_get(obj, "roots"); JSON_ASSERT(json_is_array, roots, "Got bogus value from watch-list %s"); res = malloc(sizeof(*res)); int nr = json_array_size(roots); res->nr = 0; res->roots = calloc(nr, sizeof(*res->roots)); int i; for (i = 0; i < nr; ++i) { json_t *root = json_array_get(roots, i); JSON_ASSERT(json_is_string, root, "Got non-string root from watch-list %s"); res->nr++; res->roots[i] = strdup(json_string_value(root)); } result = res; res = NULL; done: if (res) { watchman_free_watch_list(res); } json_decref(obj); return result; }
bool Value::CZString::operator<(const CZString& other) const { if (!cstr_) return index_ < other.index_; //return strcmp(cstr_, other.cstr_) < 0; // Assume both are strings. unsigned this_len = this->storage_.length_; unsigned other_len = other.storage_.length_; unsigned min_len = (std::min)(this_len, other_len); JSON_ASSERT(this->cstr_ && other.cstr_); int comp = memcmp(this->cstr_, other.cstr_, min_len); if (comp < 0) return true; if (comp > 0) return false; return (this_len < other_len); }
char Json::Parser::NextChar(bool skip){ while (true) { read_pos++; if(read_pos >= buf.size()) JSON_ASSERT("unexpected end of input"); ch = buf[read_pos]; if (skip && (' ' == ch || '\t' == ch || '\n' == ch || '\r' == ch)) { ; } else break; } return ch; }
bool Value::operator<(const Value& other) const { int typeDelta = type() - other.type(); if (typeDelta) return typeDelta < 0 ? true : false; switch (type()) { case nullValue: return false; case intValue: return value_.int_ < other.value_.int_; case uintValue: return value_.uint_ < other.value_.uint_; case realValue: return value_.real_ < other.value_.real_; case booleanValue: return value_.bool_ < other.value_.bool_; case stringValue: { if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) { if (other.value_.string_) return true; else return false; } unsigned this_len; unsigned other_len; char const* this_str; char const* other_str; decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len, &this_str); decodePrefixedString(other.isAllocated(), other.value_.string_, &other_len, &other_str); unsigned min_len = std::min<unsigned>(this_len, other_len); JSON_ASSERT(this_str && other_str); int comp = memcmp(this_str, other_str, min_len); if (comp < 0) return true; if (comp > 0) return false; return (this_len < other_len); } case arrayValue: case objectValue: { int delta = int(value_.map_->size() - other.value_.map_->size()); if (delta) return delta < 0; return (*value_.map_) < (*other.value_.map_); } default: JSON_ASSERT_UNREACHABLE; } return false; // unreachable }
void StyledStreamWriter::writeArrayValue( const Value &value ) { unsigned size = value.size(); if ( size == 0 ) pushValue( "[]" ); else { bool isArrayMultiLine = isMultineArray( value ); if ( isArrayMultiLine ) { writeWithIndent( "[" ); indent(); bool hasChildValue = !childValues_.empty(); unsigned index =0; while ( true ) { const Value &childValue = value[index]; writeCommentBeforeValue( childValue ); if ( hasChildValue ) writeWithIndent( childValues_[index] ); else { writeIndent(); writeValue( childValue ); } if ( ++index == size ) { writeCommentAfterValueOnSameLine( childValue ); break; } *document_ << ","; writeCommentAfterValueOnSameLine( childValue ); } unindent(); writeWithIndent( "]" ); } else // output on a single line { JSON_ASSERT( childValues_.size() == size ); *document_ << "[ "; for ( unsigned index =0; index < size; ++index ) { if ( index > 0 ) *document_ << ", "; *document_ << childValues_[index]; } *document_ << " ]"; } } }
const Value &Value::operator[](const char *key) const { JSON_ASSERT(type_ == nullValue || type_ == objectValue); if (type_ == nullValue) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP CZString actualKey(key, CZString::noDuplication); ObjectValues::const_iterator it = value_.map_->find(actualKey); if (it == value_.map_->end()) return null; return (*it).second; #else const Value *value = value_.map_->find(key); return value ? *value : null; #endif }
const Value &Value::operator[](UInt index) const { JSON_ASSERT(type_ == nullValue || type_ == arrayValue); if (type_ == nullValue) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP CZString key(index); ObjectValues::const_iterator it = value_.map_->find(key); if (it == value_.map_->end()) return null; return (*it).second; #else Value *value = value_.array_->find(index); return value ? *value : null; #endif }
void Value::clear () { JSON_ASSERT ( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); switch ( type_ ) { case arrayValue: case objectValue: value_.map_->clear (); break; default: break; } }
const Value& Value::operator[] ( UInt index ) const { JSON_ASSERT ( type_ == nullValue || type_ == arrayValue ); if ( type_ == nullValue ) return null; CZString key ( index ); ObjectValues::const_iterator it = value_.map_->find ( key ); if ( it == value_.map_->end () ) return null; return (*it).second; }
const Value& Value::operator[] ( const char* key ) const { JSON_ASSERT ( type_ == nullValue || type_ == objectValue ); if ( type_ == nullValue ) return null; CZString actualKey ( key, CZString::noDuplication ); ObjectValues::const_iterator it = value_.map_->find ( actualKey ); if ( it == value_.map_->end () ) return null; return (*it).second; }
Value &Value::operator[](UInt index) { JSON_ASSERT(type_ == nullValue || type_ == arrayValue); if (type_ == nullValue) *this = Value(arrayValue); #ifndef JSON_VALUE_USE_INTERNAL_MAP CZString key(index); ObjectValues::iterator it = value_.map_->lower_bound(key); if (it != value_.map_->end() && (*it).first == key) return (*it).second; ObjectValues::value_type defaultValue(key, null); it = value_.map_->insert(it, defaultValue); return (*it).second; #else return value_.array_->resolveReference(index); #endif }
Value Value::removeMemberByQuery(const string& key, const string& value) { JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); if ( type_ == nullValue ) return null; for(ObjectValues::iterator it = value_.map_->begin(); it != value_.map_->end(); it++) { Value& sub = it->second; if(sub.isMember(key)) { if(sub[key].asString() == value) { Value old(it->second); value_.map_->erase(it); return old; } } } return null; }
void Value::resize(ArrayIndex newSize) { JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue, "in Json::Value::resize(): requires arrayValue"); if (type_ == nullValue) *this = Value(arrayValue); ArrayIndex oldSize = size(); if (newSize == 0) clear(); else if (newSize > oldSize) (*this)[newSize - 1]; else { for (ArrayIndex index = newSize; index < oldSize; ++index) { value_.map_->erase(index); } JSON_ASSERT(size() == newSize); } }