/// <inheritdoc/>
        public void Apply(IonSerializationOptions options, IIonWriter writer, Type type)
        {
            IonAnnotateTypeAttribute annotateType = this.ShouldAnnotate(type, type);

            if (annotateType == null && options.IncludeTypeInformation)
            {
                // the serializer insists on type information being included
                annotateType = new IonAnnotateTypeAttribute();
            }

            if (annotateType != null)
            {
                writer.AddTypeAnnotation(options.AnnotationConvention.Apply(options, annotateType, type));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Serialize value to Ion using serializer identified by <see cref="IonAnnotateTypeAttribute"/>.
        /// </summary>
        ///
        /// <param name="writer">The Ion writer to be used for serialization.</param>
        /// <param name="item">The value to serialize.</param>
        /// <param name="annotationAttribute">The <see cref="IonAnnotateTypeAttribute"/> to identify custom serializer for serialization.</param>
        /// <typeparam name="T">The type of data to serialize.</typeparam>
        ///
        /// <returns>True if the value is serialized to Ion using AnnotatedIonSerializer. Otherwise return false.</returns>
        internal bool TryAnnotatedIonSerializer <T>(IIonWriter writer, T item, IonAnnotateTypeAttribute annotationAttribute)
        {
            if (this.options.AnnotatedIonSerializers != null)
            {
                string standardizedAnnotation = this.options.AnnotationConvention.Apply(this.options, annotationAttribute, item.GetType());
                if (this.options.AnnotatedIonSerializers.ContainsKey(standardizedAnnotation))
                {
                    var serializer = this.options.AnnotatedIonSerializers[standardizedAnnotation];
                    writer.AddTypeAnnotation(standardizedAnnotation);
                    serializer.Serialize(writer, item);
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        /// <inheritdoc/>
        public override void Serialize(IIonWriter writer, object item)
        {
            this.options.TypeAnnotator.Apply(this.options, writer, this.targetType);
            writer.StepIn(IonType.Struct);

            var serializedIonFields = new HashSet <string>();

            // Serialize the values returned from IonPropertyGetter annotated getter methods.
            foreach (var(method, ionPropertyName) in this.GetGetters())
            {
                var getValue = method.Invoke(item, Array.Empty <object>());

                writer.SetFieldName(ionPropertyName);
                this.ionSerializer.Serialize(writer, getValue);

                serializedIonFields.Add(ionPropertyName);
            }

            // Serialize any properties that satisfy the options/attributes.
            foreach (var property in this.GetValidProperties(true))
            {
                var ionPropertyName = this.IonFieldNameFromProperty(property);
                if (serializedIonFields.Contains(ionPropertyName))
                {
                    // This Ion property name was already serialized.
                    continue;
                }

                if (this.options.IgnoreReadOnlyProperties && IsReadOnlyProperty(property))
                {
                    continue;
                }

                var propertyValue = property.GetValue(item);
                if (this.options.IgnoreNulls && propertyValue == null)
                {
                    continue;
                }

                if (this.options.IgnoreDefaults && propertyValue == default)
                {
                    continue;
                }

                writer.SetFieldName(ionPropertyName);
                var ionAnnotateType = (IonAnnotateTypeAttribute)Attribute.GetCustomAttribute(property, typeof(IonAnnotateTypeAttribute), false);
                if (ionAnnotateType != null)
                {
                    if (this.ionSerializer.TryAnnotatedIonSerializer(writer, propertyValue, ionAnnotateType))
                    {
                        continue;
                    }

                    // Use the actual type of property at runtime to handle dynamic type
                    var propertyType = propertyValue != null?propertyValue.GetType() : property.PropertyType;

                    writer.AddTypeAnnotation(this.options.AnnotationConvention.Apply(this.options, ionAnnotateType, propertyType));
                }

                this.ionSerializer.Serialize(writer, propertyValue);
                serializedIonFields.Add(ionPropertyName);
            }

            // Serialize any fields that satisfy the options/attributes.
            foreach (var field in this.Fields())
            {
                var ionFieldName = GetFieldName(field);
                if (serializedIonFields.Contains(ionFieldName))
                {
                    // This Ion field name was already serialized.
                    continue;
                }

                if (this.options.IgnoreReadOnlyFields && field.IsInitOnly)
                {
                    continue;
                }

                var fieldValue = field.GetValue(item);
                if (this.options.IgnoreNulls && fieldValue == null)
                {
                    continue;
                }

                if (this.options.IgnoreDefaults && fieldValue == default)
                {
                    continue;
                }

                writer.SetFieldName(ionFieldName);
                var ionAnnotateType = (IonAnnotateTypeAttribute)Attribute.GetCustomAttribute(field, typeof(IonAnnotateTypeAttribute), false);
                if (ionAnnotateType != null && this.ionSerializer.TryAnnotatedIonSerializer(writer, fieldValue, ionAnnotateType))
                {
                    continue;
                }

                this.ionSerializer.Serialize(writer, fieldValue);
            }

            writer.StepOut();
        }
Beispiel #4
0
 public void Apply(IonSerializationOptions options, IIonWriter writer, Type type)
 {
     writer.AddTypeAnnotation("custom");
 }