public void Read_Roundtrip_PrimitiveCollection() { int[] numbers = Enumerable.Range(0, 100).ToArray(); ODataCollectionSerializer serializer = new ODataCollectionSerializer(_intCollectionType, new DefaultODataSerializerProvider(_model)); ODataCollectionDeserializer deserializer = new ODataCollectionDeserializer(_intCollectionType, _deserializerProvider); MemoryStream stream = new MemoryStream(); ODataMessageWrapper message = new ODataMessageWrapper(stream); serializer.WriteObject(numbers, new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), _model), new ODataSerializerContext { ServiceOperationName = "Property" }); stream.Seek(0, SeekOrigin.Begin); IEnumerable readnumbers = deserializer.Read(new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), _model), new ODataDeserializerContext()) as IEnumerable; Assert.Equal(numbers, readnumbers.Cast <int>()); }
public void AddTypeNameAnnotationAsNeeded_AddsAnnotationWithNull_InJsonLightNoMetadataMode() { // Arrange string expectedTypeName = "TypeName"; ODataCollectionValue value = new ODataCollectionValue { TypeName = expectedTypeName }; // Act ODataCollectionSerializer.AddTypeNameAnnotationAsNeeded(value, ODataMetadataLevel.None); // Assert ODataTypeAnnotation annotation = value.TypeAnnotation; Assert.NotNull(annotation); // Guard Assert.Null(annotation.TypeName); }
public void CreateODataCollectionValue_SetsTypeName() { // Arrange IEnumerable enumerable = new int[] { 1, 2, 3 }; ODataSerializerContext context = new ODataSerializerContext { Model = EdmCoreModel.Instance }; Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>(); serializerProvider.Setup(s => s.GetEdmTypeSerializer(It.IsAny <IEdmTypeReference>())).Returns(new ODataPrimitiveSerializer()); ODataCollectionSerializer serializer = new ODataCollectionSerializer(serializerProvider.Object); // Act ODataValue oDataValue = serializer.CreateODataCollectionValue(enumerable, _edmIntType, context); // Assert ODataCollectionValue collection = Assert.IsType <ODataCollectionValue>(oDataValue); Assert.Equal("Collection(Edm.Int32)", collection.TypeName); }
public void Read_Roundtrip_ComplexCollection() { Address[] addresses = new[] { new Address { City = "Redmond", ZipCode = "1", Street = "A", State = "123" }, new Address { City = "Seattle", ZipCode = "2", Street = "S", State = "321" } }; ODataCollectionSerializer serializer = new ODataCollectionSerializer(new DefaultODataSerializerProvider()); ODataCollectionDeserializer deserializer = new ODataCollectionDeserializer(_deserializerProvider); MemoryStream stream = new MemoryStream(); ODataMessageWrapper message = new ODataMessageWrapper(stream); ODataMessageWriterSettings settings = new ODataMessageWriterSettings { ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/") } }; ODataMessageWriter messageWriter = new ODataMessageWriter(message as IODataResponseMessage, settings, _model); ODataMessageReader messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), _model); ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "Property", Model = _model }; ODataDeserializerContext readContext = new ODataDeserializerContext() { Model = _model }; serializer.WriteObject(addresses, addresses.GetType(), messageWriter, writeContext); stream.Seek(0, SeekOrigin.Begin); IEnumerable readAddresses = deserializer.Read(messageReader, typeof(Address[]), readContext) as IEnumerable; Assert.Equal(addresses, readAddresses.Cast <Address>(), new AddressComparer()); }
public MockContainer() { serializerProvider = new DefaultODataSerializerProvider(this); collectionSerializer = new ODataCollectionSerializer(serializerProvider); primitiveSerializer = new ODataPrimitiveSerializer(); }