コード例 #1
0
        public static string WriteJsonToString(Action <JsonWriter> serializer, bool pretty = false)
        {
            StringBuilder resultBuilder = new StringBuilder();

            // [WMR 20160421] Explicit disposal
            using (StringWriter sw = new StringWriter(resultBuilder))
                using (JsonWriter jw = SerializationUtil.CreateJsonTextWriter(sw))
                {
                    jw.Formatting = pretty ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None;
                    serializer(jw);
                    jw.Flush();
                    return(resultBuilder.ToString());
                }
        }
コード例 #2
0
 public static byte[] WriteJsonToBytes(Action <JsonWriter> serializer)
 {
     // [WMR 20160421] Explicit disposal
     using (MemoryStream stream = new MemoryStream())
     {
         using (var sw = new StreamWriter(stream, new UTF8Encoding(false)))
             using (JsonWriter jw = SerializationUtil.CreateJsonTextWriter(sw))
             {
                 serializer(jw);
                 jw.Flush();
                 sw.Flush();
                 return(stream.ToArray());
             }
     }
 }
コード例 #3
0
        public static async Task <string> WriteJsonToStringAsync(Func <JsonWriter, Task> serializer, bool pretty = false, bool appendNewLine = false)
        {
            StringBuilder resultBuilder = new StringBuilder();

            // [WMR 20160421] Explicit disposal
            using (StringWriter sw = new StringWriter(resultBuilder))
                using (JsonWriter jw = SerializationUtil.CreateJsonTextWriter(sw))
                {
                    jw.Formatting = pretty ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None;
                    await serializer(jw).ConfigureAwait(false);

                    await jw.FlushAsync().ConfigureAwait(false);

                    return(appendNewLine ? resultBuilder.AppendLine().ToString() : resultBuilder.ToString());
                }
        }
コード例 #4
0
        public static async Task <byte[]> WriteJsonToBytesAsync(Func <JsonWriter, Task> serializer)
        {
            // [WMR 20160421] Explicit disposal
            using (MemoryStream stream = new MemoryStream())
            {
                using (var sw = new StreamWriter(stream, new UTF8Encoding(false)))
                    using (JsonWriter jw = SerializationUtil.CreateJsonTextWriter(sw))
                    {
                        await serializer(jw);

                        await jw.FlushAsync().ConfigureAwait(false);

                        await sw.FlushAsync().ConfigureAwait(false);

                        return(stream.ToArray());
                    }
            }
        }