1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_JSON_VALUES_UTIL_H_ 6 #define BASE_JSON_VALUES_UTIL_H_ 7 8 #include <optional> 9 10 #include "base/base_export.h" 11 #include "base/values.h" 12 13 namespace base { 14 class FilePath; 15 class Time; 16 class TimeDelta; 17 class UnguessableToken; 18 19 // Simple helper functions for converting between Value and other types. 20 // The Value representation is stable, suitable for persistent storage 21 // e.g. as JSON on disk. 22 // 23 // It is valid to pass nullptr to the ValueToEtc functions. They will just 24 // return std::nullopt. 25 26 // Converts between an int64_t and a string-flavored Value (a human 27 // readable string of that number). 28 BASE_EXPORT Value Int64ToValue(int64_t integer); 29 BASE_EXPORT std::optional<int64_t> ValueToInt64(const Value* value); 30 BASE_EXPORT std::optional<int64_t> ValueToInt64(const Value& value); 31 32 // Converts between a TimeDelta (an int64_t number of microseconds) and a 33 // string-flavored Value (a human readable string of that number). 34 BASE_EXPORT Value TimeDeltaToValue(TimeDelta time_delta); 35 BASE_EXPORT std::optional<TimeDelta> ValueToTimeDelta(const Value* value); 36 BASE_EXPORT std::optional<TimeDelta> ValueToTimeDelta(const Value& value); 37 38 // Converts between a Time (an int64_t number of microseconds since the 39 // Windows epoch) and a string-flavored Value (a human readable string of 40 // that number). 41 BASE_EXPORT Value TimeToValue(Time time); 42 BASE_EXPORT std::optional<Time> ValueToTime(const Value* value); 43 BASE_EXPORT std::optional<Time> ValueToTime(const Value& value); 44 45 // Converts between a FilePath (a std::string or std::u16string) and a 46 // string-flavored Value (the UTF-8 representation). 47 BASE_EXPORT Value FilePathToValue(FilePath file_path); 48 BASE_EXPORT std::optional<FilePath> ValueToFilePath(const Value* value); 49 BASE_EXPORT std::optional<FilePath> ValueToFilePath(const Value& value); 50 51 // Converts between a UnguessableToken (128 bits) and a string-flavored 52 // Value (32 hexadecimal digits). 53 BASE_EXPORT Value UnguessableTokenToValue(UnguessableToken token); 54 BASE_EXPORT std::optional<UnguessableToken> ValueToUnguessableToken( 55 const Value* value); 56 BASE_EXPORT std::optional<UnguessableToken> ValueToUnguessableToken( 57 const Value& value); 58 59 } // namespace base 60 61 #endif // BASE_JSON_VALUES_UTIL_H_ 62