You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As JSON Object just stored any JSON into untyped generic collections you can use C# pattern matching to navigate and mutate the JSON Object as done in this example which modifies an Gemini API Request to replace the `inline_data.data` string with its length to make it suitable for logging:
if (part.TryGetValue("inline_data", outDictionary<string,object> inlineData)
100
+
&&inlineData.TryGetValue("data", outstringdata))
109
101
{
110
102
inlineData["data"] =$"({data.Length})";
111
103
}
@@ -116,7 +108,8 @@ if (obj.TryGetValue("contents", out var oContents)
116
108
Console.WriteLine(JSON.stringify(obj));
117
109
```
118
110
119
-
This would be an equivalent implementation using System.Text.Json:
111
+
This would be an equivalent implementation using **System.Text.Json** to parse and navigate the JSON data structure
112
+
with `JsonDocument`:
120
113
121
114
```csharp
122
115
usingSystem.Text.Json;
@@ -150,9 +143,10 @@ if (root.TryGetProperty("contents", out var contentsElement)
150
143
}
151
144
}
152
145
}
146
+
Console.WriteLine(jsonDocument.RootElement);
153
147
```
154
148
155
-
But as it's a read-only data structure you'd need to reconstruct the JSON to modify it. To create an equivalent modified JSON for logging, you can use `JsonNode` for mutable operations:
149
+
But as it's a read-only data structure you'd need to reconstruct the JSON to modify it. To create an equivalent modified JSON for logging, you would use `JsonNode` for mutable operations instead:
156
150
157
151
```csharp
158
152
usingSystem.Text.Json;
@@ -187,7 +181,6 @@ if (jsonNode is JsonObject rootObj
187
181
Console.WriteLine(jsonNode.ToJsonString());
188
182
```
189
183
190
-
191
184
### Register JS Utils in ServiceStack.Text
192
185
193
186
JS Utils is already pre-configured in ServiceStack Web Apps to handle serializing & deserializing `object` types.
0 commit comments