VError VJSONWriter::StringifyArray( const VJSONArray *inArray, VString& outString) { VError err = VE_OK; if (inArray == NULL) { outString = VJSONValue::sUndefinedString; } else if (inArray->IsEmpty()) { outString = "[]"; } else { IncrementLevel(); VectorOfVString array; size_t count = inArray->GetCount(); try { array.resize( count); } catch(...) { err = vThrowError( VE_MEMORY_FULL); } VectorOfVString::iterator j = array.begin(); for( size_t i = 0 ; (i < count) && (err == VE_OK) ; ++i, ++j) { err = StringifyValue( (*inArray)[i], *j); InsertIndentString( *j); } DecrementLevel(); if (err == VE_OK) { array.front().Insert( '[', 1); AppendIndentString( array.back()); array.back().AppendUniChar( ']'); err = outString.Join( array, ',') ? VE_OK : vThrowError( VE_STRING_ALLOC_FAILED); } } return err; }
VError VJSONWriter::StringifyObject( const VJSONObject *inObject, VString& outString) { if (inObject == NULL) { outString = VJSONValue::sUndefinedString; return VE_OK; } if (std::find( fStack.begin(), fStack.end(), inObject) != fStack.end()) { return vThrowError( VE_JSON_STRINGIFY_CIRCULAR); } VError err = VE_OK; fStack.push_back( inObject); if (!inObject->DoStringify( outString, *this, &err)) { if (inObject->fMap.empty()) { outString = "{}"; } else { IncrementLevel(); VectorOfVString array; array.resize( inObject->fMap.size()); VectorOfVString::iterator j = array.begin(); for( VJSONPropertyConstIterator i( inObject) ; i.IsValid() && (err == VE_OK) ; ++i, ++j) { err = i.GetName().GetJSONString( *j, GetOptions()); if (err == VE_OK) { InsertIndentString( *j); VString value; err = StringifyValue( i.GetValue(), value); if (err == VE_OK) { j->AppendUniChar( ':'); j->AppendString( value); } } } DecrementLevel(); if (err == VE_OK) { array.front().Insert( '{', 1); AppendIndentString( array.back()); array.back().AppendUniChar( '}'); err = outString.Join( array, ',') ? VE_OK : vThrowError( VE_STRING_ALLOC_FAILED); } } } fStack.pop_back(); return err; }