private static void Member(JsonTextWriter writer, string name, DateTime value, DateTime defaultValue) { if (value == defaultValue) return; writer.Member(name).String(value); }
private static void Member(JsonTextWriter writer, string name, int value, int defaultValue) { if (value == defaultValue) return; writer.Member(name).Number(value); }
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); }
private static void Member(JsonTextWriter writer, string name, NameValueCollection collection) { Debug.Assert(writer != null); // // 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 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 = $"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}"; for (var i = index; i < count; i++) { var entry = entries[i]; writer.WriteLine(); if (i == 0) { writer.Write(' '); } writer.Write(" "); var urlTemplate = $"{requestUrl}?id=" + Uri.EscapeDataString(entry.Id); 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()); }