LCOV - code coverage report
Current view: top level - envoy/json - json_object.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 1 2 50.0 %
Date: 2024-01-05 06:35:25 Functions: 1 2 50.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <cstdint>
       4             : #include <functional>
       5             : #include <memory>
       6             : #include <string>
       7             : #include <vector>
       8             : 
       9             : #include "envoy/common/exception.h"
      10             : #include "envoy/common/pure.h"
      11             : 
      12             : #include "source/common/common/statusor.h"
      13             : 
      14             : #include "absl/types/variant.h"
      15             : 
      16             : namespace Envoy {
      17             : namespace Json {
      18             : class Object;
      19             : 
      20             : using ObjectSharedPtr = std::shared_ptr<Object>;
      21             : 
      22             : // @return false if immediate exit from iteration required.
      23             : using ObjectCallback = std::function<bool(const std::string&, const Object&)>;
      24             : 
      25             : /**
      26             :  * Exception thrown when a JSON error occurs.
      27             :  */
      28             : class Exception : public EnvoyException {
      29             : public:
      30           0 :   Exception(const std::string& message) : EnvoyException(message) {}
      31             : };
      32             : 
      33             : using ValueType = absl::variant<bool, int64_t, double, std::string>;
      34             : 
      35             : /**
      36             :  * Wraps an individual JSON node.
      37             :  */
      38             : class Object {
      39             : public:
      40       57591 :   virtual ~Object() = default;
      41             : 
      42             :   /**
      43             :    * Convert a generic object into an array of objects. This is useful for dealing
      44             :    * with arrays of arrays.
      45             :    * @return std::vector<ObjectSharedPtr> the converted object.
      46             :    */
      47             :   virtual std::vector<ObjectSharedPtr> asObjectArray() const PURE;
      48             : 
      49             :   /**
      50             :    * Get a bool, integer, double or string value by name.
      51             :    * @param name supplies the key name.
      52             :    * @return bool the value.
      53             :    */
      54             :   virtual absl::StatusOr<ValueType> getValue(const std::string& name) const PURE;
      55             : 
      56             :   /**
      57             :    * Get a boolean value by name.
      58             :    * @param name supplies the key name.
      59             :    * @return bool the value.
      60             :    */
      61             :   virtual bool getBoolean(const std::string& name) const PURE;
      62             : 
      63             :   /**
      64             :    * Get a boolean value by name.
      65             :    * @param name supplies the key name.
      66             :    * @param default_value supplies the value to return if the name does not exist.
      67             :    * @return bool the value.
      68             :    */
      69             :   virtual bool getBoolean(const std::string& name, bool default_value) const PURE;
      70             : 
      71             :   /**
      72             :    * Get an integer value by name.
      73             :    * @param name supplies the key name.
      74             :    * @return int64_t the value.
      75             :    */
      76             :   virtual int64_t getInteger(const std::string& name) const PURE;
      77             : 
      78             :   /**
      79             :    * Get an integer value by name or return a default if name does not exist.
      80             :    * @param name supplies the key name.
      81             :    * @param default_value supplies the value to return if name does not exist.
      82             :    * @return int64_t the value.
      83             :    */
      84             :   virtual int64_t getInteger(const std::string& name, int64_t default_value) const PURE;
      85             : 
      86             :   /**
      87             :    * Get a sub-object by name.
      88             :    * @param name supplies the key name.
      89             :    * @param allow_empty supplies whether to return an empty object if the key does not
      90             :    * exist.
      91             :    * @return ObjectObjectSharedPtr the sub-object.
      92             :    * @throws Json::Exception if unable to get the attribute or the type is not an object.
      93             :    */
      94             :   virtual ObjectSharedPtr getObject(const std::string& name, bool allow_empty = false) const PURE;
      95             : 
      96             :   /**
      97             :    * Get a sub-object by name.
      98             :    * @param name supplies the key name.
      99             :    * @param allow_empty supplies whether to return an empty object if the key does not
     100             :    * exist.
     101             :    * @return ObjectObjectSharedPtr the sub-object.
     102             :    */
     103             :   virtual absl::StatusOr<ObjectSharedPtr> getObjectNoThrow(const std::string& name,
     104             :                                                            bool allow_empty = false) const PURE;
     105             : 
     106             :   /**
     107             :    * Determine if an object has type Object.
     108             :    * @return bool is the object an Object?
     109             :    */
     110             :   virtual bool isObject() const PURE;
     111             : 
     112             :   /**
     113             :    * Determine if an object has type Array.
     114             :    * @return bool is the object an Array?
     115             :    */
     116             :   virtual bool isArray() const PURE;
     117             : 
     118             :   /**
     119             :    * Get an array by name.
     120             :    * @param name supplies the key name.
     121             :    * @param allow_empty specifies whether to return an empty array if the key does not exist.
     122             :    * @return std::vector<ObjectSharedPtr> the array of JSON  objects.
     123             :    */
     124             :   virtual std::vector<ObjectSharedPtr> getObjectArray(const std::string& name,
     125             :                                                       bool allow_empty = false) const PURE;
     126             : 
     127             :   /**
     128             :    * Get a string value by name.
     129             :    * @param name supplies the key name.
     130             :    * @return std::string the value.
     131             :    */
     132             :   virtual std::string getString(const std::string& name) const PURE;
     133             : 
     134             :   /**
     135             :    * Get a string value by name or return a default if name does not exist.
     136             :    * @param name supplies the key name.
     137             :    * @param default_value supplies the value to return if name does not exist.
     138             :    * @return std::string the value.
     139             :    */
     140             :   virtual std::string getString(const std::string& name,
     141             :                                 const std::string& default_value) const PURE;
     142             : 
     143             :   /**
     144             :    * Get a string array by name.
     145             :    * @param name supplies the key name.
     146             :    * @param allow_empty specifies whether to return an empty array if the key does not exist.
     147             :    * @return std::vector<std::string> the array of strings.
     148             :    */
     149             :   virtual std::vector<std::string> getStringArray(const std::string& name,
     150             :                                                   bool allow_empty = false) const PURE;
     151             : 
     152             :   /**
     153             :    * Get a double value by name.
     154             :    * @param name supplies the key name.
     155             :    * @return double the value.
     156             :    */
     157             :   virtual double getDouble(const std::string& name) const PURE;
     158             : 
     159             :   /**
     160             :    * Get a double value by name.
     161             :    * @param name supplies the key name.
     162             :    * @param default_value supplies the value to return if name does not exist.
     163             :    * @return double the value.
     164             :    */
     165             :   virtual double getDouble(const std::string& name, double default_value) const PURE;
     166             : 
     167             :   /**
     168             :    * @return a hash of the JSON object.
     169             :    * Per RFC 7159:
     170             :    *    An object is an unordered collection of zero or more name/value
     171             :    *    pairs, where a name is a string and a value is a string, number,
     172             :    *    boolean, null, object, or array.
     173             :    * Objects with fields in different orders are equivalent and produce the same hash.
     174             :    * It does not consider white space that was originally in the parsed JSON.
     175             :    */
     176             :   virtual uint64_t hash() const PURE;
     177             : 
     178             :   /**
     179             :    * Iterate over key-value pairs in an Object and call callback on each pair.
     180             :    */
     181             :   virtual void iterate(const ObjectCallback& callback) const PURE;
     182             : 
     183             :   /**
     184             :    * @return TRUE if the Object contains the key.
     185             :    * @param name supplies the key name to lookup.
     186             :    */
     187             :   virtual bool hasObject(const std::string& name) const PURE;
     188             : 
     189             :   /**
     190             :    * Validates JSON object against passed in schema.
     191             :    * @param schema supplies the schema in string format. A Json::Exception will be thrown if
     192             :    *        the JSON object doesn't conform to the supplied schema or the schema itself is not
     193             :    *        valid.
     194             :    */
     195             :   virtual void validateSchema(const std::string& schema) const PURE;
     196             : 
     197             :   /**
     198             :    * @return the value of the object as a string (where the object is a string).
     199             :    */
     200             :   virtual std::string asString() const PURE;
     201             : 
     202             :   /**
     203             :    * @return the JSON string representation of the object.
     204             :    */
     205             :   virtual std::string asJsonString() const PURE;
     206             : 
     207             :   /**
     208             :    * @return true if the JSON object is empty;
     209             :    */
     210             :   virtual bool empty() const PURE;
     211             : };
     212             : 
     213             : } // namespace Json
     214             : } // namespace Envoy

Generated by: LCOV version 1.15