BSON: Support empty objects
This commit is contained in:
		
							parent
							
								
									f06c8fd8e3
								
							
						
					
					
						commit
						5f5836ce1c
					
				
					 4 changed files with 124 additions and 33 deletions
				
			
		|  | @ -127,25 +127,18 @@ class binary_reader | |||
| 
 | ||||
|     bool parse_bson_internal() | ||||
|     { | ||||
|         int docLen = 0; | ||||
|         int byte; | ||||
|         for (int i = 0; i < 4; ++i) | ||||
|         std::int32_t documentSize; | ||||
|         get_number_little_endian(documentSize); | ||||
| 
 | ||||
|         if (not JSON_UNLIKELY(sax->start_object(documentSize - 5))) | ||||
|         { | ||||
|             byte = get(); | ||||
|             if (JSON_UNLIKELY(current == std::char_traits<char>::eof())) | ||||
|             { | ||||
|                 if (i == 1) | ||||
|                 { | ||||
|                     return sax->boolean(docLen != 0x00); | ||||
|                 } | ||||
|                 return false; | ||||
|             } | ||||
|             docLen |= static_cast<std::int32_t>(byte) << 8 * i; | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         //sax->null();
 | ||||
|         const auto result = sax->end_object(); | ||||
| 
 | ||||
|         get(); | ||||
|         return true; | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     /*!
 | ||||
|  | @ -927,6 +920,35 @@ class binary_reader | |||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     template<typename NumberType> | ||||
|     bool get_number_little_endian(NumberType& result) | ||||
|     { | ||||
|         // step 1: read input into array with system's byte order
 | ||||
|         std::array<uint8_t, sizeof(NumberType)> vec; | ||||
|         for (std::size_t i = 0; i < sizeof(NumberType); ++i) | ||||
|         { | ||||
|             get(); | ||||
|             if (JSON_UNLIKELY(not unexpect_eof())) | ||||
|             { | ||||
|                 return false; | ||||
|             } | ||||
| 
 | ||||
|             // reverse byte order prior to conversion if necessary
 | ||||
|             if (!is_little_endian) | ||||
|             { | ||||
|                 vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
 | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // step 2: convert array into number of type T and return
 | ||||
|         std::memcpy(&result, vec.data(), sizeof(NumberType)); | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     /*!
 | ||||
|     @brief create a string by reading characters from the input | ||||
| 
 | ||||
|  |  | |||
|  | @ -684,7 +684,8 @@ class binary_writer | |||
|     void write_bson_object(const BasicJsonType& j) | ||||
|     { | ||||
|         assert(j.type() == value_t::object); | ||||
| 
 | ||||
|         write_number_little_endian(5); | ||||
|         oa->write_character(static_cast<CharType>(0x00)); | ||||
|     } | ||||
| 
 | ||||
|     /*!
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue