1
#pragma once
2

            
3
#include <list>
4
#include <string>
5

            
6
#include "envoy/json/json_object.h"
7

            
8
#include "source/common/protobuf/protobuf.h"
9

            
10
#include "absl/strings/string_view.h"
11

            
12
namespace Envoy {
13
namespace Json {
14
namespace Nlohmann {
15

            
16
class Factory {
17
public:
18
  /**
19
   * Constructs a Json Object from a string.
20
   */
21
  static absl::StatusOr<ObjectSharedPtr> loadFromString(const std::string& json);
22

            
23
  /**
24
   * Constructs a Json Object from a Protobuf struct.
25
   */
26
  static ObjectSharedPtr loadFromProtobufStruct(const Protobuf::Struct& protobuf_struct);
27

            
28
  /**
29
   * Serializes a string in JSON format, throwing an exception if not valid UTF-8.
30
   *
31
   * @param The raw string -- must be in UTF-8 format.
32
   * @return A string suitable for inclusion in a JSON stream, including double-quotes.
33
   */
34
  static std::string serialize(absl::string_view str);
35

            
36
  /*
37
   * Serializes a JSON string to a byte vector using the MessagePack serialization format.
38
   * If the provided JSON string is invalid, an empty vector will be returned.
39
   * See: https://github.com/msgpack/msgpack/blob/master/spec.md
40
   */
41
  static std::vector<uint8_t> jsonToMsgpack(const std::string& json);
42

            
43
  // Serialization helper function for list of items.
44
  template <typename T> static std::string serialize(const T& items);
45
};
46

            
47
} // namespace Nlohmann
48
} // namespace Json
49
} // namespace Envoy