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