public async Task WriteInstanceAnnotationNameAsync_WritesInstanceAnnotationName()
        {
            await this.asyncJsonWriter.StartObjectScopeAsync();

            await ODataJsonLightWriterUtils.WriteInstanceAnnotationNameAsync(this.asyncJsonWriter, "odata.type");

            Assert.Equal("{\"@odata.type\":", this.builder.ToString());
        }
        public async Task WriteValuePropertyNameAsync_WritesValuePropertyName()
        {
            await this.asyncJsonWriter.StartObjectScopeAsync();

            await ODataJsonLightWriterUtils.WriteValuePropertyNameAsync(this.asyncJsonWriter);

            Assert.Equal("{\"value\":", this.builder.ToString());
        }
        /// <summary>
        /// Writes an instance annotation.
        /// </summary>
        /// <param name="instanceAnnotation">The instance annotation to write.</param>
        internal void WriteInstanceAnnotation(ODataInstanceAnnotation instanceAnnotation)
        {
            DebugUtils.CheckNoExternalCallers();

            string     name  = instanceAnnotation.Name;
            ODataValue value = instanceAnnotation.Value;

            Debug.Assert(!string.IsNullOrEmpty(name), "name should not be null or empty");
            Debug.Assert(!ODataAnnotationNames.IsODataAnnotationName(name), "A reserved name cannot be used as instance annotation key");
            Debug.Assert(value != null, "value should not be null because we use ODataNullValue for null instead");
            Debug.Assert(!(value is ODataStreamReferenceValue), "!(value is ODataStreamReferenceValue) -- ODataInstanceAnnotation and InstanceAnnotationCollection will throw if the value is a stream value.");
            Debug.Assert(this.valueSerializer.Model != null, "this.valueSerializer.Model != null");

            if (this.valueSerializer.Settings.ShouldSkipAnnotation(name))
            {
                return;
            }

            IEdmTypeReference expectedType = MetadataUtils.LookupTypeOfValueTerm(name, this.valueSerializer.Model);

            if (value is ODataNullValue)
            {
                if (expectedType != null && !expectedType.IsNullable)
                {
                    throw new ODataException(ODataErrorStrings.ODataAtomPropertyAndValueSerializer_NullValueNotAllowedForInstanceAnnotation(instanceAnnotation.Name, expectedType.ODataFullName()));
                }

                this.JsonWriter.WriteName(name);
                this.valueSerializer.WriteNullValue();
                return;
            }

            // If we didn't find an expected type from looking up the term in the model, treat this value the same way we would for open property values.
            // That is, write the type name (unless its a primitive value with a JSON-native type).  If we did find an expected type, treat the annotation value like a
            // declared property with an expected type. This will still write out the type if the value type is more derived than the declared type, for example.
            bool treatLikeOpenProperty = expectedType == null;

            ODataComplexValue complexValue = value as ODataComplexValue;

            if (complexValue != null)
            {
                this.JsonWriter.WriteName(name);
                this.valueSerializer.WriteComplexValue(complexValue, expectedType, false /*isTopLevel*/, treatLikeOpenProperty, this.valueSerializer.CreateDuplicatePropertyNamesChecker());
                return;
            }

            IEdmTypeReference    typeFromValue   = TypeNameOracle.ResolveAndValidateTypeNameForValue(this.valueSerializer.Model, expectedType, value, treatLikeOpenProperty);
            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                string collectionTypeNameToWrite = this.typeNameOracle.GetValueTypeNameForWriting(collectionValue, expectedType, typeFromValue, treatLikeOpenProperty);
                if (collectionTypeNameToWrite != null)
                {
                    ODataJsonLightWriterUtils.WriteODataTypePropertyAnnotation(this.JsonWriter, name, collectionTypeNameToWrite);
                }

                this.JsonWriter.WriteName(name);
                this.valueSerializer.WriteCollectionValue(collectionValue, expectedType, false /*isTopLevelProperty*/, false /*isInUri*/, treatLikeOpenProperty);
                return;
            }

            ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue;

            Debug.Assert(primitiveValue != null, "Did we add a new subclass of ODataValue?");

            string primitiveTypeNameToWrite = this.typeNameOracle.GetValueTypeNameForWriting(primitiveValue, expectedType, typeFromValue, treatLikeOpenProperty);

            if (primitiveTypeNameToWrite != null)
            {
                ODataJsonLightWriterUtils.WriteODataTypePropertyAnnotation(this.JsonWriter, name, primitiveTypeNameToWrite);
            }

            this.JsonWriter.WriteName(name);
            this.valueSerializer.WritePrimitiveValue(primitiveValue.Value, expectedType);
        }
 public void WriteInstanceAnnotationName_WritesInstanceAnnotationName()
 {
     this.jsonWriter.StartObjectScope();
     ODataJsonLightWriterUtils.WriteInstanceAnnotationName(this.jsonWriter, "odata.type");
     Assert.Equal("{\"@odata.type\":", this.builder.ToString());
 }
 public void WritePropertyAnnotationName_WritesPropertyAnnotationName()
 {
     this.jsonWriter.StartObjectScope();
     ODataJsonLightWriterUtils.WritePropertyAnnotationName(this.jsonWriter, "FavoriteColor", "odata.type");
     Assert.Equal("{\"[email protected]\":", this.builder.ToString());
 }
 public void WriteValuePropertyName_WritesValuePropertyName()
 {
     this.jsonWriter.StartObjectScope();
     ODataJsonLightWriterUtils.WriteValuePropertyName(this.jsonWriter);
     Assert.Equal("{\"value\":", this.builder.ToString());
 }