RStudio Launcher Plugin SDK
1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
|
Go to the documentation of this file.
24 #ifndef LAUNCHER_PLUGINS_JSON_HPP
25 #define LAUNCHER_PLUGINS_JSON_HPP
34 #include <Optional.hpp>
41 namespace launcher_plugins {
53 typedef std::vector<std::pair<std::string, std::string> > StringPairList;
54 typedef std::map<std::string, std::vector<std::string> > StringListMap;
122 explicit Value(
bool in_value);
129 explicit Value(
double in_value);
136 explicit Value(
float in_value);
143 explicit Value(
int in_value);
150 explicit Value(int64_t in_value);
157 explicit Value(
const char* in_value);
164 explicit Value(
const std::string& in_value);
171 explicit Value(
unsigned int in_value);
178 explicit Value(uint64_t in_value);
183 virtual ~Value() =
default;
319 std::vector<std::string>& out_propViolations);
410 template <
typename T>
506 virtual Error parse(
const std::string& in_jsonStr);
652 std::string
write()
const;
659 void write(std::ostream& io_ostream)
const;
681 void move(
Value&& in_other);
699 PRIVATE_IMPL_SHARED(m_impl);
714 explicit Member(
const std::shared_ptr<Impl>& in_impl);
721 const std::string&
getName()
const;
735 std::bidirectional_iterator_tag,
748 explicit Iterator(
const Object* in_parent, std::ptrdiff_t in_startPos = 0);
824 std::ptrdiff_t m_pos;
845 explicit Object(
const StringPairList& in_strPairs);
989 bool erase(
const char* in_name);
998 bool erase(
const std::string& in_name);
1023 bool hasMember(
const char* in_name)
const;
1032 bool hasMember(
const std::string& in_name)
const;
1041 void insert(
const std::string& in_name,
const Value& in_value);
1050 void insert(
const std::string& in_name,
bool in_value);
1059 void insert(
const std::string& in_name,
double in_value);
1068 void insert(
const std::string& in_name,
float in_value);
1077 void insert(
const std::string& in_name,
int in_value);
1086 void insert(
const std::string& in_name, int64_t in_value);
1095 void insert(
const std::string& in_name,
const char* in_value);
1104 void insert(
const std::string& in_name,
const std::string& in_value);
1113 void insert(
const std::string& in_name,
unsigned int in_value);
1122 void insert(
const std::string& in_name, uint64_t in_value);
1131 void insert(
const std::string& in_name,
const Array& in_value);
1140 void insert(
const std::string& in_name,
const Object& in_value);
1165 Error parse(
const char* in_jsonStr)
override;
1175 Error parse(
const std::string& in_jsonStr)
override;
1222 class Iterator:
public std::iterator<std::bidirectional_iterator_tag,
1235 explicit Iterator(
const Array* in_parent, std::ptrdiff_t in_startPos = 0);
1306 const Array* m_parent;
1311 std::ptrdiff_t m_pos;
1332 explicit Array(
const StringPairList& in_strPairs);
1475 Error parse(
const char* in_jsonStr)
override;
1485 Error parse(
const std::string& in_jsonStr)
override;
1565 void push_back(
const std::string& in_value);
1614 bool toSetString(std::set<std::string>& out_set)
const;
1632 bool toVectorInt(std::vector<int>& out_set)
const;
1663 template <
typename T>
1668 else if (std::is_same<T, Object>::value)
1669 return in_value.
getType() == Type::OBJECT;
1670 else if (std::is_same<T, Array>::value)
1671 return in_value.
getType() == Type::ARRAY;
1672 else if (std::is_same<T, std::string>::value)
1673 return in_value.
getType() == Type::STRING;
1674 else if (std::is_same<T, bool>::value)
1675 return in_value.
getType() == Type::BOOL;
1676 else if (std::is_same<T, int>::value)
1677 return in_value.
getType() == Type::INTEGER;
1678 else if (std::is_same<T, unsigned int>::value)
1679 return in_value.
getType() == Type::INTEGER;
1680 else if (std::is_same<T, int64_t>::value)
1681 return in_value.
getType() == Type::INTEGER;
1682 else if (std::is_same<T, uint64_t>::value)
1683 return in_value.
getType() == Type::INTEGER;
1684 else if (std::is_same<T, unsigned long>::value)
1685 return in_value.
getType() == Type::INTEGER;
1686 else if (std::is_same<T, double>::value)
1687 return (in_value.
getType() == Type::INTEGER) || (in_value.
getType() == Type::REAL);
1692 std::string typeAsString(Type in_type);
1693 std::ostream& operator<<(std::ostream& io_ostream, Type in_type);
1703 template <
typename T>
1717 template <
typename T>
1721 return in_object.getType();
1733 template <
typename T>
1737 if (std::is_same<T, bool>::value)
1739 else if (std::is_same<T, int>::value)
1740 return Type::INTEGER;
1741 else if (std::is_same<T, double>::value)
1743 else if (std::is_same<T, std::string>::value)
1744 return Type::STRING;
1746 logging::logErrorMessage(
"Unexpected type");
1747 return Type::NULL_TYPE;
1759 template <
typename T>
1762 return Value(in_value);
1774 template <
typename T>
1777 return in_value ?
Value(*in_value) :
Value();
1789 template <
typename T>
1793 for (
auto&& val : in_vector)
1798 return std::move(results);
1810 template <
typename T>
1814 for (
const T& val : in_set)
1819 return std::move(results);
1833 template <
typename T>
1834 Type asJsonType(
const T& in_object)
1836 return detail::asJsonType(
1848 inline std::string typeAsString(
const Value& in_value)
1850 return typeAsString(in_value.
getType());
1862 template <
typename T>
1863 inline Value toJsonValue(
const T& in_value)
1865 return detail::toJsonValue(in_value);
1877 template<
typename T>
1880 return detail::toJsonValue(in_vector).
getArray();
1892 template<
typename T>
1895 return detail::toJsonValue(in_set).
getArray();
1918 Error jsonReadError(JsonReadError in_errorCode,
const std::string& in_message,
const ErrorLocation& in_errorLocation);
1927 bool isMissingMemberError(
const Error& in_error);
1940 template <
typename T>
1944 if ((itr == in_object.
end()) || (*itr).getValue().isNull())
1946 JsonReadError::MISSING_MEMBER,
1947 "Member " + in_name +
" does not exist in the specified JSON object.",
1950 if (!isType<T>((*itr).getValue()))
1952 std::ostringstream msgStream;
1953 msgStream <<
"Member " << in_name <<
" has type " << (*itr).getValue().getType() <<
1954 " which is not compatible with requested type " <<
typeid(T).name() <<
".";
1956 JsonReadError::INVALID_TYPE,
1961 out_value = (*itr).getValue().getValue<T>();
1976 template <
typename T>
1981 if (itr == in_object.
end() || (*itr).getValue().isNull())
1984 if (!isType<T>((*itr).getValue()))
1986 std::ostringstream msgStream;
1987 msgStream <<
"Member " << in_name <<
" has type " << (*itr).getValue().getType() <<
1988 " which is not compatible with requested type " <<
typeid(T).name() <<
".";
1990 JsonReadError::INVALID_TYPE,
1995 out_value = (*itr).getValue().getValue<T>();
2010 template <
typename T>
2014 if ((itr == in_object.
end()) || (*itr).getValue().isNull())
2016 JsonReadError::MISSING_MEMBER,
2017 "Member " + in_name +
" does not exist in the specified JSON object.",
2020 if (!(*itr).getValue().isArray())
2022 "Member " + in_name +
" is not an array.",
2026 for (
size_t i = 0, n = array.
getSize(); i < n; ++i)
2028 const Value& value = array[i];
2029 if (!isType<T>(value))
2031 std::ostringstream msgStream;
2032 msgStream <<
"Element " << i <<
" of member " + in_name <<
" is of type " << value.
getType() <<
2033 " which is not compatible with the requested type " <<
typeid(T).name() <<
".";
2035 JsonReadError::INVALID_TYPE,
2040 out_values.push_back(value.
getValue<T>());
2057 template <
typename T>
2061 if ((itr == in_object.
end()) || (*itr).getValue().isNull())
2063 JsonReadError::MISSING_MEMBER,
2064 "Member " + in_name +
" does not exist in the specified JSON object.",
2067 if (!(*itr).getValue().isArray())
2069 "Member " + in_name +
" is not an array.",
2073 for (
size_t i = 0, n = array.
getSize(); i < n; ++i)
2075 const Value& value = array[i];
2076 if (!isType<T>(value))
2078 std::ostringstream msgStream;
2079 msgStream <<
"Element " << i <<
" of member " + in_name <<
" is of type " << value.
getType() <<
2080 " which is not compatible with the requested type " <<
typeid(T).name() <<
".";
2082 JsonReadError::INVALID_TYPE,
2087 out_values.insert(value.
getValue<T>());
2104 template <
typename T>
2107 std::vector<T> values;
2112 out_values = values;
2128 template <
typename T>
2136 out_values = values;
2154 template <
typename T,
typename... Args>
2177 template <
typename T,
typename... Args>
2201 template <
typename T,
typename... Args>
2225 template <
typename T,
typename... Args>
2249 template <
typename T,
typename... Args>
2252 const std::string& in_name,
2253 Optional<std::vector<T> >& out_value,
2277 template <
typename T,
typename... Args>
2280 const std::string& in_name,
2295 #endif // LAUNCHER_PLUGINS_JSON_HPP
std::reverse_iterator< Iterator > ReverseIterator
Reverse iterator for a JSON object.
Definition: Json.hpp:833
Value getValue() const
Gets the value of the member.
uint64_t getUInt64() const
Gets the value as an uint64. If the call to getType() does not return Type::UINT64,...
Value clone() const
Makes a copy of this JSON value.
bool isEmpty() const
Checks whether the JSON object is empty.
Iterator & operator=(const Iterator &in_other)
Assignment operator.
virtual Error parse(const char *in_jsonStr)
Parses the JSON string into this value.
bool operator==(const Iterator &in_other) const
Equality operator.
Class which represents a specific type of JSON Value: a JSON object.
Definition: Json.hpp:687
Value getFront() const
Gets the value at the front of the JSON array.
ReverseIterator rend() const
Gets an iterator before the first member of this object, which can be compared with an other ReverseI...
Class which represents the location of an error.
Definition: Error.hpp:74
int64_t getInt64() const
Gets the value as an int64. If the call to getType() does not return Type::INT64, this method is inva...
bool isObject() const
Checks whether the value is a JSON object or not.
bool isUInt64() const
Checks whether the value is an unsigned int 64 value or not.
virtual ~Value()=default
Virtual destructor.
bool getBool() const
Gets the value as a bool. If the call to getType() does not return Type::BOOL, this method is invalid...
Object & operator=(const Object &in_other)
Assignment operator.
Array toJsonArray(const std::vector< T > &in_vector)
Converts a vector value to a JSON array value.
Definition: Json.hpp:1878
int getInt() const
Gets the value as an int. If the call to getType() does not return Type::INT, this method is invalid.
Class which allows iterating over the elements of a JSON array.
Definition: Json.hpp:1222
void clear()
Clears the JSON array.
Value getValueAt(size_t in_index) const
Gets the value at the specified index of the JSON array.
bool isType(const Value &in_value)
Checks whether the specified JSON value is of the type specified in the template parameter.
Definition: Json.hpp:1664
Value value_type
Typedef required for the inheritance of std::iterator with a value_type of json::Value.
Definition: Json.hpp:1217
void clear()
Clears the JSON object.
bool operator!=(const Iterator &in_other) const
Inequality operator.
Array getArray() const
Gets the value as a JSON array. If the call to getType() does not return Type::ARRAY,...
Error jsonReadError(JsonReadError in_errorCode, const std::string &in_message, const ErrorLocation &in_errorLocation)
Creates a JSON read error.
bool erase(const char *in_name)
Erases a member by name.
StringPairList toStringPairList() const
Converts this array into a vector of string pairs. Elements of the form "key=value" will be parsed in...
Value operator[](const char *in_name)
Accessor operator. Gets the value a member of this JSON object by name. If no such object exists,...
bool isString() const
Checks whether the value is a string value or not.
double getDouble() const
Gets the value as a double. If the call to getType() does not return Type::DOUBLE,...
bool isArray() const
Checks whether the value is a JSON array or not.
bool toStringMap(StringListMap &out_map) const
Converts this JSON object to a map with string keys and a list of string values.
size_t getSize() const
Gets the number of members in the JSON object.
Iterator erase(const Iterator &in_itr)
Erases the member specified by the provided iterator.
Iterator(const Object *in_parent, std::ptrdiff_t in_startPos=0)
Constructor.
Value operator[](size_t in_index) const
Accessor operator. Gets the JSON value at the specified position in the array.
bool operator==(const Iterator &in_other) const
Equality operator.
Array & operator=(const Array &in_other)
Assignment operator.
Value & operator=(const Value &in_other)
Assignment operator from Value.
bool operator!=(const Iterator &in_other) const
Inequality operator.
size_t getSize() const
Gets the number of values in the JSON array.
Iterator(const Array *in_parent, std::ptrdiff_t in_startPos=0)
Constructor.
Iterator end() const
Gets an iterator after the last member of this object.
Class which represents a successful operation (i.e. no error).
Definition: Error.hpp:437
Error readObject(const Object &in_object, const std::string &in_name, T &out_value)
Reads a member from an object.
Definition: Json.hpp:1941
PRIVATE_IMPL_SHARED(m_impl)
Private implementation of Value.
void push_back(const Value &in_value)
Pushes the value onto the end of the JSON array.
Array()
Constructs an empty JSON array.
Error validate(const std::string &in_schema) const
Validates this JSON value against a schema.
bool operator!=(const Value &in_other) const
Inequality operator.
Iterator & operator--()
Pre-decrement operator.
reference operator*() const
Dereference operator.
Type asJsonType(const T &in_object, std::true_type)
Internal utility function. Gets the type of the object as a JSON type, if the object is a JSON type (...
Definition: Json.hpp:1718
Iterator & operator=(const Iterator &in_other)
Assignment operator.
unsigned int getUInt() const
Gets the value as an unsigned int. If the call to getType() does not return Type::UINT,...
Error setValueAtPointerPath(const std::string &in_pointerPath, const json::Value &in_value)
Sets a value within the current value based on the specified JSON Pointer path.
bool isFloat() const
Checks whether the value is a float value or not.
Object getObject() const
Gets the value as a JSON object. IF the call to getType() does not return Type::OBJECT,...
bool isInt() const
Checks whether the value is an int 32 value or not.
bool isInt64() const
Checks whether the value is an int 64 value or not.
bool isMissingMemberError(const Error &in_error)
Checks whether the supplied error is a "missing member" error.
Error parse(const char *in_jsonStr) override
Parses the JSON string into this array.
Class which allows iterating over the members of a JSON object.
Definition: Json.hpp:734
Iterator begin() const
Gets an iterator pointing to the first member of this object.
Iterator & operator++()
Pre-increment operator.
ReverseIterator rbegin() const
Gets an iterator pointing to the last member of this object, which iterates in the reverse direction.
Iterator find(const char *in_name) const
Finds a JSON member by name.
bool toSetString(std::set< std::string > &out_set) const
Converts this JSON array to a set of strings.
std::reverse_iterator< Iterator > ReverseIterator
Reverse iterator for a JSON array.
Definition: Json.hpp:1320
Value toJsonValue(const T &in_value)
Internal utility function. Converts a C/C++ value to a JSON value.
Definition: Json.hpp:1760
Class which represents an error.
Definition: Error.hpp:174
Type
Enum which represents the type of a json value.
Definition: Json.hpp:59
std::string getString() const
Gets the value as a string. If the call to getType() does not return Type::STRING,...
Iterator begin() const
Gets an iterator pointing to the first member of this array.
Value getBack() const
Gets the value at the back of the JSON array.
T getValue() const
Gets this JSON value as the specified type.
bool toVectorInt(std::vector< int > &out_set) const
Converts this JSON array to a vector of integers.
Class which represents a JSON array.
Definition: Json.hpp:1210
bool isEmpty() const
Checks whether the JSON array is empty.
bool toVectorString(std::vector< std::string > &out_set) const
Converts this JSON array to a vector of strings.
ReverseIterator rbegin() const
Gets an iterator pointing to the last member of this array, which iterates in the reverse direction.
bool isDouble() const
Checks whether the value is a double value or not.
Error parseAndValidate(const std::string &in_jsonStr, const std::string &in_schema)
Parses the JSON string and validates it against the schema.
Container class which represents a value that may or may not be set.
Definition: Optional.hpp:38
bool isNull() const
Checks whether the value is null or not.
void insert(const std::string &in_name, const Value &in_value)
Inserts the specified member into this JSON object. If an object with the same name already exists,...
std::string write() const
Writes this value to a string.
float getFloat() const
Gets the value as a float. If the call to getType() does not return Type::FLOAT, this method is inval...
Struct which is either a child class of std::true_type or std::false_type depending on whether T is a...
Definition: Json.hpp:1704
Type getType() const
Gets the type of this value.
static Error getSchemaDefaults(const std::string &in_schema, Object &out_schemaDefaults)
Creates a JSON object which represents the schema defaults of the provided JSON schema string.
bool hasMember(const char *in_name) const
Checks whether this object has a member with the specified name.
Error coerce(const std::string &in_schema, std::vector< std::string > &out_propViolations)
Attempts to coerce a JSON object to conform to the given schema by discarding non-conforming properti...
Member()=default
Default constructor.
static Member createMember(const std::string &in_name, const Value &in_value)
Creates a JSON object from the given name and JSON value.
std::shared_ptr< Impl > ValueImplPtr
Convenience typedef for the type of the private implementation of json::Value.
Definition: Json.hpp:86
Iterator end() const
Gets an iterator after the last member of this array.
Iterator & operator--()
Pre-decrement operator.
bool isBool() const
Checks whether the value is a boolean value or not.
Class which represents a single member of a JSON object.
Definition: Json.hpp:695
Object()
Constructs an empty JSON object.
static Object mergeObjects(const Object &in_base, const Object &in_overlay)
Merges two JSON objects together. Conflicts between the base and the overlay will be resolved by taki...
Iterator & operator++()
Pre-increment operator.
ReverseIterator rend() const
Gets an iterator before the first member of this array, which can be compared with an other ReverseIt...
StringPairList toStringPairList() const
Converts this JSON object to a list of string pairs.
std::string writeFormatted() const
Writes and formats this value to a string.
JsonReadError
Errors which may occur while reading values from JSON objects.
Definition: Json.hpp:1902
bool isUInt() const
Checks whether the value is an unsigned int 32 value or not.
Error parse(const char *in_jsonStr) override
Parses the JSON string into this object.
Class which represents a json value.
Definition: Json.hpp:74
reference operator*() const
Dereference operator.
bool operator==(const Value &in_other) const
Equality operator.
const std::string & getName() const
Gets the name of the member.