public void MaterializeComplexTypeShouldWork() { OData.ODataResource complexValue = new OData.ODataResource { Properties = new OData.ODataProperty[] { new OData.ODataProperty { Name = "age", Value = 11 }, new OData.ODataProperty { Name = "name", Value = "June" }, new OData.ODataProperty { Name = "color", Value = new OData.ODataEnumValue("blue") }, new OData.ODataProperty { Name = "primitives", Value = new OData.ODataCollectionValue { TypeName = "Edm.Int32", Items = new List <object> { 1, 2, 3 } } }, new OData.ODataProperty { Name = "enums", Value = new OData.ODataCollectionValue { TypeName = "color", Items = new List <OData.ODataEnumValue> { new OData.ODataEnumValue("white"), new OData.ODataEnumValue("blue") } } } } }; var materializerEntry = MaterializerEntry.CreateEntry(complexValue, OData.ODataFormat.Json, false, new ClientEdmModel(ODataProtocolVersion.V4)); this.CreateEntryMaterializationPolicy().Materialize(materializerEntry, typeof(ComplexType), false); var complex = materializerEntry.ResolvedObject as ComplexType; complex.Name.Should().Be("June"); complex.Age.Should().Be(11); complex.Color.Should().Be(Color.Blue); complex.Primitives.Should().ContainInOrder(1, 2, 3); complex.Enums.Should().ContainInOrder(Color.White, Color.Blue); }
/// <summary> Asynchronously start writing a resource. </summary> /// <returns>A task instance that represents the asynchronous write operation.</returns> /// <param name="resource">The resource or item to write.</param> public abstract Task WriteStartAsync(ODataResource resource);
public void MaterializeEntityShouldWork() { var odataEntry = new OData.ODataResource() { Id = new Uri("http://www.odata.org/service.svc/entitySet(1)") }; odataEntry.Properties = new OData.ODataProperty[] { new OData.ODataProperty { Name = "keyProp", Value = 1 }, new OData.ODataProperty { Name = "colorProp", Value = new OData.ODataEnumValue("blue") }, new OData.ODataProperty { Name = "primitiveCollection", Value = new OData.ODataCollectionValue { TypeName = "Edm.Int32", Items = new List <object> { 1, 2, 3 } } }, new OData.ODataProperty { Name = "enumCollection", Value = new OData.ODataCollectionValue { TypeName = "color", Items = new List <OData.ODataEnumValue> { new OData.ODataEnumValue("white"), new OData.ODataEnumValue("blue") } } } }; var complexP = new OData.ODataNestedResourceInfo() { Name = "complexProp", IsCollection = false }; var complexResource = new OData.ODataResource { Properties = new OData.ODataProperty[] { new OData.ODataProperty { Name = "age", Value = 11 }, new OData.ODataProperty { Name = "name", Value = "June" } } }; var complexColP = new OData.ODataNestedResourceInfo { Name = "complexCollection", IsCollection = true }; var complexColResourceSet = new OData.ODataResourceSet(); var items = new List <OData.ODataResource> { new OData.ODataResource { Properties = new OData.ODataProperty[] { new OData.ODataProperty { Name = "name", Value = "Aug" }, new OData.ODataProperty { Name = "age", Value = 8 }, new OData.ODataProperty { Name = "color", Value = new OData.ODataEnumValue("white") } } }, new OData.ODataResource { Properties = new OData.ODataProperty[] { new OData.ODataProperty { Name = "name", Value = "Sep" }, new OData.ODataProperty { Name = "age", Value = 9 }, new OData.ODataProperty { Name = "color", Value = new OData.ODataEnumValue("blue") } } } }; var clientEdmModel = new ClientEdmModel(ODataProtocolVersion.V4); var context = new DataServiceContext(); var materializerEntry = MaterializerEntry.CreateEntry(odataEntry, OData.ODataFormat.Json, true, clientEdmModel); MaterializerNavigationLink.CreateLink(complexP, MaterializerEntry.CreateEntry(complexResource, OData.ODataFormat.Json, true, clientEdmModel)); MaterializerFeed.CreateFeed(complexColResourceSet, items); MaterializerNavigationLink.CreateLink(complexColP, complexColResourceSet); var materializerContext = new TestMaterializerContext() { Model = clientEdmModel, Context = context }; var adapter = new EntityTrackingAdapter(new TestEntityTracker(), MergeOption.OverwriteChanges, clientEdmModel, context); QueryComponents components = new QueryComponents(new Uri("http://foo.com/Service"), new Version(4, 0), typeof(EntityType), null, new Dictionary <Expression, Expression>()); var entriesMaterializer = new ODataEntriesEntityMaterializer(new OData.ODataResource[] { odataEntry }, materializerContext, adapter, components, typeof(EntityType), null, OData.ODataFormat.Json); var customersRead = new List <EntityType>(); while (entriesMaterializer.Read()) { customersRead.Add(entriesMaterializer.CurrentValue as EntityType); } customersRead.Should().HaveCount(1); customersRead[0].KeyProp.Should().Be(1); customersRead[0].ComplexProp.Should().Equals(new ComplexType { Age = 11, Name = "June" }); customersRead[0].ColorProp.Should().Equals(Color.Blue); customersRead[0].PrimitiveCollection.Should().Equals(new List <int> { 1, 2, 3 }); ComplexType complex1 = new ComplexType { Name = "Aug", Age = 8, Color = Color.White }; ComplexType complex2 = new ComplexType { Name = "Sep", Age = 9, Color = Color.Blue }; customersRead[0].ComplexCollection.Should().Equals(new List <ComplexType> { complex1, complex2 }); customersRead[0].EnumCollection.Should().Equals(new List <Color> { Color.White, Color.Blue }); }
/// <summary>Writes a resource.</summary> /// <param name="resource">The resource or item to write.</param> /// <returns>This ODataWriter, allowing for chaining operations.</returns> public ODataWriter Write(ODataResource resource) { WriteStart(resource); WriteEnd(); return(this); }
/// <summary>Starts the writing of a resource.</summary> /// <param name="resource">The resource or item to write.</param> public abstract void WriteStart(ODataResource resource);
/// <summary> /// Provide additional serialization information to the <see cref="ODataWriter"/> for <paramref name="resource"/>. /// </summary> /// <param name="resource">The instance to set the serialization info.</param> /// <param name="serializationInfo">The serialization info to set.</param> public static void SetSerializationInfo(this ODataResource resource, ODataResourceSerializationInfo serializationInfo) { ExceptionUtils.CheckArgumentNotNull(resource, "resource"); resource.SerializationInfo = serializationInfo; }
/// <summary> Asynchronously start writing a resource. </summary> /// <returns>A task instance that represents the asynchronous write operation.</returns> /// <param name="resource">The resource or item to write.</param> public virtual Task WriteStartAsync(ODataResource resource) { return(TaskUtils.GetTaskForSynchronousOperation(() => this.WriteStart(resource))); }
public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model) { if (value == null) { value = new ODataNullValue(); } if (model == null) { model = Microsoft.OData.Edm.EdmCoreModel.Instance; } ODataNullValue nullValue = value as ODataNullValue; if (nullValue != null) { return(ExpressionConstants.KeywordNull); } ODataCollectionValue collectionValue = value as ODataCollectionValue; if (collectionValue != null) { return(ODataUriConversionUtils.ConvertToUriCollectionLiteral(collectionValue, model, version)); } ODataEnumValue enumValue = value as ODataEnumValue; if (enumValue != null) { return(ODataUriConversionUtils.ConvertToUriEnumLiteral(enumValue, version)); } ODataResource resource = value as ODataResource; if (resource != null) { return(ODataUriConversionUtils.ConvertToUriEntityLiteral(resource, model)); } ODataEntityReferenceLink link = value as ODataEntityReferenceLink; if (link != null) { return(ODataUriConversionUtils.ConvertToUriEntityReferenceLiteral(link, model)); } ODataEntityReferenceLinks links = value as ODataEntityReferenceLinks; if (links != null) { return(ODataUriConversionUtils.ConvertToUriEntityReferencesLiteral(links, model)); } var list = value as IEnumerable <ODataResource>; if (list != null) { return(ODataUriConversionUtils.ConvertToUriEntitiesLiteral(list, model)); } // Try to convert uints to their underlying type first according to the model. value = model.ConvertToUnderlyingTypeIfUIntValue(value); return(ODataUriConversionUtils.ConvertToUriPrimitiveLiteral(value, version)); }