11 #ifndef __UT_JSONValue__
12 #define __UT_JSONValue__
142 bool record_source_offsets =
false);
145 bool parseValue(std::istream& is);
148 bool loadFromFile(
const char* file_name,
149 bool record_source_offsets =
false);
152 bool saveToFile(
const char* file_name,
171 return !isMap() && !isArray();
233 result = myData.myInt != 0;
236 result = myData.myReal != 0;
249 result = myData.myBool ? 1 : 0;
268 result = myData.myBool ? 1. : 0.;
297 template <
typename T>
303 return myType == JSON_REAL || myType == JSON_INT ||
310 void setBool(
bool v);
318 { setStringType(s,
length, JSON_STRING); }
320 { setStringType(s, JSON_STRING); }
324 { setStringType(s,
length, JSON_KEY); }
326 { setStringType(s, JSON_KEY); }
363 bool setUniformArray(
int nvalues,
const int32 *
data);
364 bool setUniformArray(
int nvalues,
const int64 *
data);
365 bool setUniformArray(
int nvalues,
const fpreal16 *
data);
366 bool setUniformArray(
int nvalues,
const fpreal32 *
data);
367 bool setUniformArray(
int nvalues,
const fpreal64 *
data);
373 {
return myMapIndex; }
380 {
return mySourceLine; }
382 {
return mySourceOffset; }
384 { mySourceLine = line; mySourceOffset =
offset; }
457 myCurrArrayPos = myNumArrayEntries = 0;
467 myValue = src.myValue;
468 myIsValid = src.myIsValid;
469 myCurrArrayPos = src.myCurrArrayPos;
470 myNumArrayEntries = src.myNumArrayEntries;
471 myValueType = src.myValueType;
472 myMapKeys = src.myMapKeys;
482 return myCurrArrayPos == rhs.myCurrArrayPos
483 && myIsValid == rhs.myIsValid;
487 return !(*
this == rhs);
491 return *this->getValue();
495 return *this->getValue();
505 bool atEnd()
const {
return !myIsValid; }
508 if(myValue && myIsValid)
510 if(myValueType == JSON_MAP)
513 if(myCurrArrayPos >= myMapKeys.entries())
516 else if(myValueType == JSON_ARRAY)
519 if(myCurrArrayPos >= myNumArrayEntries)
534 if (myValue && myIsValid)
535 return iteratorValue();
540 if (myValue && myIsValid)
546 if (myValue && myIsValid
547 && (myValueType == JSON_ARRAY || myValueType == JSON_MAP))
549 return myCurrArrayPos;
567 if (!getLowerCaseKey(tmp))
575 if(myValue && myIsValid)
577 if(myValueType == JSON_MAP)
579 key = myMapKeys(myCurrArrayPos);
610 myNumArrayEntries = 0;
614 : myValue(value_in), myIsValid(false), myCurrArrayPos(start_idx)
616 myNumArrayEntries = 0;
626 int64 myCurrArrayPos;
627 int64 myNumArrayEntries;
639 using pointer = std::pair<exint, UT_JSONValue*>;
647 using traverser::operator++;
648 using traverser::operator==;
649 using traverser::operator!=;
653 return std::make_pair(
672 using reference = std::tuple<exint, UT_StringHolder, UT_JSONValue&>;
673 using pointer = std::tuple<exint, UT_StringHolder, UT_JSONValue*>;
681 using traverser::operator++;
682 using traverser::operator==;
683 using traverser::operator!=;
689 return std::make_tuple(
696 return std::make_tuple(
702 if (myValue && myIsValid)
704 if (myValueType == JSON_MAP)
706 return myMapKeys(myCurrArrayPos);
737 using traverser::operator++;
738 using traverser::operator==;
739 using traverser::operator!=;
746 if (myValue && myIsValid)
748 if (myValueType == JSON_MAP)
750 return myMapKeys(myCurrArrayPos);
760 template <
typename IteratorT>
766 return IteratorT(myValue);
770 return IteratorT(myValue, myValue->count());
775 return IteratorT(me->myValue);
777 const IteratorT
end()
const
780 return IteratorT(me->myValue, me->myValue->
count());
864 std::pair<traverser, bool>
insert(
869 template <
typename... ARGS>
880 template <
typename... ARGS>
925 const char *key_value);
928 const char *key_value);
941 {
return !(*
this ==
src); }
947 void setStringType(
const char *
s,
int64 l,
Type t);
970 template <std::
size_t N, std::enable_if_t<N == 0,
int> = 0>
978 template <std::
size_t N, std::enable_if_t<N == 1,
int> = 0>
985 template <std::
size_t N, std::enable_if_t<N==0,
int> = 0>
989 return it.getArrayIndex();
992 template <std::
size_t N, std::enable_if_t<N == 1,
int> = 0>
999 template <std::
size_t N, std::enable_if_t<N==0,
int> = 0>
1003 return it.getArrayIndex();
1006 template <std::
size_t N, std::enable_if_t<N == 1,
int> = 0>
1013 template <std::
size_t N, std::enable_if_t<N == 2,
int> = 0>
1024 :
public std::integral_constant<std::size_t, 2>
1027 template <std::
size_t N>
1030 using type = decltype(get<N>(std::declval<UT_JSONValue::map_traverser>()));
1035 :
public std::integral_constant<std::size_t, 2>
1038 template <std::
size_t N>
1041 using type = decltype(get<N>(std::declval<UT_JSONValue::indexed_traverser>()));
1046 :
public std::integral_constant<std::size_t, 3>
1049 template <std::
size_t N>
1052 using type = decltype(get<N>(
1053 std::declval<UT_JSONValue::indexed_map_traverser>()));
Used to iterate over a map from a json value.
const traverser const_iterator
traverser find(const UT_StringRef &key)
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
SYS_NO_DISCARD_RESULT const_iterator begin() const
#define SYS_DEPRECATED(__V__)
const UT_JSONValue & value() const
SYS_NO_DISCARD_RESULT UT_JSONValue * operator()(const UT_StringRef &k)
SYS_NO_DISCARD_RESULT traverser begin()
bool isNumber() const
Returns whether the value can be interpreted as a number.
SYS_FORCE_INLINE void clear()
void setNull()
Set value to a null.
void setKey(const UT_StringHolder &s)
const GLuint GLenum const void * binary
UT_JSONValueArray stores a list of UT_JSONValue objects.
SYS_NO_DISCARD_RESULT const UT_JSONValue * operator()(const UT_StringRef &k) const
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
SYS_NO_DISCARD_RESULT UT_JSONValue & operator[](const UT_StringRef &k)
std::size_t SYS_HashType
Define the type for hash values.
UT_JSONValueArray * myArray
void erase(const UT_StringRef &k)
SYS_NO_DISCARD_RESULT int getSourceOffset() const
GLuint GLsizei GLsizei * length
map_traverser(UT_JSONValue *v)
const UT_StringHolder & key() const
JSON reader class which handles parsing of JSON or bJSON files.
SYS_NO_DISCARD_RESULT bool isMap() const
Class which writes ASCII or binary JSON streams.
void removeNullValuesRecursive()
**But if you need a result
bool getLowerCaseKey(UT_StringHolder &key)
Get a lower case map key (for case insensitive maps)
const UT_JSONValue * operator->() const
decltype(get< N >(std::declval< UT_JSONValue::map_traverser >())) type
SYS_NO_DISCARD_RESULT traverser beginTraversal()
const UT_JSONValue * getValue() const
const UT_JSONValue * operator->() const
indexed_traverser(UT_JSONValue *v, exint end_len)
SYS_NO_DISCARD_RESULT UT_JSONValue * operator()(int64 i)
bool contains(const UT_StringRef &key) const
SYS_NO_DISCARD_RESULT int getSourceLine() const
Used by UT_JSONValue parsing from a stream.
SYS_NO_DISCARD_RESULT bool isValueType() const
void setSourceOffset(int line, int offset)
SYS_NO_DISCARD_RESULT const_iterator end() const
decltype(get< N >(std::declval< UT_JSONValue::indexed_traverser >())) type
void setKey(const char *s, int64 length=-1)
json_proxy< indexed_traverser > indexed_proxy
traverser emplace_back(ARGS &&...args)
Insert a json value to the back of the contained array.
const UT_JSONValue & value() const
const traverser & operator=(const traverser &src)
decltype(get< N >(std::declval< UT_JSONValue::indexed_map_traverser >())) type
std::pair< traverser, bool > insert(const UT_StringHolder &key, const UT_JSONValue &value)
Add a key/value pair to the contained map.
SYS_NO_DISCARD_RESULT indexed_map_proxy enumerateMap()
SYS_NO_DISCARD_RESULT UT_JSONFindResult findParentValueRecursive(const UT_StringHolder &map_key, const char *key_value)
static const UT_StringHolder theEmptyString
json_proxy< indexed_map_traverser > indexed_map_proxy
Traverse the children of the current value.
SYS_NO_DISCARD_RESULT const indexed_proxy enumerate() const
const UT_JSONValue & operator*() const
int64 getArrayIndex() const
indexed_map_traverser(UT_JSONValue *v, exint end_len)
traverser(const traverser &src)
SYS_NO_DISCARD_RESULT indexed_proxy enumerate()
friend class UT_JSONValue
#define SYS_NO_DISCARD_RESULT
SYS_NO_DISCARD_RESULT bool isArray() const
UT_JSONValue & operator=(const UT_JSONValue &v)
std::forward_iterator_tag iterator_category
SYS_NO_DISCARD_RESULT SYS_HashType hash() const
Compute a hash.
UT_JSONValue * operator->()
indexed_traverser(UT_JSONValue *v)
SYS_NO_DISCARD_RESULT const UT_JSONValue & operator[](int64 i) const
SYS_NO_DISCARD_RESULT const UT_JSONValue * operator()(int64 i) const
bool getKey(UT_StringHolder &key) const
traverser & operator++()
++iterator
void setString(const UT_StringHolder &s)
SYS_NO_DISCARD_RESULT UT_JSONValue & operator[](int64 i)
const UT_JSONValue & value() const
const UT_StringHolder & key() const
SYS_NO_DISCARD_RESULT const map_proxy items() const
const UT_JSONValue & operator*() const
bool operator==(const traverser &rhs) const
No post increment as it is harmful.
void setString(const char *s, int64 length=-1)
UT_JSONFindResult(UT_JSONValue *v=nullptr, int64 idx=-1)
SYS_NO_DISCARD_RESULT int getMapIndex() const
Used internally by UT_JSONValueMap.
SYS_NO_DISCARD_RESULT exint count() const
Number of elements contained inside the json value.
SYS_NO_DISCARD_RESULT UT_StringRef toLower() const
SYS_NO_DISCARD_RESULT map_proxy items()
GA_API const UT_StringHolder N
SYS_NO_DISCARD_RESULT const_iterator cend() const
SYS_NO_DISCARD_RESULT Type getType() const
Get the type of data stored in the object.
json_proxy< map_traverser > map_proxy
Class to store JSON objects as C++ objects.
**If you just want to fire and args
UT_JSONValue * getValue()
SYS_NO_DISCARD_RESULT bool isNull() const
SYS_NO_DISCARD_RESULT bool operator!=(const UT_JSONValue &src) const
const IteratorT end() const
UT_JSONValue * operator->()
bool getKey(UT_String &key)
Deprecated in favour of UT_StringHolder method.
map_traverser(UT_JSONValue *v, exint end_len)
UT_API size_t format(char *buffer, size_t buffer_size, const UT_JSONValue &v)
SYS_NO_DISCARD_RESULT UT_JSONFindResult findValueRecursive(const UT_StringHolder &map_key, const char *key_value)
UT_JSONValue & operator*()
traverser append(const UT_JSONValue &v)
Insert a json value to the back of the contained array.
SYS_NO_DISCARD_RESULT const UT_StringHolder & getString() const
indexed_map_traverser(UT_JSONValue *v)
const IteratorT begin() const
SYS_NO_DISCARD_RESULT const indexed_map_proxy enumerateMap() const
std::pair< traverser, bool > emplace(const UT_StringHolder &key, ARGS &&...args)
Add an entry to the contained map.
void setMapIndex(int i)
Used internally by UT_JSONValueMap.
bool operator!=(const traverser &rhs) const
SYS_NO_DISCARD_RESULT traverser end()
SYS_NO_DISCARD_RESULT const_iterator cbegin() const