-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSI<->C++ bridging conversion for folly::dynamic
Summary: Enables bridging conversions to/from jsi::Value and folly::dynamic. Changelog: [Internal] - Add JSI<->C++ bridging conversion for folly::dynamic Differential Revision: D45544843 fbshipit-source-id: d1bae315d84d0d9841a54e73de5cba4ab87b13d0
- Loading branch information
1 parent
1be65ba
commit cdfa044
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/react-native/ReactCommon/react/bridging/Dynamic.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/dynamic.h> | ||
#include <react/bridging/Number.h> | ||
|
||
namespace facebook::react { | ||
|
||
template <> | ||
struct Bridging<folly::dynamic> { | ||
static folly::dynamic fromJs(jsi::Runtime &rt, const jsi::Value &value) { | ||
if (value.isNull()) { | ||
return nullptr; | ||
} else if (value.isBool()) { | ||
return value.asBool(); | ||
} else if (value.isNumber()) { | ||
return value.asNumber(); | ||
} else if (value.isString()) { | ||
return value.asString(rt).utf8(rt); | ||
} else if (value.isObject()) { | ||
auto obj = value.asObject(rt); | ||
if (obj.isArray(rt)) { | ||
folly::dynamic result = folly::dynamic::array(); | ||
auto array = obj.asArray(rt); | ||
auto length = array.size(rt); | ||
for (auto i = 0; i < length; ++i) { | ||
result.push_back(Bridging<folly::dynamic>::fromJs( | ||
rt, array.getValueAtIndex(rt, i))); | ||
} | ||
return result; | ||
} else { | ||
folly::dynamic result = folly::dynamic::object(); | ||
auto propertyNames = obj.getPropertyNames(rt); | ||
auto length = propertyNames.length(rt); | ||
for (auto i = 0; i < length; i++) { | ||
auto propertyName = propertyNames.getValueAtIndex(rt, i); | ||
result[Bridging<folly::dynamic>::fromJs(rt, propertyName)] = | ||
Bridging<folly::dynamic>::fromJs( | ||
rt, obj.getProperty(rt, propertyName.asString(rt))); | ||
} | ||
return result; | ||
} | ||
} | ||
return nullptr; | ||
} | ||
|
||
static jsi::Value toJs(jsi::Runtime &rt, folly::dynamic value) { | ||
if (value.isNull()) { | ||
return jsi::Value::null(); | ||
} else if (value.isBool()) { | ||
return Bridging<bool>::toJs(rt, value.asBool()); | ||
} else if (value.isNumber()) { | ||
return Bridging<double>::toJs(rt, value.asDouble()); | ||
} else if (value.isString()) { | ||
return Bridging<std::string>::toJs(rt, value.asString()); | ||
} else if (value.isArray()) { | ||
auto result = jsi::Array(rt, value.size()); | ||
for (auto i = 0; i < value.size(); ++i) { | ||
result.setValueAtIndex( | ||
rt, i, Bridging<folly::dynamic>::toJs(rt, value[i])); | ||
} | ||
return result; | ||
} else if (value.isObject()) { | ||
auto result = jsi::Object(rt); | ||
for (auto &pair : value.items()) { | ||
if (pair.first.isString()) { | ||
result.setProperty( | ||
rt, | ||
pair.first.asString().c_str(), | ||
Bridging<folly::dynamic>::toJs(rt, pair.second)); | ||
} | ||
} | ||
return result; | ||
} | ||
return jsi::Value::undefined(); | ||
} | ||
}; | ||
|
||
} // namespace facebook::react |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters