allow push_back() and pop_back() calls on json_pointer
Putting pop_back() to public and creating a trivial push_back() method allows users of nlohmann::json_pointer to manipulate an existing json-pointer by adding or removing keys at the end. This is useful for traversing a JSON-instance and keeping track of its "absolute path" at any moment. In my case for a schema-validator error-handler.
This commit is contained in:
		
							parent
							
								
									e5753b14a8
								
							
						
					
					
						commit
						9225cf2f57
					
				
					 3 changed files with 74 additions and 2 deletions
				
			
		|  | @ -97,7 +97,6 @@ class json_pointer | ||||||
|         return res; |         return res; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   private: |  | ||||||
|     /*!
 |     /*!
 | ||||||
|     @brief remove and return last reference pointer |     @brief remove and return last reference pointer | ||||||
|     @throw out_of_range.405 if JSON pointer has no parent |     @throw out_of_range.405 if JSON pointer has no parent | ||||||
|  | @ -114,6 +113,16 @@ class json_pointer | ||||||
|         return last; |         return last; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /*!
 | ||||||
|  |     @brief remove and return last reference pointer | ||||||
|  |     @throw out_of_range.405 if JSON pointer has no parent | ||||||
|  |     */ | ||||||
|  |     void push_back(const std::string& tok) | ||||||
|  |     { | ||||||
|  |         reference_tokens.push_back(tok); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |   private: | ||||||
|     /// return whether pointer points to the root document
 |     /// return whether pointer points to the root document
 | ||||||
|     bool is_root() const noexcept |     bool is_root() const noexcept | ||||||
|     { |     { | ||||||
|  |  | ||||||
|  | @ -11885,7 +11885,6 @@ class json_pointer | ||||||
|         return res; |         return res; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|   private: |  | ||||||
|     /*!
 |     /*!
 | ||||||
|     @brief remove and return last reference pointer |     @brief remove and return last reference pointer | ||||||
|     @throw out_of_range.405 if JSON pointer has no parent |     @throw out_of_range.405 if JSON pointer has no parent | ||||||
|  | @ -11902,6 +11901,16 @@ class json_pointer | ||||||
|         return last; |         return last; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /*!
 | ||||||
|  |     @brief remove and return last reference pointer | ||||||
|  |     @throw out_of_range.405 if JSON pointer has no parent | ||||||
|  |     */ | ||||||
|  |     void push_back(const std::string& tok) | ||||||
|  |     { | ||||||
|  |         reference_tokens.push_back(tok); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |   private: | ||||||
|     /// return whether pointer points to the root document
 |     /// return whether pointer points to the root document
 | ||||||
|     bool is_root() const noexcept |     bool is_root() const noexcept | ||||||
|     { |     { | ||||||
|  |  | ||||||
|  | @ -459,4 +459,58 @@ TEST_CASE("JSON pointers") | ||||||
|             CHECK(j.is_object()); |             CHECK(j.is_object()); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     SECTION("push and pop") | ||||||
|  |     { | ||||||
|  |         const json j = | ||||||
|  |         { | ||||||
|  |             {"", "Hello"}, | ||||||
|  |             {"pi", 3.141}, | ||||||
|  |             {"happy", true}, | ||||||
|  |             {"name", "Niels"}, | ||||||
|  |             {"nothing", nullptr}, | ||||||
|  |             { | ||||||
|  |                 "answer", { | ||||||
|  |                     {"everything", 42} | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             {"list", {1, 0, 2}}, | ||||||
|  |             { | ||||||
|  |                 "object", { | ||||||
|  |                     {"currency", "USD"}, | ||||||
|  |                     {"value", 42.99}, | ||||||
|  |                     {"", "empty string"}, | ||||||
|  |                     {"/", "slash"}, | ||||||
|  |                     {"~", "tilde"}, | ||||||
|  |                     {"~1", "tilde1"} | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         // empty json_pointer returns the root JSON-object
 | ||||||
|  |         auto ptr = ""_json_pointer; | ||||||
|  |         CHECK(j[ptr] == j); | ||||||
|  | 
 | ||||||
|  |         // simple field access
 | ||||||
|  |         ptr.push_back("pi"); | ||||||
|  |         CHECK(j[ptr] == j["pi"]); | ||||||
|  | 
 | ||||||
|  |         ptr.pop_back(); | ||||||
|  |         CHECK(j[ptr] == j); | ||||||
|  | 
 | ||||||
|  |         // object and children access
 | ||||||
|  |         ptr.push_back("answer"); | ||||||
|  |         ptr.push_back("everything"); | ||||||
|  |         CHECK(j[ptr] == j["answer"]["everything"]); | ||||||
|  | 
 | ||||||
|  |         ptr.pop_back(); | ||||||
|  |         ptr.pop_back(); | ||||||
|  |         CHECK(j[ptr] == j); | ||||||
|  | 
 | ||||||
|  |         // push key which has to be encoded
 | ||||||
|  |         ptr.push_back("object"); | ||||||
|  |         ptr.push_back("/"); | ||||||
|  |         CHECK(j[ptr] == j["object"]["/"]); | ||||||
|  |         CHECK(ptr.to_string() == "/object/~1"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue