// PUBLIC METHODS /////////////////////////////////////////////////// #region Extensions Methods public static ApiProperty GetApiAttribute(this IAttributeInfo attributeInfo, IGetAttributes apiResource) { Contract.Requires(attributeInfo != null); if (apiResource == null || apiResource.Attributes == null) { return(null); } var apiAttributeName = attributeInfo.ApiPropertyName; ApiProperty apiAttribute; return(apiResource.Attributes.TryGetApiProperty(apiAttributeName, out apiAttribute) ? apiAttribute : null); }
public static void MapApiAttributesToClrResource(this IResourceType resourceType, object clrResource, IGetAttributes apiGetAttributes, IServiceModel serviceModel) { Contract.Requires(resourceType != null); Contract.Requires(clrResource != null); Contract.Requires(apiGetAttributes != null); Contract.Requires(serviceModel != null); var attributesInfo = resourceType.AttributesInfo; var attributesInfoCollection = attributesInfo.Collection; foreach (var attributeInfo in attributesInfoCollection) { var apiAttribute = attributeInfo.GetApiAttribute(apiGetAttributes); if (apiAttribute == null) { continue; } var clrAttribute = apiAttribute.ValueAsObject(); if (!attributeInfo.IsComplexType) { attributeInfo.SetClrProperty(clrResource, clrAttribute); continue; } // Handle attribute complex type with JSON.NET and IContractResolver. JToken clrAttributeAsJToken; if (!TypeConverter.TryConvert(clrAttribute, out clrAttributeAsJToken)) { clrAttributeAsJToken = JToken.FromObject(clrAttribute); } var clrAttributeClrType = attributeInfo.ClrPropertyType; var complexTypesContractResolver = serviceModel.CreateComplexTypesContractResolver(); var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = complexTypesContractResolver }; var jsonSerializer = JsonSerializer.Create(jsonSerializerSettings); clrAttribute = clrAttributeAsJToken.ToObject(clrAttributeClrType, jsonSerializer); attributeInfo.SetClrProperty(clrResource, clrAttribute); } }
// PUBLIC METHODS /////////////////////////////////////////////////// #region Factory Methods public static DomAttribute CreateFromApiResource(IAttributeInfo attributeInfo, IGetAttributes apiGetAttributes) { Contract.Requires(attributeInfo != null); Contract.Requires(apiGetAttributes != null); var apiAttribute = attributeInfo.GetApiAttribute(apiGetAttributes); var apiPropertyName = attributeInfo.ApiPropertyName; var clrPropertyType = attributeInfo.ClrPropertyType; var clrPropertyName = attributeInfo.ClrPropertyName; var clrAttribute = apiAttribute.ToClrObject(clrPropertyType); var domAttribute = new DomAttribute(apiAttribute, apiPropertyName, clrAttribute, clrPropertyName, clrPropertyType); return(domAttribute); }
protected static void WriteAttributes(JsonWriter writer, JsonSerializer serializer, IGetAttributes getAttributes) { Contract.Requires(writer != null); Contract.Requires(serializer != null); Contract.Requires(getAttributes != null); if (getAttributes.Attributes == null) { return; } writer.WritePropertyName(Keywords.Attributes); var attributes = getAttributes.Attributes; WriteApiObject(writer, serializer, attributes); }