public string SerializeToString(T value) { if (value == null) { return(null); } if (typeof(T) == typeof(string)) { return(value as string); } if (typeof(T) == typeof(object) || typeof(T).IsAbstract || typeof(T).IsInterface) { if (typeof(T).IsAbstract || typeof(T).IsInterface) { JsState.IsWritingDynamic = true; } var result = JsonSerializer.SerializeToString(value, value.GetType()); if (typeof(T).IsAbstract || typeof(T).IsInterface) { JsState.IsWritingDynamic = false; } return(result); } var writer = StringWriterManager.Allocate(); JsonWriter <T> .WriteObject(writer, value); return(StringWriterManager.ReturnAndFree(writer)); }
public static string SerializeToString <T>(T value) { var writer = StringWriterManager.Allocate(); GetWriteFn(value.GetType())(writer, value); return(StringWriterManager.ReturnAndFree(writer)); }
public static string SerializeToCsv <T>(IEnumerable <T> records) { var writer = StringWriterManager.Allocate(); writer.WriteCsv(records); return(StringWriterManager.ReturnAndFree(writer)); }
/// <summary>Converts the <see cref="DateTimeOffset"/> to its JSON string representation using the <see cref="DateFormatHandling"/> specified.</summary> /// <param name="value">The value to convert.</param> /// <param name="format">The format the date will be converted to.</param> /// <returns>A JSON string representation of the <see cref="DateTimeOffset"/>.</returns> public static string ToString(DateTimeOffset value, DateFormatHandling format) { //using (StringWriter writer = StringUtils.CreateStringWriter(64)) var writer = StringWriterManager.Allocate(); writer.Write('"'); DateTimeUtils.WriteDateTimeOffsetString(writer, value, format, null, CultureInfo.InvariantCulture); writer.Write('"'); return(StringWriterManager.ReturnAndFree(writer)); }
/// <summary>Converts the <see cref="DateTime"/> to its JSON string representation using the <see cref="DateFormatHandling"/> specified.</summary> /// <param name="value">The value to convert.</param> /// <param name="format">The format the date will be converted to.</param> /// <param name="timeZoneHandling">The time zone handling when the date is converted to a string.</param> /// <returns>A JSON string representation of the <see cref="DateTime"/>.</returns> public static string ToString(DateTime value, DateFormatHandling format, DateTimeZoneHandling timeZoneHandling) { var updatedDateTime = DateTimeUtils.EnsureDateTime(value, timeZoneHandling); //using (StringWriter writer = StringUtils.CreateStringWriter(64)) var writer = StringWriterManager.Allocate(); writer.Write('"'); DateTimeUtils.WriteDateTimeString(writer, updatedDateTime, format, null, CultureInfo.InvariantCulture); writer.Write('"'); return(StringWriterManager.ReturnAndFree(writer)); }
public string SerializeToString(T value) { if (value == null) { return(null); } if (value is string) { return(value as string); } var writer = StringWriterManager.Allocate(); JsvWriter <T> .WriteObject(writer, value); return(StringWriterManager.ReturnAndFree(writer)); }
public static string SerializeToString <T>(T value) { if (value == null) { return(null); } if (typeof(T) == typeof(string)) { return(value as string); } var writer = StringWriterManager.Allocate(); CsvSerializer <T> .WriteObject(writer, value); return(StringWriterManager.ReturnAndFree(writer)); }
/// <summary>Serializes the specified object to a JSON string using a type, formatting and <see cref="JsonSerializer"/>.</summary> /// <param name="jsonSerializerPool">The <see cref="JsonSerializer"/> pool used to serialize the object</param> /// <param name="value">The object to serialize.</param> /// <param name="type">The type of the value being serialized. /// This parameter is used when <see cref="JsonSerializer.TypeNameHandling"/> is <see cref="TypeNameHandling.Auto"/> to write out the type name if the type of the value does not match. /// Specifying the type is optional.</param> /// <returns>A JSON string representation of the object.</returns> public static string SerializeObject(this ObjectPool <JsonSerializer> jsonSerializerPool, object value, Type type = null) { var sw = StringWriterManager.Allocate(); var jsonSerializer = jsonSerializerPool.Take(); try { using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.ArrayPool = JsonConvertX.GlobalCharacterArrayPool; jsonWriter.CloseOutput = false; jsonWriter.Formatting = jsonSerializer.Formatting; jsonSerializer.Serialize(jsonWriter, value, type); jsonWriter.Flush(); } return(sw.ToString()); } finally { StringWriterManager.Free(sw); jsonSerializerPool.Return(jsonSerializer); } }