Simplify Value.fromJSON API
Reviewed By: mhorowitz Differential Revision: D5154478 fbshipit-source-id: 3f9528a6401d89df7e170d55da9c0327db5f4f3e
This commit is contained in:
parent
407b8b4d1f
commit
7081391c3d
|
@ -521,8 +521,7 @@ Value JSCExecutor::callFunctionSyncWithValue(
|
||||||
void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue) {
|
void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue) {
|
||||||
try {
|
try {
|
||||||
SystraceSection s("JSCExecutor::setGlobalVariable", "propName", propName);
|
SystraceSection s("JSCExecutor::setGlobalVariable", "propName", propName);
|
||||||
|
auto valueToInject = Value::fromJSON(adoptString(std::move(jsonValue)));
|
||||||
auto valueToInject = Value::fromJSON(m_context, adoptString(std::move(jsonValue)));
|
|
||||||
Object::getGlobalObject(m_context).setProperty(propName.c_str(), valueToInject);
|
Object::getGlobalObject(m_context).setProperty(propName.c_str(), valueToInject);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::throw_with_nested(std::runtime_error("Error setting global variable: " + propName));
|
std::throw_with_nested(std::runtime_error("Error setting global variable: " + propName));
|
||||||
|
|
|
@ -36,7 +36,7 @@ TEST(Value, FromJSON) {
|
||||||
prepare();
|
prepare();
|
||||||
JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr);
|
JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr);
|
||||||
String s(ctx, "{\"a\": 4}");
|
String s(ctx, "{\"a\": 4}");
|
||||||
Value v(Value::fromJSON(ctx, s));
|
Value v(Value::fromJSON(s));
|
||||||
EXPECT_TRUE(v.isObject());
|
EXPECT_TRUE(v.isObject());
|
||||||
JSC_JSGlobalContextRelease(ctx);
|
JSC_JSGlobalContextRelease(ctx);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ TEST(Value, ToJSONString) {
|
||||||
prepare();
|
prepare();
|
||||||
JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr);
|
JSGlobalContextRef ctx = JSC_JSGlobalContextCreateInGroup(false, nullptr, nullptr);
|
||||||
String s(ctx, "{\"a\": 4}");
|
String s(ctx, "{\"a\": 4}");
|
||||||
Value v(Value::fromJSON(ctx, s));
|
Value v(Value::fromJSON(s));
|
||||||
folly::dynamic dyn = folly::parseJson(v.toJSONString());
|
folly::dynamic dyn = folly::parseJson(v.toJSONString());
|
||||||
ASSERT_NE(nullptr, dyn);
|
ASSERT_NE(nullptr, dyn);
|
||||||
EXPECT_TRUE(dyn.isObject());
|
EXPECT_TRUE(dyn.isObject());
|
||||||
|
|
|
@ -38,7 +38,8 @@ std::string Value::toJSONString(unsigned indent) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */
|
/* static */
|
||||||
Value Value::fromJSON(JSContextRef ctx, const String& json) {
|
Value Value::fromJSON(const String& json) {
|
||||||
|
JSContextRef ctx = json.context();
|
||||||
auto result = JSC_JSValueMakeFromJSONString(ctx, json);
|
auto result = JSC_JSValueMakeFromJSONString(ctx, json);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw JSException(folly::to<std::string>(
|
throw JSException(folly::to<std::string>(
|
||||||
|
@ -66,7 +67,7 @@ Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) {
|
||||||
return Value(ctx, jsVal);
|
return Value(ctx, jsVal);
|
||||||
#else
|
#else
|
||||||
auto json = folly::toJson(value);
|
auto json = folly::toJson(value);
|
||||||
return fromJSON(ctx, String(ctx, json.c_str()));
|
return fromJSON(String(ctx, json.c_str()));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -330,7 +330,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
RN_EXPORT std::string toJSONString(unsigned indent = 0) const;
|
RN_EXPORT std::string toJSONString(unsigned indent = 0) const;
|
||||||
RN_EXPORT static Value fromJSON(JSContextRef ctx, const String& json);
|
RN_EXPORT static Value fromJSON(const String& json);
|
||||||
RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value);
|
RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value);
|
||||||
RN_EXPORT JSContextRef context() const;
|
RN_EXPORT JSContextRef context() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue