fixed #31: only use spaces in pretty print
This commit is contained in:
		
							parent
							
								
									b30e9ee5f4
								
							
						
					
					
						commit
						7724d34741
					
				
					 4 changed files with 13 additions and 13 deletions
				
			
		| 
						 | 
					@ -132,7 +132,7 @@ You can also get a string representation (serialize):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```cpp
 | 
					```cpp
 | 
				
			||||||
// explicit conversion to string
 | 
					// explicit conversion to string
 | 
				
			||||||
std::string s = j.dump();    // {\"happy\": true, \"pi\": 3.141}
 | 
					std::string s = j.dump();    // {\"happy\":true,\"pi\":3.141}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// serialization with pretty printing
 | 
					// serialization with pretty printing
 | 
				
			||||||
std::cout << j.dump(4) << std::endl;
 | 
					std::cout << j.dump(4) << std::endl;
 | 
				
			||||||
| 
						 | 
					@ -142,8 +142,6 @@ std::cout << j.dump(4) << std::endl;
 | 
				
			||||||
// }
 | 
					// }
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The value of s could be `{"pi": 3.141, "happy": true}`, but the order of the entries in the object is not fixed.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
You can also use streams to serialize and deserialize:
 | 
					You can also use streams to serialize and deserialize:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```cpp
 | 
					```cpp
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -982,7 +982,7 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (i != value_.array->begin())
 | 
					                if (i != value_.array->begin())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    result += prettyPrint ? ",\n" : ", ";
 | 
					                    result += prettyPrint ? ",\n" : ",";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                result += indent() + i->dump(prettyPrint, indentStep, currentIndent);
 | 
					                result += indent() + i->dump(prettyPrint, indentStep, currentIndent);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -1017,10 +1017,11 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (i != value_.object->begin())
 | 
					                if (i != value_.object->begin())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    result += prettyPrint ? ",\n" : ", ";
 | 
					                    result += prettyPrint ? ",\n" : ",";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                result += indent() + "\"" + i->first + "\": " + i->second.dump(prettyPrint, indentStep,
 | 
					                result += indent() + "\"" + i->first + "\":" + (prettyPrint ? " " : "") + i->second.dump(
 | 
				
			||||||
                          currentIndent);
 | 
					                              prettyPrint, indentStep,
 | 
				
			||||||
 | 
					                              currentIndent);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // decrease indentation
 | 
					            // decrease indentation
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -530,7 +530,7 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (i != value_.array->begin())
 | 
					                if (i != value_.array->begin())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    result += prettyPrint ? ",\n" : ", ";
 | 
					                    result += prettyPrint ? ",\n" : ",";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                result += indent() + i->dump(prettyPrint, indentStep, currentIndent);
 | 
					                result += indent() + i->dump(prettyPrint, indentStep, currentIndent);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -565,10 +565,11 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (i != value_.object->begin())
 | 
					                if (i != value_.object->begin())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    result += prettyPrint ? ",\n" : ", ";
 | 
					                    result += prettyPrint ? ",\n" : ",";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                result += indent() + "\"" + i->first + "\": " + i->second.dump(prettyPrint, indentStep,
 | 
					                result += indent() + "\"" + i->first + "\":" + (prettyPrint ? " " : "") + i->second.dump(
 | 
				
			||||||
                          currentIndent);
 | 
					                              prettyPrint, indentStep,
 | 
				
			||||||
 | 
					                              currentIndent);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // decrease indentation
 | 
					            // decrease indentation
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1921,8 +1921,8 @@ TEST_CASE("Parser")
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        auto j23 = "{ \"a\": null, \"b\": true, \"c\": [1,2,3], \"d\": {\"a\": 0} }"_json;
 | 
					        auto j23 = "{ \"a\": null, \"b\": true, \"c\": [1,2,3], \"d\": {\"a\": 0} }"_json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        CHECK(j23.dump() == "{\"a\": null, \"b\": true, \"c\": [1, 2, 3], \"d\": {\"a\": 0}}");
 | 
					        CHECK(j23.dump() == "{\"a\":null,\"b\":true,\"c\":[1,2,3],\"d\":{\"a\":0}}");
 | 
				
			||||||
        CHECK(j23.dump(-1) == "{\"a\": null, \"b\": true, \"c\": [1, 2, 3], \"d\": {\"a\": 0}}");
 | 
					        CHECK(j23.dump(-1) == "{\"a\":null,\"b\":true,\"c\":[1,2,3],\"d\":{\"a\":0}}");
 | 
				
			||||||
        CHECK(j23.dump(0) ==
 | 
					        CHECK(j23.dump(0) ==
 | 
				
			||||||
              "{\n\"a\": null,\n\"b\": true,\n\"c\": [\n1,\n2,\n3\n],\n\"d\": {\n\"a\": 0\n}\n}");
 | 
					              "{\n\"a\": null,\n\"b\": true,\n\"c\": [\n1,\n2,\n3\n],\n\"d\": {\n\"a\": 0\n}\n}");
 | 
				
			||||||
        CHECK(j23.dump(4) ==
 | 
					        CHECK(j23.dump(4) ==
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue