public dynamic ToObject(JsonApiResource jsonApiResource) { var typeResolver = _settings.TypeResolver; var resolvedType = typeResolver.ResolveType(jsonApiResource.Type); if (resolvedType == null) { throw new JsonApiTypeNotFoundException(string.Format("No type found for {0}", jsonApiResource.Type)); } var resource = (dynamic)Activator.CreateInstance(resolvedType); var propResolver = _settings.PropertyResolver; var idProperty = propResolver.ResolveJsonApiId(resolvedType); SetParseableStringProperty(resource, idProperty, jsonApiResource.Id); var typeProperty = propResolver.ResolveJsonApiType(resolvedType); SetProperty(resource, typeProperty, jsonApiResource.Type); MapAttributes(resource, jsonApiResource); MapRelationships(resource, jsonApiResource); return resource; }
private void MapRelationships(dynamic resource, JsonApiResource jsonApiResource) { if (jsonApiResource.Relationships == null) { return; } foreach (var kvp in jsonApiResource.Relationships) { MapRelationship(resource, kvp.Key, kvp.Value); } }
private void MapAttributes(dynamic resource, JsonApiResource jsonApiResource) { if (jsonApiResource.Attributes == null) { return; } foreach (var kvp in jsonApiResource.Attributes) { var attrProperty = _settings.PropertyResolver.ResolveJsonApiAttribute(((object)resource).GetType(), kvp.Key); SetProperty(resource, attrProperty, kvp.Value); } }