private void SerializeExpandoObject(XSerializerXmlTextWriter writer, IDictionary <string, object> expando, ISerializeOptions options) { if (expando == null && !options.ShouldEmitNil) { return; } writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); using (writer.WriteDefaultNamespace(_options.DefaultNamespace)) { if (expando == null) { writer.WriteNilAttribute(); writer.WriteEndElement(); return; } var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var property in expando) { if (property.Value == null) { continue; } IXmlSerializerInternal serializer; if (property.Value is ExpandoObject) { serializer = DynamicSerializer.GetSerializer <ExpandoObject>(null, _options.WithRootElementName(property.Key)); } else { serializer = CustomSerializer.GetSerializer(property.Value.GetType(), null, _options.WithRootElementName(property.Key)); } serializer.SerializeObject(writer, property.Value, options); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndElement(); } }
public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { var expando = instance as ExpandoObject; if (expando != null || instance == null) { SerializeExpandoObject(writer, expando, options); return; } IXmlSerializerInternal serializer; if (!options.ShouldAlwaysEmitTypes || instance.IsAnonymous()) { serializer = CustomSerializer.GetSerializer(instance.GetType(), _encryptAttribute, _options); } else { serializer = CustomSerializer.GetSerializer(typeof(object), _encryptAttribute, _options.WithAdditionalExtraTypes(instance.GetType())); } serializer.SerializeObject(writer, instance, options); }
public IXmlSerializerInternal GetSerializer <T>(EncryptAttribute encryptAttribute, IXmlSerializerOptions options) { return(_serializerCache.GetOrAdd( Tuple.Create(typeof(T), encryptAttribute, options), _ => { var type = typeof(T); if (type == typeof(object) || type == typeof(ExpandoObject)) { return DynamicSerializer.GetSerializer <T>(encryptAttribute, options); } IXmlSerializerInternal serializer; if (type.IsPrimitiveLike() || type.IsNullablePrimitiveLike() || ValueTypes.IsRegistered(type)) { serializer = new XmlElementSerializer <T>(encryptAttribute, options); } else if (type.IsAssignableToNonGenericIDictionary() || type.IsAssignableToGenericIDictionary() || type.IsReadOnlyDictionary()) { serializer = DictionarySerializer.GetSerializer(type, encryptAttribute, options); } else if (type.IsAssignableToNonGenericIEnumerable() || type.IsAssignableToGenericIEnumerable()) { serializer = ListSerializer.GetSerializer(type, encryptAttribute, options, null); } else { serializer = CustomSerializer.GetSerializer(type, encryptAttribute, options); } return serializer; })); }