private static void EncodeEnclosed(Error error, JsonTextWriter writer) { Debug.Assert(error != null); Debug.Assert(writer != null); writer.Object(); EncodeMembers(error, writer); writer.Pop(); }
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(); } }
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); }
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(); }
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(); }
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(); } }
public override IEnumerable <string> Entries(IList <ErrorLogEntry> entries, int index, int count, int total) { Debug.Assert(entries != null); Debug.Assert(index >= 0); Debug.Assert(index + count <= entries.Count); var writer = new StringWriter { NewLine = "\n" }; if (_wrapped) { writer.WriteLine("<script type='text/javascript' language='javascript'>"); writer.WriteLine("//<[!CDATA["); } writer.Write(_callback); writer.Write('('); var json = new JsonTextWriter(writer); json.Object() .Member("total").Number(total) .Member("errors").Array(); var requestUrl = ErrorLogPageFactory.GetRequestUrl(Context); for (var i = index; i < count; i++) { var entry = entries[i]; writer.WriteLine(); if (i == 0) { writer.Write(' '); } writer.Write(" "); var urlTemplate = new Uri(requestUrl, "{0}?id=" + Uri.EscapeDataString(entry.Id)).ToString(); json.Object(); ErrorJson.EncodeMembers(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>"); } } yield return(writer.ToString()); }
public override void Entries(IList <ErrorLogEntry> 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 = 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); }