Improve clarity of schema parsing and serialization
This commit is contained in:
parent
c34990759d
commit
1922125ab0
|
@ -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);
|
||||||
}
|
for (size_t i = 0; i < propertyCount; i++) {
|
||||||
else {
|
|
||||||
propertyNames = JSObjectCopyPropertyNames(ctx, propertiesObject);
|
|
||||||
propertyCount = JSPropertyNameArrayGetCount(propertyNames);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue