Example #1
0
        public static string ToJsonString(this JToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            // The following code is different from token.ToString(), which special-cases null to return "" instead of
            // "null".
            StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            try
            {
                using (JsonWriter jsonWriter = JsonSerialization.CreateJsonTextWriter(stringWriter))
                {
                    token.WriteTo(jsonWriter);
                    jsonWriter.Flush();
                    stringWriter.Flush();

                    string result = stringWriter.ToString();
                    stringWriter = null;

                    return(result);
                }
            }
            finally
            {
                if (stringWriter != null)
                {
                    stringWriter.Dispose();
                }
            }
        }
Example #2
0
        public static string ToJsonString(this JToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            // The following code is different from token.ToString(), which special-cases null to return "" instead of
            // "null".
            using (StringWriter stringWriter = new StringWriter())
                using (JsonWriter jsonWriter = JsonSerialization.CreateJsonTextWriter(stringWriter))
                {
                    token.WriteTo(jsonWriter);
                    jsonWriter.Flush();
                    stringWriter.Flush();
                    return(stringWriter.ToString());
                }
        }