Пример #1
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);
        }
Пример #2
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();
        }
Пример #3
0
        private static void Member(JsonTextWriter writer, string name, DateTime value, DateTime defaultValue)
        {
            if (value == defaultValue)
                return;

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

            writer.Member(name).Number(value);
        }
Пример #5
0
        private static void EncodeEnclosed(Error error, JsonTextWriter writer)
        {
            Debug.Assert(error != null);
            Debug.Assert(writer != null);

            writer.Object();
            Encode(error, writer);
            writer.Pop();
        }
Пример #6
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);
        }
            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 = Context.Request.Url;

                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);
            }