From 49cc0707b18fc492d4eb54d400df755d40043f0a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 23 Jul 2026 15:11:10 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20JSON=20string=20concaten?= =?UTF-8?q?ation=20with=20StringBuilder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: manupawickramasinghe <73810867+manupawickramasinghe@users.noreply.github.com> --- Utilities/HttpServer.cs | 100 ++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/Utilities/HttpServer.cs b/Utilities/HttpServer.cs index dfac1026e..4d1f7a7d1 100644 --- a/Utilities/HttpServer.cs +++ b/Utilities/HttpServer.cs @@ -205,19 +205,20 @@ private void ServeResourceImage(HttpListenerResponse response, string name) { } private void SendJSON(HttpListenerResponse response) { + StringBuilder sb = new StringBuilder(); + sb.Append("{\"id\": 0, \"Text\": \"Sensor\", \"Children\": ["); + nodeCount = 1; + GenerateJSON(root, sb); + sb.Append("]"); + sb.Append(", \"Min\": \"Min\""); + sb.Append(", \"Value\": \"Value\""); + sb.Append(", \"Max\": \"Max\""); + sb.Append(", \"ImageURL\": \"\""); + sb.Append("}"); + + var responseContent = sb.ToString(); + byte[] buffer = Encoding.UTF8.GetBytes(responseContent); - string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": ["; - nodeCount = 1; - JSON += GenerateJSON(root); - JSON += "]"; - JSON += ", \"Min\": \"Min\""; - JSON += ", \"Value\": \"Value\""; - JSON += ", \"Max\": \"Max\""; - JSON += ", \"ImageURL\": \"\""; - JSON += "}"; - - var responseContent = JSON; - byte[] buffer = Encoding.UTF8.GetBytes(responseContent); response.AddHeader("Cache-Control", "no-cache"); @@ -234,44 +235,43 @@ private void SendJSON(HttpListenerResponse response) { response.Close(); } - private string GenerateJSON(Node n) { - string JSON = "{\"id\": " + nodeCount + ", \"Text\": \"" + n.Text - + "\", \"Children\": ["; - nodeCount++; - - foreach (Node child in n.Nodes) - JSON += GenerateJSON(child) + ", "; - if (JSON.EndsWith(", ")) - JSON = JSON.Remove(JSON.LastIndexOf(",")); - JSON += "]"; - - if (n is SensorNode) { - JSON += ", \"Min\": \"" + ((SensorNode)n).Min + "\""; - JSON += ", \"Value\": \"" + ((SensorNode)n).Value + "\""; - JSON += ", \"Max\": \"" + ((SensorNode)n).Max + "\""; - JSON += ", \"ImageURL\": \"images/transparent.png\""; - } else if (n is HardwareNode) { - JSON += ", \"Min\": \"\""; - JSON += ", \"Value\": \"\""; - JSON += ", \"Max\": \"\""; - JSON += ", \"ImageURL\": \"images_icon/" + - GetHardwareImageFile((HardwareNode)n) + "\""; - } else if (n is TypeNode) { - JSON += ", \"Min\": \"\""; - JSON += ", \"Value\": \"\""; - JSON += ", \"Max\": \"\""; - JSON += ", \"ImageURL\": \"images_icon/" + - GetTypeImageFile((TypeNode)n) + "\""; - } else { - JSON += ", \"Min\": \"\""; - JSON += ", \"Value\": \"\""; - JSON += ", \"Max\": \"\""; - JSON += ", \"ImageURL\": \"images_icon/computer.png\""; - } - - JSON += "}"; - return JSON; - } + private void GenerateJSON(Node n, StringBuilder sb) { + sb.Append("{\"id\": ").Append(nodeCount).Append(", \"Text\": \"").Append(n.Text).Append("\", \"Children\": ["); + nodeCount++; + + bool first = true; + foreach (Node child in n.Nodes) { + if (!first) + sb.Append(", "); + GenerateJSON(child, sb); + first = false; + } + sb.Append("]"); + + if (n is SensorNode) { + sb.Append(", \"Min\": \"").Append(((SensorNode)n).Min).Append("\""); + sb.Append(", \"Value\": \"").Append(((SensorNode)n).Value).Append("\""); + sb.Append(", \"Max\": \"").Append(((SensorNode)n).Max).Append("\""); + sb.Append(", \"ImageURL\": \"images/transparent.png\""); + } else if (n is HardwareNode) { + sb.Append(", \"Min\": \"\""); + sb.Append(", \"Value\": \"\""); + sb.Append(", \"Max\": \"\""); + sb.Append(", \"ImageURL\": \"images_icon/").Append(GetHardwareImageFile((HardwareNode)n)).Append("\""); + } else if (n is TypeNode) { + sb.Append(", \"Min\": \"\""); + sb.Append(", \"Value\": \"\""); + sb.Append(", \"Max\": \"\""); + sb.Append(", \"ImageURL\": \"images_icon/").Append(GetTypeImageFile((TypeNode)n)).Append("\""); + } else { + sb.Append(", \"Min\": \"\""); + sb.Append(", \"Value\": \"\""); + sb.Append(", \"Max\": \"\""); + sb.Append(", \"ImageURL\": \"images_icon/computer.png\""); + } + + sb.Append("}"); + } private static void ReturnFile(HttpListenerContext context, string filePath) {