| // Copyright 2026 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // https://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // | |
| #ifndef THIRD_PARTY_CREDENTIO_UTILS_CRJSON_UTILS_H_ | |
| #define THIRD_PARTY_CREDENTIO_UTILS_CRJSON_UTILS_H_ | |
| #include <cstdint> | |
| #include "absl/functional/function_ref.h" | |
| #include "absl/strings/string_view.h" | |
| #include "cbor/cbor.h" | |
| #include "nlohmann/json_fwd.hpp" | |
| namespace credentio { | |
| // Record a string value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordString(const cbor::MapView& map, absl::string_view key, | |
| bool required, nlohmann::json& json); | |
| // Record a byte string value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordByteString(const cbor::MapView& map, uint32_t key, bool required, | |
| nlohmann::json& json); | |
| // Record a byte string value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordByteString(const cbor::MapView& map, absl::string_view key, | |
| bool required, nlohmann::json& json); | |
| // Record a byte string value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordUint64(const cbor::MapView& map, absl::string_view key, | |
| bool required, nlohmann::json& json); | |
| // Record an int64 value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordInt64(const cbor::MapView& map, absl::string_view key, bool required, | |
| nlohmann::json& json); | |
| // Record a bool value from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordBool(const cbor::MapView& map, absl::string_view key, bool required, | |
| nlohmann::json& json); | |
| // A function that processes an array element. | |
| // | |
| // The function is called for each element in the array and the index of the | |
| // element in the array. The function should return a JSON object that | |
| // represents the element. | |
| using ArrayProcessor = absl::FunctionRef<nlohmann::json( | |
| uint32_t index, const cbor::ItemView& item)>; | |
| // A function that processes a map. | |
| // | |
| // The function is called for each map in the array of maps and should return a | |
| // JSON object that represents the map. | |
| using MapProcessor = | |
| absl::FunctionRef<nlohmann::json(const cbor::MapView& map)>; | |
| // Record an array from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordArray(const cbor::MapView& map, absl::string_view key, bool required, | |
| ArrayProcessor processor, nlohmann::json& json); | |
| // Record an array of maps from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordArrayOfMaps(const cbor::MapView& map, absl::string_view key, | |
| bool required, MapProcessor processor, | |
| nlohmann::json& json); | |
| // Record a map from the map to the JSON. | |
| // | |
| // If `required` is true, the key must be present in the map, otherwise the | |
| // function will add the key to the `_missing` array in the JSON. | |
| // | |
| // Returns true if the key was present in the map, false otherwise. | |
| bool RecordMap(const cbor::MapView& map, absl::string_view key, bool required, | |
| MapProcessor processor, nlohmann::json& json); | |
| } // namespace credentio | |
| #endif // THIRD_PARTY_CREDENTIO_UTILS_CRJSON_UTILS_H_ |