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) {