Handle empty lists and dictionaries in JSON output

Updated `EnterList<T>` to write "[]" for empty lists and return false.
Modified `EnterDictionary<TKey, TValue>` to write "{}" for empty string-keyed dictionaries and "[]" for empty non-string-keyed dictionaries, also returning false.
These changes ensure a more succinct representation of empty collections in the output.
This commit is contained in:
ds5678 2025-03-12 19:34:39 -07:00
parent a95f69f42e
commit a9685e3dc6

View File

@ -74,6 +74,12 @@ public class DefaultJsonWalker : AssetWalker
public override bool EnterList<T>(IReadOnlyList<T> list)
{
if (list.Count == 0)
{
Writer.Write("[]");
return false;
}
Writer.WriteLine('[');
Writer.Indent++;
return true;
@ -95,11 +101,23 @@ public class DefaultJsonWalker : AssetWalker
{
if (IsString<TKey>())
{
if (dictionary.Count == 0)
{
Writer.Write("{}");
return false;
}
Writer.WriteLine('{');
Writer.Indent++;
}
else
{
if (dictionary.Count == 0)
{
Writer.Write("[]");
return false;
}
Writer.WriteLine('[');
Writer.Indent++;
}