public void Init()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complex1 = new EdmComplexType("ns", "complex1");
            complex1.AddProperty(new EdmStructuralProperty(complex1, "p1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            model.AddElement(complex1);            
            
            EdmComplexType complex2 = new EdmComplexType("ns", "complex2");
            complex2.AddProperty(new EdmStructuralProperty(complex2, "p1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            model.AddElement(complex2);

            EdmComplexTypeReference complex2Reference = new EdmComplexTypeReference(complex2, isNullable: false);
            EdmCollectionType primitiveCollectionType = new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false));
            EdmCollectionType complexCollectionType = new EdmCollectionType(complex2Reference);
            EdmCollectionTypeReference primitiveCollectionTypeReference = new EdmCollectionTypeReference(primitiveCollectionType);
            EdmCollectionTypeReference complexCollectionTypeReference = new EdmCollectionTypeReference(complexCollectionType);

            model.AddElement(new EdmTerm("custom", "int", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "string", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "double", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "bool", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: true)));
            model.AddElement(new EdmTerm("custom", "decimal", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Decimal, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "timespan", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Duration, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "guid", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Guid, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "complex", complex2Reference));
            model.AddElement(new EdmTerm("custom", "primitiveCollection", primitiveCollectionTypeReference));
            model.AddElement(new EdmTerm("custom", "complexCollection", complexCollectionTypeReference));

            this.stream = new MemoryStream();
            this.settings = new ODataMessageWriterSettings { Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") };
            this.settings.SetServiceDocumentUri(ServiceDocumentUri);
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(model, this.stream));
        }
        public void WriteInstanceAnnotationShouldSkipAnnotationBaseOnAnnotationFilter()
        {
            this.settings = new ODataMessageWriterSettings {Version = ODataVersion.V4, ShouldIncludeAnnotation = name => false};
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(EdmCoreModel.Instance, this.stream));

            var annotation = AtomInstanceAnnotation.CreateFrom(new ODataInstanceAnnotation("Namespace.TermName", new ODataPrimitiveValue("string value")), /*target*/ null);
            this.serializer.WriteInstanceAnnotation(annotation);
            this.ValidatePayload("");
        }
        public void Init()
        {
            model = new EdmModel();
            EdmComplexType complexType = new EdmComplexType("ns", "complex");
            complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(isNullable: false)));
            model.AddElement(complexType);

            this.stream = new MemoryStream();
            this.settings = new ODataMessageWriterSettings { Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") };
            this.settings.SetServiceDocumentUri(ServiceDocumentUri);
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(model, this.stream));
            this.instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
        }
        private void VerifyPrimitiveValueRoundtrips(object clrValue, string edmTypeName, ODataVersion version, string description)
        {
            IEdmModel model = new EdmModel();
            IEdmPrimitiveTypeReference typeReference = new EdmPrimitiveTypeReference((IEdmPrimitiveType)model.FindType(edmTypeName), true);

            MemoryStream stream = new MemoryStream();
            using (ODataAtomOutputContext outputContext = new ODataAtomOutputContext(
                ODataFormat.Atom,
                new NonDisposingStream(stream),
                Encoding.UTF8,
                new ODataMessageWriterSettings() { Version = version },
                /*writingResponse*/ true,
                /*synchronous*/ true,
                model,
                /*urlResolver*/ null))
            {
                ODataAtomPropertyAndValueSerializer serializer = new ODataAtomPropertyAndValueSerializer(outputContext);
                serializer.XmlWriter.WriteStartElement("ValueElement");
                serializer.WritePrimitiveValue(
                    clrValue,
                    /*collectionValidator*/ null,
                    typeReference,
                    /*serializationTypeNameAnnotation*/ null);
                serializer.XmlWriter.WriteEndElement();
            }

            stream.Position = 0;

            object actualValue;
            using (ODataAtomInputContext inputContext = new ODataAtomInputContext(
                ODataFormat.Atom,
                stream,
                Encoding.UTF8,
                new ODataMessageReaderSettings(),
                /*readingResponse*/ true,
                /*synchronous*/ true,
                model,
                /*urlResolver*/ null))
            {
                ODataAtomPropertyAndValueDeserializer deserializer = new ODataAtomPropertyAndValueDeserializer(inputContext);
                deserializer.XmlReader.MoveToContent();
                actualValue = deserializer.ReadNonEntityValue(
                    typeReference,
                    /*duplicatePropertyNamesChecker*/ null,
                    /*collectionValidator*/ null,
                    /*validateNullValue*/ true);
            }

            if (clrValue is byte[])
            {
                ((byte[])actualValue).Should().Equal((byte[])clrValue, description);
            }
            else
            {
                actualValue.Should().Be(clrValue, description);
            }
        }
        private string SerializeProperty(ODataProperty odataProperty)
        {
            MemoryStream outputStream = new MemoryStream();
            ODataAtomOutputContext atomOutputContext = this.CreateAtomOutputContext(this.model, outputStream);
            var serializer = new ODataAtomPropertyAndValueSerializer(atomOutputContext);

            serializer.WriteTopLevelProperty(odataProperty);
            atomOutputContext.Flush();
            outputStream.Position = 0;
            string result = new StreamReader(outputStream).ReadToEnd();

            return result;
        }
