Improve clarity of schema parsing and serialization

This commit is contained in:
Scott Kyle 2016-01-13 14:53:39 -08:00
parent c34990759d
commit 1922125ab0
2 changed files with 12 additions and 25 deletions

View File

@ -140,36 +140,22 @@ static inline ObjectSchema RJSParseObjectSchema(JSContextRef ctx, JSObjectRef ob
objectSchema.name = RJSValidatedStringProperty(ctx, objectSchemaObject, nameString); objectSchema.name = RJSValidatedStringProperty(ctx, objectSchemaObject, nameString);
JSObjectRef propertiesObject = RJSValidatedObjectProperty(ctx, objectSchemaObject, propertiesString, "ObjectSchema must have a 'properties' object."); JSObjectRef propertiesObject = RJSValidatedObjectProperty(ctx, objectSchemaObject, propertiesString, "ObjectSchema must have a 'properties' object.");
JSPropertyNameArrayRef propertyNames = NULL;
size_t propertyCount;
if (RJSIsValueArray(ctx, propertiesObject)) { if (RJSIsValueArray(ctx, propertiesObject)) {
propertyCount = RJSValidatedListLength(ctx, propertiesObject); size_t propertyCount = RJSValidatedListLength(ctx, propertiesObject);
}
else {
propertyNames = JSObjectCopyPropertyNames(ctx, propertiesObject);
propertyCount = JSPropertyNameArrayGetCount(propertyNames);
}
for (size_t i = 0; i < propertyCount; i++) { for (size_t i = 0; i < propertyCount; i++) {
Property property;
// Check if the properties were provided as an object.
if (propertyNames) {
JSStringRef propertyName = JSPropertyNameArrayGetNameAtIndex(propertyNames, i);
JSValueRef propertyValue = RJSValidatedPropertyValue(ctx, propertiesObject, propertyName);
property = RJSParseProperty(ctx, propertyValue, RJSStringForJSString(propertyName), objectDefaults);
}
else {
JSObjectRef propertyObject = RJSValidatedObjectAtIndex(ctx, propertiesObject, (unsigned int)i); JSObjectRef propertyObject = RJSValidatedObjectAtIndex(ctx, propertiesObject, (unsigned int)i);
std::string propertyName = RJSValidatedStringProperty(ctx, propertyObject, nameString); std::string propertyName = RJSValidatedStringProperty(ctx, propertyObject, nameString);
property = RJSParseProperty(ctx, propertyObject, propertyName, objectDefaults); objectSchema.properties.emplace_back(RJSParseProperty(ctx, propertyObject, propertyName, objectDefaults));
} }
objectSchema.properties.emplace_back(property);
} }
else {
if (propertyNames) { JSPropertyNameArrayRef propertyNames = JSObjectCopyPropertyNames(ctx, propertiesObject);
size_t propertyCount = JSPropertyNameArrayGetCount(propertyNames);
for (size_t i = 0; i < propertyCount; i++) {
JSStringRef propertyName = JSPropertyNameArrayGetNameAtIndex(propertyNames, i);
JSValueRef propertyValue = RJSValidatedPropertyValue(ctx, propertiesObject, propertyName);
objectSchema.properties.emplace_back(RJSParseProperty(ctx, propertyValue, RJSStringForJSString(propertyName), objectDefaults));
}
JSPropertyNameArrayRelease(propertyNames); JSPropertyNameArrayRelease(propertyNames);
} }

View File

@ -269,6 +269,7 @@ json RPCServer::serialize_json_value(JSValueRef value) {
}; };
} }
else { else {
// Serialize this JS object as a plain object since it doesn't match any known types above.
JSPropertyNameArrayRef js_keys = JSObjectCopyPropertyNames(m_context, js_object); JSPropertyNameArrayRef js_keys = JSObjectCopyPropertyNames(m_context, js_object);
size_t count = JSPropertyNameArrayGetCount(js_keys); size_t count = JSPropertyNameArrayGetCount(js_keys);
std::vector<std::string> keys; std::vector<std::string> keys;