/// <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));
            }
        }
Esempio n. 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);
        }
 /// <inheritdoc/>
 public string Apply(IonSerializationOptions options, IonAnnotateTypeAttribute annotateType, Type type)
 {
     annotateType.Prefix ??= options.TypeAnnotationPrefix.Apply(type);
     annotateType.Name ??= options.TypeAnnotationName.Apply(type);
     return(annotateType.Prefix + "." + annotateType.Name);
 }