Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing JSON Text according to the grammar rules laid out in RFC 4627.
This class supports ELMAH and is not intended to be used directly from your code. It may be modified or removed in the future without notice. It has public accessibility for testing purposes. If you need a general-purpose JSON Text encoder, consult JSON.org for implementations or use classes available from the Microsoft .NET Framework.
Пример #1
0
        private static void EncodeEnclosed(Error error, JsonTextWriter writer)
        {
            Debug.Assert(error != null);
            Debug.Assert(writer != null);

            writer.Object();
            EncodeMembers(error, writer);
            writer.Pop();
        }
Пример #2
0
        internal static void Encode(Error error, JsonTextWriter writer)
        {
            Debug.Assert(error != null);
            Debug.Assert(writer != null);

            Member(writer, "application", error.ApplicationName);
            Member(writer, "host", error.HostName);
            Member(writer, "type", error.Type);
            Member(writer, "message", error.Message);
            Member(writer, "source", error.Source);
            Member(writer, "detail", error.Detail);
            Member(writer, "user", error.User);
            Member(writer, "time", error.Time, DateTime.MinValue);
            Member(writer, "statusCode", error.StatusCode, 0);
            Member(writer, "webHostHtmlMessage", error.WebHostHtmlMessage);
            Member(writer, "serverVariables", error.ServerVariables);
            Member(writer, "queryString", error.QueryString);
            Member(writer, "form", error.Form);
            Member(writer, "cookies", error.Cookies);
        }
Пример #3
0
            public override void Entries(IList entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                StringWriter writer = new StringWriter();
                writer.NewLine = "\n";

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                JsonTextWriter json = new JsonTextWriter(writer);
                json.Object()
                    .Member("total").Number(total)
                    .Member("errors").Array();

                Uri requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                for (int i = index; i < count; i++)
                {
                    ErrorLogEntry entry = (ErrorLogEntry) entries[i];
                    writer.WriteLine();
                    if (i == 0) writer.Write(' ');
                    writer.Write("  ");

                    string urlTemplate = new Uri(requestUrl, "{0}?id=" + HttpUtility.UrlEncode(entry.Id)).ToString();

                    json.Object();
                        ErrorJson.Encode(entry.Error, json);
                        json.Member("hrefs")
                        .Array()
                            .Object()
                                .Member("type").String("text/html")
                                .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                            .Object()
                                .Member("type").String("aplication/json")
                                .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                            .Object()
                                .Member("type").String("application/xml")
                                .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                        .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                    writer.WriteLine();

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                        writer.WriteLine(@"</body></html>");
                }

                Context.Response.Output.Write(writer);
            }
Пример #4
0
        private static void Member(JsonTextWriter writer, string name, NameValueCollection collection)
        {
            Debug.Assert(writer != null);
            Debug.AssertStringNotEmpty(name);

            //
            // Bail out early if the collection is null or empty.
            //

            if (collection == null || collection.Count == 0)
                return;

            //
            // Save the depth, which we'll use to lazily emit the collection.
            // That is, if we find that there is nothing useful in it, then
            // we could simply avoid emitting anything.
            //

            int depth = writer.Depth;

            //
            // For each key, we get all associated values and loop through
            // twice. The first time round, we count strings that are
            // neither null nor empty. If none are found then the key is
            // skipped. Otherwise, second time round, we encode
            // strings that are neither null nor empty. If only such string
            // exists for a key then it is written directly, otherwise
            // multiple strings are naturally wrapped in an array.
            //

            foreach (string key in collection.Keys)
            {
                string[] values = collection.GetValues(key);

                if (values == null || values.Length == 0)
                    continue;

                int count = 0; // Strings neither null nor empty.

                for (int i = 0; i < values.Length; i++)
                {
                    string value = values[i];
                    if (value != null && value.Length > 0)
                        count++;
                }

                if (count == 0) // None?
                    continue;   // Skip key

                //
                // There is at least one value so now we emit the key.
                // Before doing that, we check if the collection member
                // was ever started. If not, this would be a good time.
                //

                if (depth == writer.Depth)
                {
                    writer.Member(name);
                    writer.Object();
                }

                writer.Member(key);

                if (count > 1)
                    writer.Array(); // Wrap multiples in an array

                for (int i = 0; i < values.Length; i++)
                {
                    string value = values[i];
                    if (value != null && value.Length > 0)
                        writer.String(value);
                }

                if (count > 1)
                    writer.Pop();   // Close multiples array
            }

            //
            // If we are deeper than when we began then the collection was
            // started so we terminate it here.
            //

            if (writer.Depth > depth)
                writer.Pop();
        }
Пример #5
0
        private static void Member(JsonTextWriter writer, string name, string value)
        {
            Debug.Assert(writer != null);
            Debug.AssertStringNotEmpty(name);

            if (value == null || value.Length == 0)
                return;

            writer.Member(name).String(value);
        }
Пример #6
0
        private static void Member(JsonTextWriter writer, string name, DateTime value, DateTime defaultValue)
        {
            if (value == defaultValue)
                return;

            writer.Member(name).String(value);
        }
Пример #7
0
        private static void Member(JsonTextWriter writer, string name, int value, int defaultValue)
        {
            if (value == defaultValue)
                return;

            writer.Member(name).Number(value);
        }
Пример #8
0
        private static void Member(JsonTextWriter writer, string name, NameValueCollection collection)
        {
            Debug.Assert(writer != null);
            Debug.AssertStringNotEmpty(name);

            //
            // Bail out early if the collection is null or empty.
            //

            if (collection == null || collection.Count == 0) 
                return;

            //
            // Save the depth, which we'll use to lazily emit the collection.
            // That is, if we find that there is nothing useful in it, then
            // we could simply avoid emitting anything.
            //

            var depth = writer.Depth;

            //
            // For each key, we get all associated values and loop through
            // twice. The first time round, we count strings that are 
            // neither null nor empty. If none are found then the key is 
            // skipped. Otherwise, second time round, we encode
            // strings that are neither null nor empty. If only such string
            // exists for a key then it is written directly, otherwise
            // multiple strings are naturally wrapped in an array.
            //

            var items = from i in Enumerable.Range(0, collection.Count)
                        let values = collection.GetValues(i)
                        where values != null && values.Length > 0
                        let some = // Neither null nor empty
                            from v in values
                            where !string.IsNullOrEmpty(v)
                            select v
                        let nom = some.Take(2).Count()
                        where nom > 0
                        select new
                        {
                            Key = collection.GetKey(i), 
                            IsArray = nom > 1, 
                            Values = some,
                        };
            
            foreach (var item in items)
            {
                //
                // There is at least one value so now we emit the key.
                // Before doing that, we check if the collection member
                // was ever started. If not, this would be a good time.
                //

                if (depth == writer.Depth)
                {
                    writer.Member(name);
                    writer.Object();
                }

                writer.Member(item.Key ?? string.Empty);

                if (item.IsArray)
                    writer.Array(); // Wrap multiples in an array

                foreach (var value in item.Values)
                    writer.String(value);

                if (item.IsArray) 
                    writer.Pop();   // Close multiples array
            }

            //
            // If we are deeper than when we began then the collection was
            // started so we terminate it here.
            //

            if (writer.Depth > depth)
                writer.Pop();
        }
Пример #9
0
        private static void Member(JsonTextWriter writer, string name, string value)
        {
            Debug.Assert(writer != null);
            Debug.AssertStringNotEmpty(name);

            if (string.IsNullOrEmpty(value))
                return;

            writer.Member(name).String(value);
        }