Skip to content

Commit 7635fa4

Browse files
feat(audience): add Json.Serialize(dict, indent) pretty-print overload
Second overload indenting nested containers by `indent` spaces per level. Round-trips through Deserialize identically; indent ≤ 0 routes to the existing compact Serialize, so every wire-payload caller is unaffected. Internal scaffolding: - WriteObject / WriteArray / WriteValue thread indent + depth so the overload can emit newlines and leading spaces at the correct nesting level. - AppendNewline helper centralises the "\n + (indent * depth) spaces" pattern. - pretty-path writes ":" with a trailing space; empty containers stay on a single line ({} / []), matching the compact form for collapsed nodes. No behaviour change on the compact overload for any existing caller; the indent threading is inert when indent == 0 (pretty=false). Motivated by the sample-app landing next — its Init tab renders the effective AudienceConfig as an echo view, and the Events/Identity tabs show constructed property dicts, all via Json.Serialize(…, 2). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6ba9bb9 commit 7635fa4

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

  • src/Packages/Audience/Runtime/Utility

src/Packages/Audience/Runtime/Utility/Json.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@ internal static class Json
1010
internal static string Serialize(Dictionary<string, object> data)
1111
{
1212
var sb = new StringBuilder();
13-
WriteObject(sb, data);
13+
WriteObject(sb, data, indent: 0, depth: 0);
1414
return sb.ToString();
1515
}
1616

17-
private static void WriteValue(StringBuilder sb, object value)
17+
// Pretty-prints `data` with `indent` spaces per nesting level.
18+
// Round-trips through Deserialize identically. Indent ≤ 0 returns
19+
// the compact form. Use for human-readable output; wire payloads
20+
// use the compact overload.
21+
internal static string Serialize(Dictionary<string, object> data, int indent)
22+
{
23+
if (indent <= 0) return Serialize(data);
24+
var sb = new StringBuilder();
25+
WriteObject(sb, data, indent, depth: 0);
26+
return sb.ToString();
27+
}
28+
29+
private static void WriteValue(StringBuilder sb, object value, int indent, int depth)
1830
{
1931
if (value == null)
2032
{
@@ -56,46 +68,60 @@ private static void WriteValue(StringBuilder sb, object value)
5668
}
5769
else if (value is Dictionary<string, object> dict)
5870
{
59-
WriteObject(sb, dict);
71+
WriteObject(sb, dict, indent, depth);
6072
}
6173
else if (value is IList list)
6274
{
63-
WriteArray(sb, list);
75+
WriteArray(sb, list, indent, depth);
6476
}
6577
else
6678
{
6779
WriteString(sb, value.ToString());
6880
}
6981
}
7082

71-
private static void WriteObject(StringBuilder sb, Dictionary<string, object> dict)
83+
private static void WriteObject(StringBuilder sb, Dictionary<string, object> dict, int indent, int depth)
7284
{
7385
sb.Append('{');
86+
if (dict.Count == 0) { sb.Append('}'); return; }
87+
88+
var pretty = indent > 0;
7489
var first = true;
7590
foreach (var kvp in dict)
7691
{
77-
if (!first)
78-
sb.Append(',');
92+
if (!first) sb.Append(',');
7993
first = false;
94+
if (pretty) AppendNewline(sb, indent, depth + 1);
8095
WriteString(sb, kvp.Key);
81-
sb.Append(':');
82-
WriteValue(sb, kvp.Value);
96+
sb.Append(pretty ? ": " : ":");
97+
WriteValue(sb, kvp.Value, indent, depth + 1);
8398
}
99+
if (pretty) AppendNewline(sb, indent, depth);
84100
sb.Append('}');
85101
}
86102

87-
private static void WriteArray(StringBuilder sb, IList list)
103+
private static void WriteArray(StringBuilder sb, IList list, int indent, int depth)
88104
{
89105
sb.Append('[');
106+
if (list.Count == 0) { sb.Append(']'); return; }
107+
108+
var pretty = indent > 0;
90109
for (var i = 0; i < list.Count; i++)
91110
{
92-
if (i > 0)
93-
sb.Append(',');
94-
WriteValue(sb, list[i]);
111+
if (i > 0) sb.Append(',');
112+
if (pretty) AppendNewline(sb, indent, depth + 1);
113+
WriteValue(sb, list[i], indent, depth + 1);
95114
}
115+
if (pretty) AppendNewline(sb, indent, depth);
96116
sb.Append(']');
97117
}
98118

119+
private static void AppendNewline(StringBuilder sb, int indent, int depth)
120+
{
121+
sb.Append('\n');
122+
sb.Append(' ', indent * depth);
123+
}
124+
99125
private static void WriteString(StringBuilder sb, string s)
100126
{
101127
sb.Append('"');

0 commit comments

Comments
 (0)