Skip to content

Commit

Permalink
Add Object.getPropertyNames() and Object.toJSONMap APIs
Browse files Browse the repository at this point in the history
Summary:
Adds APIs to get all the enumerable property names of an object and to get an object as a map of property names to JSON values.

public

Reviewed By: lexs

Differential Revision: D2916238

fb-gh-sync-id: 0d9ee1eb4886d58fba8241537f6a0dad6024bd0e
shipit-source-id: 0d9ee1eb4886d58fba8241537f6a0dad6024bd0e
  • Loading branch information
astreet authored and facebook-github-bot-5 committed Feb 10, 2016
1 parent e6b6aed commit bab4818
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ReactAndroid/src/main/jni/react/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ Value Object::getProperty(const String& propName) const {
return Value(m_context, property);
}

Value Object::getPropertyAtIndex(unsigned index) const {
JSValueRef exn;
JSValueRef property = JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn);
if (!property) {
std::string exceptionText = Value(m_context, exn).toString().str();
throwJSExecutionException("Failed to get property at index %u: %s", index, exceptionText.c_str());
}
return Value(m_context, property);
}

Value Object::getProperty(const char *propName) const {
return getProperty(String(propName));
}
Expand All @@ -96,6 +106,30 @@ void Object::setProperty(const char *propName, const Value& value) const {
setProperty(String(propName), value);
}

std::vector<std::string> Object::getPropertyNames() const {
std::vector<std::string> names;
auto namesRef = JSObjectCopyPropertyNames(m_context, m_obj);
size_t count = JSPropertyNameArrayGetCount(namesRef);
for (size_t i = 0; i < count; i++) {
auto string = String::ref(JSPropertyNameArrayGetNameAtIndex(namesRef, i));
names.emplace_back(string.str());
}
JSPropertyNameArrayRelease(namesRef);
return names;
}

std::unordered_map<std::string, std::string> Object::toJSONMap() const {
std::unordered_map<std::string, std::string> map;
auto namesRef = JSObjectCopyPropertyNames(m_context, m_obj);
size_t count = JSPropertyNameArrayGetCount(namesRef);
for (size_t i = 0; i < count; i++) {
auto key = String::ref(JSPropertyNameArrayGetNameAtIndex(namesRef, i));
map.emplace(key.str(), getProperty(key).toJSONString());
}
JSPropertyNameArrayRelease(namesRef);
return map;
}

/* static */
Object Object::create(JSContextRef ctx) {
JSObjectRef newObj = JSObjectMake(
Expand Down
6 changes: 6 additions & 0 deletions ReactAndroid/src/main/jni/react/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include <memory>
#include <sstream>
#include <unordered_map>
#include <vector>

#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSRetainPtr.h>
#include <JavaScriptCore/JSStringRef.h>
Expand Down Expand Up @@ -123,8 +126,11 @@ class Object : public noncopyable {

Value getProperty(const String& propName) const;
Value getProperty(const char *propName) const;
Value getPropertyAtIndex(unsigned index) const;
void setProperty(const String& propName, const Value& value) const;
void setProperty(const char *propName, const Value& value) const;
std::vector<std::string> getPropertyNames() const;
std::unordered_map<std::string, std::string> toJSONMap() const;

void makeProtected() {
if (!m_isProtected && m_obj) {
Expand Down

0 comments on commit bab4818

Please sign in to comment.