| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
| license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 4 | |
| 5 | // This file defines utility functions for escaping strings suitable for JSON. |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
| [email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 7 | #ifndef BASE_JSON_STRING_ESCAPE_H_ |
| 8 | #define BASE_JSON_STRING_ESCAPE_H_ |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | |
| [email protected] | d36519b | 2009-05-20 16:43:49 | [diff] [blame] | 10 | #include <string> |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 11 | #include <string_view> |
| [email protected] | d36519b | 2009-05-20 16:43:49 | [diff] [blame] | 12 | |
| [email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
| [email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 15 | namespace base { |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
| sabbakumov | 51d82b24 | 2017-06-13 04:28:13 | [diff] [blame] | 17 | // Appends to |dest| an escaped version of |str|. Valid UTF-8 code units and |
| 18 | // characters will pass through from the input to the output. Invalid code |
| 19 | // units and characters will be replaced with the U+FFFD replacement character. |
| 20 | // This function returns true if no replacement was necessary and false if |
| 21 | // there was a lossy replacement. On return, |dest| will contain a valid UTF-8 |
| 22 | // JSON string. |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 23 | // |
| 24 | // Non-printing control characters will be escaped as \uXXXX sequences for |
| 25 | // readability. |
| 26 | // |
| 27 | // If |put_in_quotes| is true, then a leading and trailing double-quote mark |
| 28 | // will be appended to |dest| as well. |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 29 | BASE_EXPORT bool EscapeJSONString(std::string_view str, |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 30 | bool put_in_quotes, |
| 31 | std::string* dest); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 33 | // Performs a similar function to the UTF-8 std::string_view version above, |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 34 | // converting UTF-16 code units to UTF-8 code units and escaping non-printing |
| 35 | // control characters. On return, |dest| will contain a valid UTF-8 JSON string. |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 36 | BASE_EXPORT bool EscapeJSONString(std::u16string_view str, |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 37 | bool put_in_quotes, |
| 38 | std::string* dest); |
| [email protected] | a9602de | 2010-03-18 23:43:11 | [diff] [blame] | 39 | |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 40 | // Helper functions that wrap the above two functions but return the value |
| 41 | // instead of appending. |put_in_quotes| is always true. |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 42 | BASE_EXPORT std::string GetQuotedJSONString(std::string_view str); |
| 43 | BASE_EXPORT std::string GetQuotedJSONString(std::u16string_view str); |
| [email protected] | d36519b | 2009-05-20 16:43:49 | [diff] [blame] | 44 | |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 45 | // Given an arbitrary byte string |str|, this will escape all non-ASCII bytes |
| 46 | // as \uXXXX escape sequences. This function is *NOT* meant to be used with |
| 47 | // Unicode strings and does not validate |str| as one. |
| 48 | // |
| 49 | // CAVEAT CALLER: The output of this function may not be valid JSON, since |
| 50 | // JSON requires escape sequences to be valid UTF-16 code units. This output |
| 51 | // will be mangled if passed to to the base::JSONReader, since the reader will |
| 52 | // interpret it as UTF-16 and convert it to UTF-8. |
| 53 | // |
| 54 | // The output of this function takes the *appearance* of JSON but is not in |
| Nigel Tao | 71c958db | 2020-04-09 23:18:44 | [diff] [blame] | 55 | // fact valid according to RFC 8259. |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 56 | BASE_EXPORT std::string EscapeBytesAsInvalidJSONString(std::string_view str, |
| [email protected] | bbe1571 | 2013-12-11 22:10:45 | [diff] [blame] | 57 | bool put_in_quotes); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | |
| [email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 59 | } // namespace base |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | |
| [email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 61 | #endif // BASE_JSON_STRING_ESCAPE_H_ |