Exemple #6
0
        /// <summary>
        /// Writes an <see cref="ODataProperty"/> as message payload.
        /// </summary>
        /// <param name="property">The property to write.</param>
        private void WritePropertyImplementation(ODataProperty property)
        {
            ODataAtomPropertyAndValueSerializer atomPropertyAndValueSerializer = new ODataAtomPropertyAndValueSerializer(this);

            atomPropertyAndValueSerializer.WriteTopLevelProperty(property);
        }
        private void VerifyComplexTypeRoundtrip(ODataComplexValue value, string typeName)
        {
            var typeReference = new EdmComplexTypeReference((IEdmComplexType)model.FindType(typeName), true);
            MemoryStream stream = new MemoryStream();
            using (ODataAtomOutputContext outputContext = new ODataAtomOutputContext(
                ODataFormat.Atom,
                new NonDisposingStream(stream),
                Encoding.UTF8,
                new ODataMessageWriterSettings() { Version = ODataVersion.V4 },
                /*writingResponse*/ true,
                /*synchronous*/ true,
                model,
                /*urlResolver*/ null))
            {
                ODataAtomPropertyAndValueSerializer serializer = new ODataAtomPropertyAndValueSerializer(outputContext);
                serializer.XmlWriter.WriteStartElement("ValueElement");
                serializer.WriteComplexValue(
                    value, 
                    typeReference, 
                    /*isOpenPropertyType*/ false, 
                    /*isWritingCollection*/ false, 
                    /*beforeValueAction*/ null, 
                    /*afterValueAction*/ null, 
                    new DuplicatePropertyNamesChecker(false, false), 
                    /*collectionValidator*/ null, 
                    /*projectedProperties*/ null);
                serializer.XmlWriter.WriteEndElement();
            }

            stream.Position = 0;
            object actualValue;

            using (ODataAtomInputContext inputContext = new ODataAtomInputContext(
                ODataFormat.Atom,
                stream,
                Encoding.UTF8,
                new ODataMessageReaderSettings(),
                /*readingResponse*/ true,
                /*synchronous*/ true,
                model,
                /*urlResolver*/ null))
            {
                ODataAtomPropertyAndValueDeserializer deserializer = new ODataAtomPropertyAndValueDeserializer(inputContext);
                deserializer.XmlReader.MoveToContent();
                actualValue = deserializer.ReadNonEntityValue(
                    typeReference, 
                    /*duplicatePropertyNamesChecker*/ null,
                    /*collectionValidator*/ null, 
                    /*validateNullValue*/ true);
            }

            TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value);
        }
        private string SerializePropertyWithOwningType(EdmModel model, IEdmStructuredType owningType, ODataProperty odataProperty)
        {
            MemoryStream outputStream = new MemoryStream();
            ODataAtomOutputContext atomOutputContext = this.CreateAtomOutputContext(model, outputStream);
            var serializer = new ODataAtomPropertyAndValueSerializer(atomOutputContext);

            serializer.WritePayloadStart();
            serializer.WriteProperties(
                owningType,
                new[] { odataProperty },
                /*isWritingCollection*/ false,
                null,
                null,
                new DuplicatePropertyNamesChecker(allowDuplicateProperties: true, isResponse: true),
                ProjectedPropertiesAnnotation.AllProjectedPropertiesInstance
                );
            serializer.WritePayloadEnd();

            atomOutputContext.Flush();
            outputStream.Position = 0;
            string result = new StreamReader(outputStream).ReadToEnd();

            return result;
        }
 /// <summary>
 /// Writes an <see cref="ODataProperty"/> as message payload.
 /// </summary>
 /// <param name="property">The property to write.</param>
 private void WritePropertyImplementation(ODataProperty property)
 {
     ODataAtomPropertyAndValueSerializer atomPropertyAndValueSerializer = new ODataAtomPropertyAndValueSerializer(this);
     atomPropertyAndValueSerializer.WriteTopLevelProperty(property);
 }