24 #ifndef PXR_BASE_JS_CONVERTER_H
25 #define PXR_BASE_JS_CONVERTER_H
33 PXR_NAMESPACE_OPEN_SCOPE
37 template <
class ValueType,
class MapType,
bool UseInt64 = true>
38 struct Js_ValueToInt {
39 static ValueType Apply(
const JsValue& value) {
47 template <
class ValueType,
class MapType>
48 struct Js_ValueToInt<ValueType, MapType, false>
50 static ValueType Apply(
const JsValue& value) {
51 return ValueType(value.
GetInt());
78 template <
class ValueType,
class MapType,
bool UseInt64 = true>
81 typedef std::vector<ValueType> VectorType;
87 return _ToValueType(value);
92 static ValueType _ToValueType(
const JsValue& value) {
94 case JsValue::ObjectType:
95 return ValueType(_ObjectToMap(value.
GetJsObject()));
96 case JsValue::ArrayType:
97 return ValueType(_ArrayToVector(value.
GetJsArray()));
98 case JsValue::BoolType:
99 return ValueType(value.
GetBool());
100 case JsValue::StringType:
102 case JsValue::RealType:
103 return ValueType(value.
GetReal());
104 case JsValue::IntType:
105 return Js_ValueToInt<ValueType, MapType, UseInt64>::Apply(value);
106 case JsValue::NullType:
116 static MapType _ObjectToMap(
const JsObject&
object) {
118 for (
const auto& p :
object) {
119 result[p.first] = _ToValueType(p.second);
125 static VectorType _ArrayToVector(
const JsArray& array) {
127 result.reserve(array.size());
128 for (
const auto& value : array) {
129 result.push_back(_ToValueType(value));
138 template <
class ValueType,
class MapType>
143 PXR_NAMESPACE_CLOSE_SCOPE
145 #endif // PXR_BASE_JS_CONVERTER_H
ValueType JsConvertToContainerType(const JsValue &value)
Returns value converted recursively to the template and map types given by the ValueType and MapType ...
JS_API int GetInt() const
Returns the integer held by this value.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Low-level utilities for informing users of various internal and external diagnostic conditions...
JS_API const std::string & GetString() const
Returns the string held by this value.
A discriminated union type for JSON values.
JS_API const JsObject & GetJsObject() const
Returns the object held by this value.
static ValueType Convert(const JsValue &value)
Converts the given value recursively to a structure using the value and map types specified by the Va...
JS_API uint64_t GetUInt64() const
Returns the 64-bit unsigned integer held by this value.
JS_API int64_t GetInt64() const
Returns the 64-bit integer held by this value.
JS_API const JsArray & GetJsArray() const
Returns the array held by this value.
A helper class that can convert recursive JsValue structures to identical structures using a differen...
JS_API bool GetBool() const
Returns the bool held by this value.
JS_API Type GetType() const
Returns the type of this value.
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.
JS_API double GetReal() const
Returns the double held by this value.