本文整理汇总了C++中folly::dynamic::type方法的典型用法代码示例。如果您正苦于以下问题:C++ dynamic::type方法的具体用法?C++ dynamic::type怎么用?C++ dynamic::type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类folly::dynamic
的用法示例。
在下文中一共展示了dynamic::type方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dynamic_to_variant
static Variant dynamic_to_variant(const folly::dynamic& v) {
switch (v.type()) {
case folly::dynamic::Type::NULLT:
return init_null();
case folly::dynamic::Type::BOOL:
return v.asBool();
case folly::dynamic::Type::DOUBLE:
return v.asDouble();
case folly::dynamic::Type::INT64:
return v.asInt();
case folly::dynamic::Type::STRING:
return v.data();
case folly::dynamic::Type::ARRAY:
case folly::dynamic::Type::OBJECT:
ArrayInit arr_init(v.size(), ArrayInit::Mixed{});
for (auto& item : v.items()) {
arr_init.add(dynamic_to_variant(item.first),
dynamic_to_variant(item.second));
}
Array ret = arr_init.toArray();
// Sort the array since folly::dynamic has a tendency to iterate from
// back to front. This way a var_dump of the array, for example, looks
// ordered.
ret.sort(Array::SortNaturalAscending, true, false);
return ret;
}
not_reached();
}
示例2: fromDynamicInner
JSValueRef Value::fromDynamicInner(JSContextRef ctx, const folly::dynamic& obj) {
switch (obj.type()) {
// For primitive types (and strings), just create and return an equivalent JSValue
case folly::dynamic::Type::NULLT:
return JSC_JSValueMakeNull(ctx);
case folly::dynamic::Type::BOOL:
return JSC_JSValueMakeBoolean(ctx, obj.getBool());
case folly::dynamic::Type::DOUBLE:
return JSC_JSValueMakeNumber(ctx, obj.getDouble());
case folly::dynamic::Type::INT64:
return JSC_JSValueMakeNumber(ctx, obj.asDouble());
case folly::dynamic::Type::STRING:
return JSC_JSValueMakeString(ctx, String(ctx, obj.getString().c_str()));
case folly::dynamic::Type::ARRAY: {
// Collect JSValue for every element in the array
JSValueRef vals[obj.size()];
for (size_t i = 0; i < obj.size(); ++i) {
vals[i] = fromDynamicInner(ctx, obj[i]);
}
// Create a JSArray with the values
JSValueRef arr = JSC_JSObjectMakeArray(ctx, obj.size(), vals, nullptr);
return arr;
}
case folly::dynamic::Type::OBJECT: {
// Create an empty object
JSObjectRef jsObj = JSC_JSObjectMake(ctx, nullptr, nullptr);
// Create a JSValue for each of the object's children and set them in the object
for (auto it = obj.items().begin(); it != obj.items().end(); ++it) {
JSC_JSObjectSetProperty(
ctx,
jsObj,
String(ctx, it->first.asString().c_str()),
fromDynamicInner(ctx, it->second),
kJSPropertyAttributeNone,
nullptr);
}
return jsObj;
}
default:
// Assert not reached
LOG(FATAL) << "Trying to convert a folly object of unsupported type.";
return JSC_JSValueMakeNull(ctx);
}
}
示例3: to_string
static std::string dynamic_to_std_string(const folly::dynamic& v) {
switch (v.type()) {
case folly::dynamic::Type::NULLT:
case folly::dynamic::Type::ARRAY:
case folly::dynamic::Type::OBJECT:
return "";
case folly::dynamic::Type::BOOL:
return std::to_string(v.asBool());
case folly::dynamic::Type::DOUBLE:
return std::to_string(v.asDouble());
case folly::dynamic::Type::INT64:
return std::to_string(v.asInt());
case folly::dynamic::Type::STRING:
return v.data();
}
not_reached();
}
示例4: dynamic_to_variant
static Variant dynamic_to_variant(const folly::dynamic& v) {
switch (v.type()) {
case folly::dynamic::Type::NULLT:
return init_null_variant;
case folly::dynamic::Type::BOOL:
return v.asBool();
case folly::dynamic::Type::DOUBLE:
return v.asDouble();
case folly::dynamic::Type::INT64:
return v.asInt();
case folly::dynamic::Type::STRING:
return v.data();
case folly::dynamic::Type::ARRAY:
case folly::dynamic::Type::OBJECT:
ArrayInit ret(v.size(), ArrayInit::Mixed{});
for (auto& item : v.items()) {
ret.add(dynamic_to_variant(item.first),
dynamic_to_variant(item.second));
}
return ret.toArray();
}
not_reached();
}