1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_UTILS_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_UTILS_H_ 19 20 #include <stdint.h> 21 22 #include "perfetto/ext/base/optional.h" 23 #include "perfetto/ext/base/string_view.h" 24 25 #include "src/trace_processor/importers/common/args_tracker.h" 26 27 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON) 28 #include <json/value.h> 29 #else 30 namespace Json { 31 class Value {}; 32 } // namespace Json 33 #endif 34 35 namespace perfetto { 36 namespace trace_processor { 37 namespace json { 38 39 // Returns whether JSON related functioanlity is supported with the current 40 // build flags. 41 bool IsJsonSupported(); 42 43 enum class TimeUnit { kNs = 1, kUs = 1000, kMs = 1000000 }; 44 base::Optional<int64_t> CoerceToTs(TimeUnit unit, const Json::Value& value); 45 base::Optional<int64_t> CoerceToInt64(const Json::Value& value); 46 base::Optional<uint32_t> CoerceToUint32(const Json::Value& value); 47 48 // Parses the given JSON string into a JSON::Value object. 49 // This function should only be called if |IsJsonSupported()| returns true. 50 base::Optional<Json::Value> ParseJsonString(base::StringView raw_string); 51 52 // Flattens the given Json::Value and adds each leaf node to the bound args 53 // inserter. Note: 54 // * |flat_key| and |key| should be non-empty and will be used to prefix the 55 // keys of all leaf nodes in the JSON. 56 // * |storage| is used to intern all strings (e.g. keys and values). 57 // * This function should only be called if |IsJsonSupported()| returns true. 58 bool AddJsonValueToArgs(const Json::Value& value, 59 base::StringView flat_key, 60 base::StringView key, 61 TraceStorage* storage, 62 ArgsTracker::BoundInserter* inserter); 63 64 } // namespace json 65 } // namespace trace_processor 66 } // namespace perfetto 67 68 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_UTILS_H_ 69