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