15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file defines utility functions for escaping strings suitable for JSON. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_JSON_STRING_ESCAPE_H_ 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_JSON_STRING_ESCAPE_H_ 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h" 1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/strings/string_piece.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Appends to |dest| an escaped version of |str|. Valid UTF-8 code units will 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// pass through from the input to the output. Invalid code units will be 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// replaced with the U+FFFD replacement character. This function returns true 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// if no replacement was necessary and false if there was a lossy replacement. 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// On return, |dest| will contain a valid UTF-8 JSON string. 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Non-printing control characters will be escaped as \uXXXX sequences for 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// readability. 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// If |put_in_quotes| is true, then a leading and trailing double-quote mark 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// will be appended to |dest| as well. 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT bool EscapeJSONString(const StringPiece& str, 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool put_in_quotes, 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string* dest); 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Performs a similar function to the UTF-8 StringPiece version above, 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// converting UTF-16 code units to UTF-8 code units and escaping non-printing 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// control characters. On return, |dest| will contain a valid UTF-8 JSON string. 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT bool EscapeJSONString(const StringPiece16& str, 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool put_in_quotes, 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string* dest); 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Helper functions that wrap the above two functions but return the value 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// instead of appending. |put_in_quotes| is always true. 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT std::string GetQuotedJSONString(const StringPiece& str); 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT std::string GetQuotedJSONString(const StringPiece16& str); 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Given an arbitrary byte string |str|, this will escape all non-ASCII bytes 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// as \uXXXX escape sequences. This function is *NOT* meant to be used with 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Unicode strings and does not validate |str| as one. 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// CAVEAT CALLER: The output of this function may not be valid JSON, since 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// JSON requires escape sequences to be valid UTF-16 code units. This output 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// will be mangled if passed to to the base::JSONReader, since the reader will 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// interpret it as UTF-16 and convert it to UTF-8. 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The output of this function takes the *appearance* of JSON but is not in 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// fact valid according to RFC 4627. 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT std::string EscapeBytesAsInvalidJSONString(const StringPiece& str, 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool put_in_quotes); 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // BASE_JSON_STRING_ESCAPE_H_ 61