private void SetElementPropertyValue(XSerializerXmlReader reader, bool hasInstanceBeenCreated, ISerializeOptions options, IDictionary <string, object> expando) { var propertyName = reader.Name; var serializer = DynamicSerializer.GetSerializer <object>(null, _options.WithRootElementName(reader.Name)); var value = serializer.DeserializeObject(reader, options); expando[propertyName] = value; }
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 static IXmlSerializerInternal GetSerializer <T>(EncryptAttribute encryptAttribute, IXmlSerializerOptions options) { var serializer = new DynamicSerializer(encryptAttribute, options); if (typeof(T) == typeof(object)) { return(serializer); } if (typeof(T) == typeof(ExpandoObject)) { return(new DynamicSerializerExpandoObjectProxy(serializer)); } throw new InvalidOperationException("The only valid generic arguments for DynamicSerializer.GetSerializer<T> are object, dynamic, and ExpandoObject"); }
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; })); }
public DynamicSerializerExpandoObjectProxy(DynamicSerializer serializer) { _serializer = serializer; }