Skip to content

Commit 00ca84f

Browse files
committed
Update js-utils.md
1 parent 899d662 commit 00ca84f

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

MyApp/_pages/js-utils.md

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,19 @@ C# Pattern matching provides a powerful and intuitive approach for introspecting
3434
### Getting the client_id in a ComfyUI Output
3535

3636
```csharp
37-
var comfyOutput = JSON.parse(json);
38-
var promptId = comfyOutput.Keys.First();
39-
var prompt = (Dictionary<string, object?>)comfyOutput[promptId]!;
40-
if (prompt.TryGetValue("prompt", out var oPromptTuple) && oPromptTuple is List<object> promptTuple)
37+
var comfyOutput = JSON.ParseObject(json);
38+
var prompt = (Dictionary<string, object?>)result.Values.First()!;
39+
if (prompt.TryGetValue("prompt", out List<object> promptTuple) && promptTuple.Count > 3)
4140
{
42-
if (promptTuple.Count > 3)
41+
var extraData = promptTuple[3];
42+
if (extraData is Dictionary<string, object?> extraDataDict)
4343
{
44-
var extraData = promptTuple[3];
45-
if (extraData is Dictionary<string, object?> extraDataDict)
44+
if (extraDataDict.TryGetValue("client_id", out string clientId))
4645
{
47-
if (extraDataDict.TryGetValue("client_id", out var oClientId))
48-
{
49-
return oClientId as string;
50-
}
46+
ret.ClientId = clientId;
5147
}
5248
}
5349
}
54-
return null;
5550
```
5651

5752
Equivalent implementation using System.Text.Json:
@@ -62,50 +57,47 @@ using System.Text.Json;
6257
var jsonDocument = JsonDocument.Parse(json);
6358
var root = jsonDocument.RootElement;
6459

65-
var promptId = root.EnumerateObject().FirstOrDefault().Name;
66-
if (!string.IsNullOrEmpty(promptId) && root.TryGetProperty(promptId, out var promptElement))
60+
// Get the first property value (equivalent to result.Values.First())
61+
var firstProperty = root.EnumerateObject().FirstOrDefault();
62+
if (firstProperty.Value.ValueKind == JsonValueKind.Object)
6763
{
68-
if (promptElement.TryGetProperty("prompt", out var promptTupleElement)
69-
&& promptTupleElement.ValueKind == JsonValueKind.Array)
64+
var prompt = firstProperty.Value;
65+
66+
if (prompt.TryGetProperty("prompt", out var promptElement)
67+
&& promptElement.ValueKind == JsonValueKind.Array)
7068
{
71-
var promptArray = promptTupleElement.EnumerateArray().ToArray();
69+
var promptArray = promptElement.EnumerateArray().ToArray();
7270
if (promptArray.Length > 3)
7371
{
7472
var extraDataElement = promptArray[3];
7573
if (extraDataElement.ValueKind == JsonValueKind.Object
7674
&& extraDataElement.TryGetProperty("client_id", out var clientIdElement)
7775
&& clientIdElement.ValueKind == JsonValueKind.String)
7876
{
79-
return clientIdElement.GetString();
77+
var clientId = clientIdElement.GetString();
78+
Console.WriteLine(clientId);
8079
}
8180
}
8281
}
8382
}
84-
return null;
8583
```
8684

87-
### Parsing a Gemini API Request
85+
### Parsing and mutating a Gemini API Request
8886

8987
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:
9088

9189
```csharp
92-
var obj = (Dictionary<string,object>) JSON.parse(json);
93-
if (obj.TryGetValue("contents", out var oContents)
94-
&& oContents is List<object> contents)
90+
var obj = JSON.ParseObject(origJson);
91+
if (obj.TryGetValue("contents", out List<object> contents))
9592
{
96-
foreach (var oContent in contents)
93+
foreach (var content in contents.OfType<Dictionary<string,object>>())
9794
{
98-
if (oContent is Dictionary<string, object> content
99-
&& content.TryGetValue("parts", out var oParts)
100-
&& oParts is List<object> parts)
95+
if (content.TryGetValue("parts", out List<object> parts))
10196
{
102-
foreach (var oPart in parts)
97+
foreach (var part in parts.OfType<Dictionary<string,object>>())
10398
{
104-
if (oPart is Dictionary<string, object> part
105-
&& part.TryGetValue("inline_data", out var oInlineData)
106-
&& oInlineData is Dictionary<string, object> inlineData
107-
&& inlineData.TryGetValue("data", out var oData)
108-
&& oData is string data)
99+
if (part.TryGetValue("inline_data", out Dictionary<string,object> inlineData)
100+
&& inlineData.TryGetValue("data", out string data))
109101
{
110102
inlineData["data"] = $"({data.Length})";
111103
}
@@ -116,7 +108,8 @@ if (obj.TryGetValue("contents", out var oContents)
116108
Console.WriteLine(JSON.stringify(obj));
117109
```
118110

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`:
120113

121114
```csharp
122115
using System.Text.Json;
@@ -150,9 +143,10 @@ if (root.TryGetProperty("contents", out var contentsElement)
150143
}
151144
}
152145
}
146+
Console.WriteLine(jsonDocument.RootElement);
153147
```
154148

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:
156150

157151
```csharp
158152
using System.Text.Json;
@@ -187,7 +181,6 @@ if (jsonNode is JsonObject rootObj
187181
Console.WriteLine(jsonNode.ToJsonString());
188182
```
189183

190-
191184
### Register JS Utils in ServiceStack.Text
192185

193186
JS Utils is already pre-configured in ServiceStack Web Apps to handle serializing & deserializing `object` types.

0 commit comments

Comments
 (0)