protected override string SerializeObject <T>(T serializableValue, ISerializerOptions serializerOptions = null) { using (var writer = new StringWriter()) { using (XmlWriter xmlWriter = new XmlTextWriter(writer)) { DataContractSerializer serializer = null; if (serializerOptions != null) { var options = serializerOptions as DataContractSerializerOptions; if (options != null && options.KnownTypes != null) { serializer = new DataContractSerializer(typeof(T), options.RootName, options.RootNamespace, options.KnownTypes); } } if (serializer == null) { serializer = new DataContractSerializer(typeof(T)); } serializer.WriteObject(xmlWriter, serializableValue); return(writer.ToString()); } } }
private Config ConvertOptions(ISerializerOptions options) { return(new Config { //AllowTrailingCommas = options.AllowTrailingCommas, IncludeNullValues = !options.IgnoreNullValues, //WriteIndented = options.WriteIndented }); }
public ReadOnlySpan <byte> SerializeJson <T>(T value, ISerializerOptions options) { var jsonString = JsonSerializer.Serialize(value, options.JsonOptions); var bytes = Encoding.UTF8.GetBytes(jsonString); var readOnlySpan = new ReadOnlySpan <byte>(bytes, 0, bytes.Length); return(readOnlySpan); }
public virtual T DeSerialize <T>(string deSerializableValue, ISerializerOptions serializerOptions) { var returnValue = DeSerializeObject(typeof(T), deSerializableValue, serializerOptions); if (returnValue != null) { return((T)returnValue); } return(default(T)); }
protected override string SerializeObject <T>(T serializableValue, ISerializerOptions serializerOptions = null) { if (serializerOptions == null) { return(JsonConvert.SerializeObject(serializableValue)); } else { var jsonSerOp = serializerOptions as JsonSerializerOptions; return(JsonConvert.SerializeObject(serializableValue, jsonSerOp.Formatting, jsonSerOp.JsonSerializerSettings)); } }
public async Task <T> DeserializeJsonAsync <T>(string filePath, ISerializerOptions options = null) { if (!File.Exists(filePath)) { throw new FileNotFoundException("File does not exist.", filePath); } var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); var value = await DeserializeJsonAsync <T>(fileStream, options); return(value); }
public T DeserializeJson <T>(Stream stream, ISerializerOptions options) { var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); var jsonBytes = memoryStream.ToArray(); var readOnlySpan = new ReadOnlySpan <byte>(jsonBytes, 0, jsonBytes.Length); var value = DeserializeJson <T>(readOnlySpan, options); return(value); }
public T DeserializeJson <T>(string filePath, ISerializerOptions options) { if (!File.Exists(filePath)) { throw new FileNotFoundException("File does not exist.", filePath); } var fileBytes = File.ReadAllBytes(filePath); var readOnlySpan = new ReadOnlySpan <byte>(fileBytes, 0, fileBytes.Length); var value = JsonSerializer.Deserialize <T>(readOnlySpan, options.JsonOptions); return(value); }
/// <summary> /// Serializes the specified collection. /// </summary> /// <param name="obj">The obj.</param> /// <param name="checkForObjectName">if set to <see langword="true"/> [check for object name].</param> /// <returns></returns> public ICollection<object> FromCollection(ICollection collection, ISerializerOptions options) { var list = new List<object>(collection.Count); foreach (var item in collection) { if (item is ICollection && !(item is IDictionary<string, object>)) list.Add(FromCollection((ICollection)item, options)); else list.Add(FromObject(item, options)); } return list; }
protected override string SerializeObject <T>(T serializableValue, ISerializerOptions serializerOptions) { XmlSerializer serializer = null; if (serializerOptions != null) { var options = serializerOptions as XMLSerializerOptions; if (options != null && options.ExtraTypes != null) { serializer = new XmlSerializer(typeof(T), options.ExtraTypes); } } if (serializer == null) { serializer = new XmlSerializer(typeof(T)); } MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms, Encoding.UTF8); StreamReader sr = null; string result = ""; try { serializer.Serialize(sw, serializableValue); ms.Position = 0; sr = new StreamReader(ms, Encoding.UTF8); result = sr.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { sw.Close(); if (sr != null) { sr.Close(); } ms.Close(); } return(result); }
protected override string SerializeObject <T>(T serializableValue, ISerializerOptions serializerOptions) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); StreamReader sr = null; string result = ""; try { formatter.Serialize(ms, serializableValue); ms.Position = 0; sr = new StreamReader(ms, Encoding.UTF8); result = sr.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { try { if (sr != null) { sr.Close(); } ms.Close(); } catch (Exception) { } } return(result); }
public T Deserialize <T>(string str, ISerializerOptions options) { return(JsonSerializer.Deserialize <T>(str, (JsonSerializerOptions)options.ShimSpecificOptions)); }
public async Task SerializeJsonAsync <T>(T value, string outputPath, ISerializerOptions options) { var fileStream = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write); await JsonSerializer.SerializeAsync(fileStream, value, options.JsonOptions); }
public string Serialize(object obj, ISerializerOptions options) { return(JsonSerializer.Serialize(obj, (JsonSerializerOptions)options.ShimSpecificOptions)); }
public void SerializeJson <T>(T value, string outputPath, ISerializerOptions options) { var jsonString = JsonSerializer.Serialize(value, options.JsonOptions); File.WriteAllText(outputPath, jsonString); }
public virtual string Serialize <T>(T serializableValue, ISerializerOptions serializerOptions) { var returnValue = SerializeObject <T>(serializableValue, serializerOptions); return(returnValue); }
public object DeSerialize(Type type, string deSerializableValue, ISerializerOptions serializerOptions) { var returnValue = DeSerializeObject(type, deSerializableValue, serializerOptions); return(returnValue); }
public async Task <T> DeserializeJsonAsync <T>(Stream stream, ISerializerOptions options = null) { var value = await JsonSerializer.DeserializeAsync <T>(stream, options.JsonOptions); return(value); }
public T DeserializeJson <T>(ReadOnlySpan <byte> utf8Json, ISerializerOptions options) { var value = JsonSerializer.Deserialize <T>(utf8Json, options.JsonOptions); return(value); }
protected abstract string SerializeObject <T>(T serializableValue, ISerializerOptions serializerOptions = null);
protected abstract object DeSerializeObject(Type type, string deserializableValue, ISerializerOptions serializerOptions = null);
public void Configure(ISerializerOptions options) { JsonSerializerOptionsCurrent = ConvertOptions(options); }
public string Serialize(object obj, ISerializerOptions options) { var jsonSerializerOptions = ConvertOptions(options); return(this.Serialize(obj, jsonSerializerOptions)); }
protected override object DeSerializeObject(Type type, string deserializableValue, ISerializerOptions serializerOptions) { // Yeni bir Personel Nesnesi Yaratıyoruz Personel ReturnedEmployee; // DeSerialize işleminde kullanılmak üzere yeni bir XmlSerializer // nesnesi yaratıyor ve Serialize edilmiş verinin hangi nesne(Class) tipine çevirileceğini gösteriyoruz. XmlSerializer serializer = null; if (serializerOptions != null) { var options = serializerOptions as XMLSerializerOptions; if (options != null && options.ExtraTypes != null) { var list = options.ExtraTypes.ToList(); if (list.Count(x => x == typeof(SerializableDictionary <string, string>)) < 0) { list.Add(typeof(SerializableDictionary <string, string>)); options.ExtraTypes = list.ToArray(); } serializer = new XmlSerializer(type, options.ExtraTypes); } else { options = new XMLSerializerOptions { ExtraTypes = new Type[] { typeof(SerializableDictionary <string, string>) } }; } } if (serializer == null) { serializer = new XmlSerializer(type); } // XML Verisini tutmak için bir StringReader yaratıyoruz. StringReader SR = new StringReader(deserializableValue); XmlReader XR = new XmlTextReader(SR); // XML verisinin Deserialize edilip edilmeyeceğini kontrol ediyoruz. if (serializer.CanDeserialize(XR)) { // Ve XML verisini Deserialize ediyoruz. return(serializer.Deserialize(XR)); } else { return(null); } }
/// <summary> /// Serializes the specified obj. /// </summary> /// <param name="obj">The obj.</param> /// <param name="checkForObjectName">if set to <see langword="true"/> [check for object name].</param> /// <returns></returns> public IDictionary<string, object> FromObject(object obj, ISerializerOptions options) { object value = SerializeValue(obj, 0 /* level */, options.MaxSerializableLevelsSupported ?? LevelsToSerialize); string modelName = null; // remove special case model name if found in output if (value is IDictionary<string, object> && ((IDictionary<string, object>)value).ContainsKey(ModelNameKey)) { modelName = Convert.ToString(((IDictionary<string, object>)value)[ModelNameKey]); ((IDictionary<string, object>)value).Remove(ModelNameKey); } if (options.CheckForObjectName || !(value is IDictionary<string, object>) || !String.IsNullOrWhiteSpace(modelName)) { string name = obj is ICollection ? "collection" : "object"; // make sure the object isn't an easily handled primity type with IEnumerable if (Type.GetTypeCode(obj.GetType()) != TypeCode.Object) name = "object"; // make sure this type of dictionary is treated as an object if (obj is IDictionary<string, object>) name = "object"; // check for special case name from dictionary object if (!String.IsNullOrWhiteSpace(modelName)) name = modelName; // get what the object likes to be called if (obj.GetType().IsDefined(typeof(SerializableObjectAttribute), true)) { object[] attrs = obj.GetType().GetCustomAttributes(typeof(SerializableObjectAttribute), true); if (attrs.Length > 0) { SerializableObjectAttribute attr = attrs[0] as SerializableObjectAttribute; name = attr.Name; } } IDictionary<string, object> response = new Dictionary<string, object>(1); response.Add(name, value); value = response; } return value as IDictionary<string, object>; }
public T Deserialize <T>(string str, ISerializerOptions options) { var jsonSerializerOptions = ConvertOptions(options); return(this.Deserialize <T>(str, jsonSerializerOptions)); }
protected override object DeSerializeObject(Type type, string deserializableValue, ISerializerOptions serializerOptions = null) { using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(deserializableValue))) { using (var reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas())) { DataContractSerializer serializer = null; if (serializerOptions != null) { var options = serializerOptions as DataContractSerializerOptions; if (options != null && options.KnownTypes != null) { serializer = new DataContractSerializer(type, options.RootName, options.RootNamespace, options.KnownTypes); } } if (serializer == null) { serializer = new DataContractSerializer(type); } // Deserialize the data and read it from the instance. return(serializer.ReadObject(reader)); } } }
protected override object DeSerializeObject(Type type, string deserializableValue, ISerializerOptions serializerOptions) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); try { var repo = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(deserializableValue)); var result = formatter.Deserialize(repo); return(result); } catch (Exception ex) { throw ex; } finally { try { ms.Close(); } catch (Exception) { } } }
protected override object DeSerializeObject(Type type, string deserializableValue, ISerializerOptions serializerOptions = null) { var jsonSerOp = serializerOptions as JsonSerializerOptions; if (serializerOptions == null || jsonSerOp == null) { return(JsonConvert.DeserializeObject(deserializableValue, type)); } else { return(JsonConvert.DeserializeObject(deserializableValue, type, jsonSerOp.JsonSerializerSettings)); } }