mirror of
				https://github.com/open-source-parsers/jsoncpp.git
				synced 2025-11-03 19:40:36 +01:00 
			
		
		
		
	[1611376]writer now escapes special characters. When no special chars are present, old behavior is retained. New method might have a performance penalty b/c of operator new inside std::string. (This would not exist if the whole thing operated on ostream instead, I think.)
This commit is contained in:
		@@ -560,6 +560,7 @@ Reader::decodeString( Token &token, std::string &decoded )
 | 
			
		||||
               if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) )
 | 
			
		||||
                  return false;
 | 
			
		||||
               // @todo encode unicode as utf8.
 | 
			
		||||
	       // @todo remember to alter the writer too.
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
         default:
 | 
			
		||||
 
 | 
			
		||||
@@ -64,7 +64,45 @@ std::string valueToString( bool value )
 | 
			
		||||
 | 
			
		||||
std::string valueToQuotedString( const char *value )
 | 
			
		||||
{
 | 
			
		||||
   return std::string("\"") + value + "\"";
 | 
			
		||||
   // Not sure how to handle unicode...
 | 
			
		||||
   if (std::strpbrk(value, "\"\\\b\f\n\r\t") == NULL)
 | 
			
		||||
      return std::string("\"") + value + "\"";
 | 
			
		||||
   // We have to walk value and escape any special characters.
 | 
			
		||||
   // Appending to std::string is not efficient, but this should be rare.
 | 
			
		||||
   // (Note: forward slashes are *not* rare, but I am not escaping them.)
 | 
			
		||||
   std::string result("\"");
 | 
			
		||||
   for (const char* c=value; *c != 0; ++c){
 | 
			
		||||
      switch(*c){
 | 
			
		||||
         case '\"':
 | 
			
		||||
	 result += "\\\"";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\\':
 | 
			
		||||
	 result += "\\\\";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\b':
 | 
			
		||||
	 result += "\\b";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\f':
 | 
			
		||||
	 result += "\\f";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\n':
 | 
			
		||||
	 result += "\\n";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\r':
 | 
			
		||||
	 result += "\\r";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '\t':
 | 
			
		||||
	 result += "\\t";
 | 
			
		||||
	 break;
 | 
			
		||||
	 case '/':
 | 
			
		||||
	 // Even though \/ is considered a legal escape in JSON, a bare
 | 
			
		||||
	 // slash is also legal, so I see no reason to escape it.
 | 
			
		||||
	 // (I hope I am not misunderstanding something.)
 | 
			
		||||
	 default:
 | 
			
		||||
	    result += *c;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
   return result + "\"";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Class Writer
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user