Skip to content

Commit

Permalink
Try to get rid of std::variant in json_container.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou committed Sep 28, 2024
1 parent 4f1c62a commit dbc5272
Showing 1 changed file with 123 additions and 10 deletions.
133 changes: 123 additions & 10 deletions include/util/json_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,145 @@ struct Null
};

/**
* Typed Value sum-type implemented as a variant able to represent tree-like JSON structures.
* Typed Object.
*
* Dispatch on its type by either by using apply_visitor or its get function.
* Unwrap the key-value pairs holding type via its values member attribute.
*/
using Value = std::variant<String, Number, Object, Array, True, False, Null>;
struct Object;

/**
* Typed Object.
* Typed Array.
*
* Unwrap the key-value pairs holding type via its values member attribute.
* Unwrap the Value holding type via its values member attribute.
*/
struct Array;

struct Value;

// Definitions of Object and Array (must come after Value is defined)
struct Object
{
std::unordered_map<std::string, Value> values;
};

/**
* Typed Array.
*
* Unwrap the Value holding type via its values member attribute.
*/
struct Array
{
std::vector<Value> values;
};


struct Value {
enum class Type {
Invalid,
String,
Number,
Object,
Array,
True,
False,
Null
};
String string;
Number number;
Object object;
Array array;
Type type;

Value() noexcept : type(Type::Invalid) {}
Value(const Null&) noexcept : type(Type::Null) {}
Value(const True&) noexcept : type(Type::True) {}
Value(const False&) noexcept : type(Type::False) {}
Value(String &&string_) noexcept : string(std::move(string_)), type(Type::String) {}
Value(const Number &number_) noexcept : number(number_), type(Type::Number) {}
Value(const Object &object_) noexcept : object(object_), type(Type::Object) {}
Value(const Array &array_) noexcept : array(array_), type(Type::Array) {}

Value(double number) noexcept : number(number), type(Type::Number) {}
Value(std::string string) noexcept : string(std::move(string)), type(Type::String) {}
Value(const char* string) noexcept : string(string), type(Type::String) {}
};

} // namespace osrm::util::json

namespace std {
template <typename T>
inline T& get(osrm::util::json::Value& value) noexcept;

template<>
inline osrm::util::json::String& get<osrm::util::json::String>(osrm::util::json::Value& value) noexcept
{
return value.string;
}

template<>
inline osrm::util::json::Number& get<osrm::util::json::Number>(osrm::util::json::Value& value) noexcept
{
return value.number;
}

template<>
inline osrm::util::json::Object& get<osrm::util::json::Object>(osrm::util::json::Value& value) noexcept
{
return value.object;
}

template<>
inline osrm::util::json::Array& get<osrm::util::json::Array>(osrm::util::json::Value& value) noexcept
{
return value.array;
}

template <typename T>
inline const T& get(const osrm::util::json::Value& value) noexcept;

template<>
inline const osrm::util::json::String& get<osrm::util::json::String>(const osrm::util::json::Value& value) noexcept
{
return value.string;
}

template<>
inline const osrm::util::json::Number& get<osrm::util::json::Number>(const osrm::util::json::Value& value) noexcept
{
return value.number;
}

template<>
inline const osrm::util::json::Object& get<osrm::util::json::Object>(const osrm::util::json::Value& value) noexcept
{
return value.object;
}

template<typename Visitor>
inline void visit(Visitor&& visitor, const osrm::util::json::Value& value)
{
switch (value.type)
{
case osrm::util::json::Value::Type::String:
visitor(value.string);
break;
case osrm::util::json::Value::Type::Number:
visitor(value.number);
break;
case osrm::util::json::Value::Type::Object:
visitor(value.object);
break;
case osrm::util::json::Value::Type::Array:
visitor(value.array);
break;
case osrm::util::json::Value::Type::True:
visitor(osrm::util::json::True{});
break;
case osrm::util::json::Value::Type::False:
visitor(osrm::util::json::False{});
break;
case osrm::util::json::Value::Type::Null:
visitor(osrm::util::json::Null{});
break;
case osrm::util::json::Value::Type::Invalid:
break;
}
}

}// namespace std
#endif // JSON_CONTAINER_HPP

0 comments on commit dbc5272

Please sign in to comment.