1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker // This file defines utility functions for escaping strings suitable for JSON. 6*635a8641SAndroid Build Coastguard Worker 7*635a8641SAndroid Build Coastguard Worker #ifndef BASE_JSON_STRING_ESCAPE_H_ 8*635a8641SAndroid Build Coastguard Worker #define BASE_JSON_STRING_ESCAPE_H_ 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <string> 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h" 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker namespace base { 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker // Appends to |dest| an escaped version of |str|. Valid UTF-8 code units and 18*635a8641SAndroid Build Coastguard Worker // characters will pass through from the input to the output. Invalid code 19*635a8641SAndroid Build Coastguard Worker // units and characters will be replaced with the U+FFFD replacement character. 20*635a8641SAndroid Build Coastguard Worker // This function returns true if no replacement was necessary and false if 21*635a8641SAndroid Build Coastguard Worker // there was a lossy replacement. On return, |dest| will contain a valid UTF-8 22*635a8641SAndroid Build Coastguard Worker // JSON string. 23*635a8641SAndroid Build Coastguard Worker // 24*635a8641SAndroid Build Coastguard Worker // Non-printing control characters will be escaped as \uXXXX sequences for 25*635a8641SAndroid Build Coastguard Worker // readability. 26*635a8641SAndroid Build Coastguard Worker // 27*635a8641SAndroid Build Coastguard Worker // If |put_in_quotes| is true, then a leading and trailing double-quote mark 28*635a8641SAndroid Build Coastguard Worker // will be appended to |dest| as well. 29*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool EscapeJSONString(StringPiece str, 30*635a8641SAndroid Build Coastguard Worker bool put_in_quotes, 31*635a8641SAndroid Build Coastguard Worker std::string* dest); 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker // Performs a similar function to the UTF-8 StringPiece version above, 34*635a8641SAndroid Build Coastguard Worker // converting UTF-16 code units to UTF-8 code units and escaping non-printing 35*635a8641SAndroid Build Coastguard Worker // control characters. On return, |dest| will contain a valid UTF-8 JSON string. 36*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool EscapeJSONString(StringPiece16 str, 37*635a8641SAndroid Build Coastguard Worker bool put_in_quotes, 38*635a8641SAndroid Build Coastguard Worker std::string* dest); 39*635a8641SAndroid Build Coastguard Worker 40*635a8641SAndroid Build Coastguard Worker // Helper functions that wrap the above two functions but return the value 41*635a8641SAndroid Build Coastguard Worker // instead of appending. |put_in_quotes| is always true. 42*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string GetQuotedJSONString(StringPiece str); 43*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string GetQuotedJSONString(StringPiece16 str); 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker // Given an arbitrary byte string |str|, this will escape all non-ASCII bytes 46*635a8641SAndroid Build Coastguard Worker // as \uXXXX escape sequences. This function is *NOT* meant to be used with 47*635a8641SAndroid Build Coastguard Worker // Unicode strings and does not validate |str| as one. 48*635a8641SAndroid Build Coastguard Worker // 49*635a8641SAndroid Build Coastguard Worker // CAVEAT CALLER: The output of this function may not be valid JSON, since 50*635a8641SAndroid Build Coastguard Worker // JSON requires escape sequences to be valid UTF-16 code units. This output 51*635a8641SAndroid Build Coastguard Worker // will be mangled if passed to to the base::JSONReader, since the reader will 52*635a8641SAndroid Build Coastguard Worker // interpret it as UTF-16 and convert it to UTF-8. 53*635a8641SAndroid Build Coastguard Worker // 54*635a8641SAndroid Build Coastguard Worker // The output of this function takes the *appearance* of JSON but is not in 55*635a8641SAndroid Build Coastguard Worker // fact valid according to RFC 4627. 56*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string EscapeBytesAsInvalidJSONString(StringPiece str, 57*635a8641SAndroid Build Coastguard Worker bool put_in_quotes); 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker } // namespace base 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker #endif // BASE_JSON_STRING_ESCAPE_H_ 62