public static void SerializeToStream(object value, Type type, Stream stream) { var writer = new StreamWriter(stream, UTF8Encoding); JsvWriter.GetWriteFn(type)(writer, value); writer.Flush(); }
public static string SerializeToString(object value, Type type) { if (value == null) { return(null); } if (type == typeof(string)) { return(value as string); } var sb = new StringBuilder(); using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture)) { try { SerializationVisitorTracker.TrackSerialization(writer); JsvWriter.GetWriteFn(type)(writer, value); } finally { SerializationVisitorTracker.UnTrackSerialization(writer); } } return(sb.ToString()); }
public static bool FormUrlEncoded(TextWriter writer, string propertyName, object obj) { if (obj is IDictionary map) { var i = 0; foreach (var key in map.Keys) { if (i++ > 0) { writer.Write('&'); } var value = map[key]; writer.Write(propertyName); writer.Write('['); writer.Write(key.ToString()); writer.Write("]="); if (value == null) { writer.Write(JsonUtils.Null); } else if (value is string strValue && strValue == string.Empty) /*ignore*/ } { else { var writeFn = JsvWriter.GetWriteFn(value.GetType()); writeFn(writer, value); } }
public static void SerializeToStream(object value, Type type, Stream stream) { var writer = new StreamWriter(stream, PclExport.Instance.GetUseEncoding(false)); JsvWriter.GetWriteFn(type)(writer, value); writer.Flush(); }
public static void SerializeToWriter(object value, Type type, TextWriter writer) { if (value == null) { return; } if (type == typeof(string)) { writer.Write(value); return; } JsvWriter.GetWriteFn(type)(writer, value); }
public static void SerializeToStream(object value, Type type, Stream stream) { var writer = new StreamWriter(stream, UTF8EncodingWithoutBom); try { SerializationVisitorTracker.TrackSerialization(writer); JsvWriter.GetWriteFn(type)(writer, value); } finally { SerializationVisitorTracker.UnTrackSerialization(writer); } writer.Flush(); }
public static string SerializeToString(object value, Type type) { if (value == null) { return(null); } if (type == typeof(string)) { return(value as string); } var writer = StringWriterThreadStatic.Allocate(); JsvWriter.GetWriteFn(type)(writer, value); return(StringWriterThreadStatic.ReturnAndFree(writer)); }
public static bool FormUrlEncoded(TextWriter writer, string propertyName, object obj) { var map = obj as IDictionary; if (map != null) { var i = 0; foreach (var key in map.Keys) { if (i++ > 0) { writer.Write('&'); } var value = map[key]; writer.Write(propertyName); writer.Write('['); writer.Write(key.ToString()); writer.Write("]="); if (value == null) { writer.Write(JsonUtils.Null); } else { var writeFn = JsvWriter.GetWriteFn(value.GetType()); writeFn(writer, value); } } return(true); } var typeConfig = typeConfigCache.GetOrAdd(obj.GetType(), t => { var genericType = typeof(PropertyTypeConfig <>).MakeGenericType(t); var fi = genericType.Fields().First(x => x.Name == "Config");; var config = (PropertyTypeConfig)fi.GetValue(null); return(config); }); typeConfig.WriteFn(propertyName, writer, obj); return(true); }
public static string SerializeToString(object value, Type type) { if (value == null) { return(null); } if (type == typeof(string)) { return(value as string); } using (var sb = StringBuilderWriter.Create()) { JsvWriter.GetWriteFn(type)(sb.Writer, value); return(sb.ToString()); } }
public static string SerializeToString(object value, Type type) { if (value == null) { return(null); } if (type == typeof(string)) { return(value as string); } var sb = new StringBuilder(); using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture)) { JsvWriter.GetWriteFn(type)(writer, value); } return(sb.ToString()); }
public static void SerializeToWriter(object value, Type type, TextWriter writer) { if (value == null) { return; } if (type == typeof(string)) { writer.Write(value); return; } try { SerializationVisitorTracker.TrackSerialization(writer); JsvWriter.GetWriteFn(type)(writer, value); } finally { SerializationVisitorTracker.UnTrackSerialization(writer); } }
public static void WriteComplexQueryStringProperties(string typeName, TextWriter writer, object instance) { var i = 0; if (PropertyWriters != null) { var typedInstance = (T)instance; var len = PropertyWriters.Length; for (var index = 0; index < len; index++) { var propertyWriter = PropertyWriters[index]; if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize(typedInstance)) { continue; } var propertyValue = instance != null?propertyWriter.GetterFn(typedInstance) : null; if (propertyWriter.propertySuppressDefaultAttribute && Equals(propertyWriter.DefaultValue, propertyValue)) { continue; } if ((propertyValue == null || propertyWriter.propertySuppressDefaultConfig && Equals(propertyWriter.DefaultValue, propertyValue)) && !Serializer.IncludeNullValues) { continue; } if (JsConfig.ExcludePropertyReferences != null && JsConfig.ExcludePropertyReferences.Contains(propertyWriter.propertyReferenceName)) { continue; } if (i++ > 0) { writer.Write('&'); } var propertyValueType = propertyValue?.GetType(); if (propertyValueType != null && propertyValueType.IsUserType() && !propertyValueType.HasInterface(typeof(IEnumerable))) { // Nested Complex Type: legal_entity[dob][day]=1 var prefix = "{0}[{1}]".Fmt(typeName, propertyWriter.PropertyName); var props = propertyValueType.GetSerializableProperties(); for (var j = 0; j < props.Length; j++) { var pi = props[j]; var pValue = pi.GetValue(propertyValue, TypeConstants.EmptyObjectArray); if (pValue == null && !Serializer.IncludeNullValues) { continue; } if (j > 0) { writer.Write('&'); } writer.Write(prefix); writer.Write('['); writer.Write(GetPropertyName(pi.Name)); writer.Write("]="); if (pValue == null) { writer.Write(JsonUtils.Null); } else { JsvWriter.GetWriteFn(pValue.GetType())(writer, pValue); } } } else { writer.Write(typeName); writer.Write('['); writer.Write(propertyWriter.PropertyName); writer.Write("]="); if (propertyValue == null) { writer.Write(JsonUtils.Null); } else { propertyWriter.WriteFn(writer, propertyValue); } } } } }
/// <summary> /// Gets the write function. /// </summary> /// <param name="type">The type.</param> /// <returns>WriteObjectDelegate.</returns> public WriteObjectDelegate GetWriteFn(Type type) => JsvWriter.GetWriteFn(type);