public void SetElementPropertyValue(ISerializeOptions options, out bool shouldIssueRead) { SerializableProperty property; if (_caseSensitiveSerializableProperties.TryGetValue(_reader.Name, out property)) { var value = property.ReadValue(_reader, options); if (_accumulatedValues.ContainsKey(property.Name.ToLower())) { var existingValue = _accumulatedValues[property.Name.ToLower()]; Combine(existingValue, value); } else { _accumulatedValues.Add(property.Name.ToLower(), value); } shouldIssueRead = !property.ReadsPastLastElement; } else { shouldIssueRead = true; } }
public void SetTextNodePropertyValue(ISerializeOptions options) { if (_textNodeProperty != null) { _textNodeProperty.ReadValue(_reader, _instance, options); } }
public static void SerializeObject( this IXmlSerializerInternal serializer, Stream stream, object instance, Encoding encoding, Formatting formatting, ISerializeOptions options) { options = options.WithNewSerializationState(); StreamWriter streamWriter = null; try { streamWriter = new StreamWriter(stream, encoding ?? Encoding.UTF8); var xmlWriter = new XSerializerXmlTextWriter(streamWriter, options) { Formatting = formatting }; serializer.SerializeObject(xmlWriter, instance, options); xmlWriter.Flush(); } finally { if (streamWriter != null) { streamWriter.Flush(); } } }
public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options) { var isNil = reader.IsNil(); if (isNil && reader.IsEmptyElement) { return(null); } object deserializedObject; var type = reader.GetXsdType <object>(_options.ExtraTypes); if (type != null) { var serializer = XmlSerializerFactory.Instance.GetSerializer(type, _encryptAttribute, _options.WithRootElementName(reader.Name)); deserializedObject = serializer.DeserializeObject(reader, options); } else { deserializedObject = DeserializeToDynamic(reader, options); } return (isNil ? null : deserializedObject); }
public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options) { if (ValueTypes.IsRegistered(typeof(T))) { while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } } if (reader.IsNil()) { return(default(T)); } var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options); var value = reader.ReadString(); if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return(_valueConverter.ParseString(value, options)); }
public void WriteValue(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { if (_shouldSerializeFunc(instance)) { var value = _getValueFunc(instance); _serializer.Value.SerializeObject(writer, value, options); } }
private static object ParseStringForTimeSpan(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(new TimeSpan()); } return(TimeSpan.Parse(value, CultureInfo.InvariantCulture)); }
public void SetTextNodePropertyValue(ISerializeOptions options) { if (_textNodeProperty != null) { var value = _textNodeProperty.ReadValue(_reader, options); _accumulatedValues[_textNodeProperty.Name.ToLower()] = value; } }
public object ParseString(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } return new Uri(value); }
private static object ParseStringForNullableTimeSpan(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(null); } return(TimeSpan.Parse(value, options.GetCulture())); }
private static object ParseStringForNullableGuid(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(null); } return(Guid.Parse(value)); }
private static string GetStringFromChar(object value, ISerializeOptions options) { if (options.ShouldSerializeCharAsInt) { value = (int)(char)value; } return(value.ToString()); }
public object ParseString(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(null); } return(Type.GetType(value)); }
public void StageAttributeValue(ISerializeOptions options) { SerializableProperty property; if (_attributeProperties.TryGetValue(_reader.Name, out property)) { _setPropertyActions.Add(() => property.ReadValue(_reader, _instance, options)); } }
private static object ParseStringForChar(string value, ISerializeOptions options) { if (options.ShouldSerializeCharAsInt) { var charAsInt = ushort.Parse(value); return((char)charAsInt); } return((char)Convert.ChangeType(value, typeof(char))); }
public string GetString(object value, ISerializeOptions options) { var returnValue = _decoratedValueConverter.GetString(value, options); if (options.ShouldEncrypt) { returnValue = _encryptionAlgorithm.Encrypt(returnValue); } return returnValue; }
public string GetString(object value, ISerializeOptions options) { var returnValue = _decoratedValueConverter.GetString(value, options); if (options.ShouldEncrypt) { returnValue = _encryptionAlgorithm.Encrypt(returnValue); } return(returnValue); }
private static object ParseStringForNullableDateTimeOffset(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(null); } return(DateTimeOffset.Parse( value, options.GetCulture(), DateTimeStyles.RoundtripKind)); }
private static object ParseStringForDateTimeOffset(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(new DateTimeOffset()); } return(DateTimeOffset.Parse( value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)); }
public object ParseString(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } var enumTypeName = value.Substring(0, value.LastIndexOf('.')); var enumValue = value.Substring(value.LastIndexOf('.') + 1); var enumType = _enumExtraTypes.Single(t => t.Name == enumTypeName); return Enum.Parse(enumType, enumValue); }
public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options) { var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options); var value = _valueConverter.ParseString(reader.Value, options); if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return(value); }
public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options) { var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options); var value = _valueConverter.ParseString(reader.Value, options); if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return value; }
public void SerializeObject(XSerializerXmlTextWriter writer, object value, ISerializeOptions options) { if (value != null) { var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); writer.WriteValue(_valueConverter.GetString(value, options)); if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } } }
public object ParseString(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(null); } var enumTypeName = value.Substring(0, value.LastIndexOf('.')); var enumValue = value.Substring(value.LastIndexOf('.') + 1); var enumType = _enumExtraTypes.Single(t => t.Name == enumTypeName); return(Enum.Parse(enumType, enumValue)); }
public void SetElementPropertyValue(ISerializeOptions options, out bool shouldIssueRead) { SerializableProperty property; if (_caseSensitiveSerializableProperties.TryGetValue(_reader.Name, out property)) { property.ReadValue(_reader, _instance, options); shouldIssueRead = !property.ReadsPastLastElement; } else { shouldIssueRead = true; } }
private static ISerializeOptions WithNewSerializationState(this ISerializeOptions serializeOptions) { return(new SerializeOptions { EncryptionMechanism = serializeOptions.EncryptionMechanism, EncryptKey = serializeOptions.EncryptKey, Namespaces = serializeOptions.Namespaces, SerializationState = new SerializationState(), ShouldAlwaysEmitTypes = serializeOptions.ShouldAlwaysEmitTypes, ShouldEmitNil = serializeOptions.ShouldEmitNil, ShouldEncrypt = serializeOptions.ShouldEncrypt, ShouldRedact = serializeOptions.ShouldRedact, ShouldIgnoreCaseForEnum = serializeOptions.ShouldIgnoreCaseForEnum }); }
public void StageAttributeValue(ISerializeOptions options) { SerializableProperty property; if (_attributeProperties.TryGetValue(_reader.Name, out property)) { _setPropertyActions.Add( () => { var value = property.ReadValue(_reader, options); _accumulatedValues[property.Name.ToLower()] = value; }); } }
public void SerializeObject(XSerializerXmlTextWriter writer, object value, ISerializeOptions options) { var wasEmptyWriter = writer.IsEmpty; writer.WriteStartDocument(); if (value != null) { WriteElement(writer, w => w.WriteValue(_valueConverter.GetString(value, options)), options); } else if (_alwaysEmitNil || options.ShouldEmitNil || wasEmptyWriter) { WriteElement(writer, w => w.WriteNilAttribute(), options); } }
public string GetString(object value, ISerializeOptions options) { var type = value as Type; if (type == null) { return(null); } var typeString = _redactAttribute != null ? _redactAttribute.Redact(type, options.ShouldRedact) : GetStringValue(type); return(typeString); }
public string GetString(object value, ISerializeOptions options) { var uri = value as Uri; if (uri == null) { return(null); } var uriString = _redactAttribute != null ? _redactAttribute.Redact(uri, options.ShouldRedact) : uri.ToString(); return(uriString); }
public string GetString(object value, ISerializeOptions options) { var uri = value as Uri; if (uri == null) { return null; } var uriString = _redactAttribute != null ? _redactAttribute.Redact(uri, options.ShouldRedact) : uri.ToString(); return uriString; }
public string GetString(object value, ISerializeOptions options) { var type = value as Type; if (type == null) { return null; } var typeString = _redactAttribute != null ? _redactAttribute.Redact(type, options.ShouldRedact) : GetStringValue(type); return typeString; }
public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { if (instance == null && !options.ShouldEmitNil) { return; } writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); using (writer.WriteDefaultNamespace(_options.DefaultNamespace)) { if (instance == null) { writer.WriteNilAttribute(); writer.WriteEndElement(); return; } var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var item in GetDictionaryEntries(instance)) { writer.WriteStartElement("Item"); if (item.Key != null) { _keySerializer.SerializeObject(writer, item.Key, options); } if (item.Value != null) { _valueSerializer.SerializeObject(writer, item.Value, options); } writer.WriteEndElement(); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndElement(); } }
public static void SerializeObject( this IXmlSerializerInternal serializer, TextWriter writer, object instance, Formatting formatting, ISerializeOptions options) { options = options.WithNewSerializationState(); var xmlWriter = new XSerializerXmlTextWriter(writer, options) { Formatting = formatting }; serializer.SerializeObject(xmlWriter, instance, options); xmlWriter.Flush(); }
public string GetString(object value, ISerializeOptions options) { var enumValue = value as Enum; if (enumValue == null) { return null; } var enumStringValue = _redactAttribute != null ? _redactAttribute.Redact(enumValue, options.ShouldRedact) : value.ToString(); var combinedValue = value.GetType().Name + "." + enumStringValue; return combinedValue; }
public string GetString(object value, ISerializeOptions options) { var enumValue = value as Enum; if (enumValue == null) { return(null); } var enumStringValue = _redactAttribute != null ? _redactAttribute.Redact(enumValue, options.ShouldRedact) : value.ToString(); var combinedValue = value.GetType().Name + "." + enumStringValue; return(combinedValue); }
public void SerializeObject(XSerializerXmlTextWriter writer, object value, ISerializeOptions options) { if (value != null) { writer.WriteStartAttribute(_attributeName); // TODO: include namespaces var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); writer.WriteString(_valueConverter.GetString(value, options)); if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } writer.WriteEndAttribute(); } }
private static object ParseStringForBool(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return(default(bool)); } if (value == "1") { return(true); } if (value == "0") { return(false); } return(Convert.ToBoolean(value)); }
public static string SerializeObject( this System.Xml.Serialization.XmlSerializer serializer, object instance, Encoding encoding, Formatting formatting, ISerializeOptions options) { var sb = new StringBuilder(); using (var stringWriter = new StringWriterWithEncoding(sb, encoding ?? Encoding.UTF8)) { using (var xmlWriter = new XSerializerXmlTextWriter(stringWriter, options)) { xmlWriter.Formatting = formatting; serializer.Serialize(xmlWriter, instance, options.Namespaces); } } return sb.ToString(); }
private static object ParseStringForNullableGuid(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } return Guid.Parse(value); }
private static object ParseStringForNullableTimeSpan(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } return TimeSpan.Parse(value, CultureInfo.InvariantCulture); }
private static object ParseStringForNullableDateTimeOffset(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } return DateTimeOffset.Parse( value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); }
public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options) { object collection = null; var hasInstanceBeenCreated = false; bool shouldIssueRead; var setIsDecryptionEnabledBackToFalse = false; if (_options.RootElementName == null) { setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options); collection = _createCollection(); hasInstanceBeenCreated = true; } do { shouldIssueRead = true; switch (reader.NodeType) { case XmlNodeType.Element: if (_options.RootElementName != null) { if (reader.Name == _options.RootElementName) { if (reader.IsNil()) { if (reader.IsEmptyElement) { return null; } collection = null; hasInstanceBeenCreated = true; } else { setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options); collection = _createCollection(); hasInstanceBeenCreated = true; if (reader.IsEmptyElement) { if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return collection; } } break; } } else { // If there's no root element, and we encounter another element, we're done - get out! if (reader.Name != _itemElementName) { if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return collection == null ? null : CheckAndReturn(hasInstanceBeenCreated, collection); } } if (reader.Name == _itemElementName) { var item = DeserializeItem(reader, _itemSerializer, hasInstanceBeenCreated, options, out shouldIssueRead); if (collection != null) { AddItemToCollection(collection, item); } } break; case XmlNodeType.EndElement: if (_options.RootElementName != null) { if (reader.Name == _options.RootElementName) { if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return collection == null ? null : CheckAndReturn(hasInstanceBeenCreated, collection); } } else { if (reader.Name != _itemElementName) { if (setIsDecryptionEnabledBackToFalse) { reader.IsDecryptionEnabled = false; } return collection == null ? null : CheckAndReturn(hasInstanceBeenCreated, collection); } } break; } } while (reader.ReadIfNeeded(shouldIssueRead)); throw new InvalidOperationException("Deserialization error: attempted to return a deserialized instance before it was created."); }
public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options) { if (instance == null && !options.ShouldEmitNil) { return; } if (_options.RootElementName != null) { writer.WriteStartDocument(); writer.WriteStartElement(_options.RootElementName); writer.WriteDefaultDocumentNamespaces(); writer.WriteDefaultNamespace(_options.DefaultNamespace).Dispose(); } if (instance == null) { writer.WriteNilAttribute(); } else { var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options); foreach (var item in (IEnumerable)instance) { _itemSerializer.SerializeObject(writer, item, options); } if (setIsEncryptionEnabledBackToFalse) { writer.IsEncryptionEnabled = false; } } if (_options.RootElementName != null) { writer.WriteEndElement(); } }
private XSerializerXmlTextWriter(EncryptingTextWriter encryptingTextWriter, ISerializeOptions options) : base(encryptingTextWriter) { _encryptingTextWriter = encryptingTextWriter; _options = options; }
private static object DeserializeItem(XSerializerXmlReader reader, IXmlSerializerInternal serializer, bool hasInstanceBeenCreated, ISerializeOptions options, out bool shouldIssueRead) { if (!hasInstanceBeenCreated) { throw new InvalidOperationException("Deserialization error: attempted to deserialize an item before creating its list."); } var deserialized = serializer.DeserializeObject(reader, options); shouldIssueRead = true; return deserialized; }
public string GetString(object value, ISerializeOptions options) { return _getString(value, options); }
private static string GetStringFromTimeSpan(object value, ISerializeOptions options) { var timeSpan = (TimeSpan)value; return timeSpan.ToString("G"); }
private static string GetStringFromDateTimeOffset(object value, ISerializeOptions options) { var dateTimeOffset = (DateTimeOffset)value; return dateTimeOffset.ToString("O"); }
public object ParseString(string value, ISerializeOptions options) { return _parseString(value, options); }
private static string GetStringFromBool(object value, ISerializeOptions options) { return value.ToString().ToLower(); }
private static string GetStringFromChar(object value, ISerializeOptions options) { if (options.ShouldSerializeCharAsInt) { value = (int)(char)value; } return value.ToString(); }
private static string GetStringFromNullableDateTimeOffset(object value, ISerializeOptions options) { return value == null ? null : GetStringFromDateTimeOffset(value, options); }
private static object ParseStringForBool(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return default(bool); } if (value == "1") { return true; } if (value == "0") { return false; } return Convert.ToBoolean(value); }
private static string GetStringFromNullableTimeSpan(object value, ISerializeOptions options) { return value == null ? null : GetStringFromTimeSpan(value, options); }
private static object ParseStringForNullableBool(string value, ISerializeOptions options) { return string.IsNullOrEmpty(value) ? null : ParseStringForBool(value, options); }
private static object ParseStringForNullableChar(string value, ISerializeOptions options) { if (string.IsNullOrEmpty(value)) { return null; } if (options.ShouldSerializeCharAsInt) { var charAsInt = ushort.Parse(value); return (char)charAsInt; } return (char)Convert.ChangeType(value, typeof(char)); }
public XSerializerXmlTextWriter(TextWriter writer, ISerializeOptions options) : this(new EncryptingTextWriter(writer, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState), options) { }