/// <summary> /// Serialize the DTO as JSON /// </summary> /// <param name="reply"></param> /// <returns></returns> public string Serialize(Reply reply) { string resultsJson = string.Join(",", reply.Prtg.Result.Select(p => ConvertToJson(p)).ToArray()); string completeResult = "{\"prtg\":{\"result\":[" + resultsJson + "]"; if (!string.IsNullOrWhiteSpace(reply.Prtg.Text)) { completeResult = completeResult + $", \"text\":\"{reply.Prtg.Text}\""; } completeResult = completeResult + "}}"; return completeResult; }
/// <summary> /// Queries the sensor to get the results, and then uses the serializer to convert it to a string. /// </summary> /// <param name="sensor"></param> /// <returns></returns> private Response QuerySensor(ISensor sensor) { try { var reply = new Reply(sensor.Description, sensor.Results().ToList()); return Response.AsText(_serializer.Serialize(reply), _serializer.ContentType); } catch (Exception err) { Console.WriteLine("ERROR WITH REQUEST!"); Console.WriteLine(err.ToString()); return 500; } }
/// <summary> /// Serialize the reply as xml /// </summary> /// <param name="reply"></param> /// <returns></returns> public string Serialize(Reply reply) { //return Response.AsJson(new Reply(sensor.Description, resultsList)); string resultsJson = string.Join(Environment.NewLine, reply.Prtg.Result.Select(this.ConvertToXml).ToArray()); StringBuilder result = new StringBuilder(); result.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); result.AppendLine("<prtg>"); result.AppendLine(resultsJson); if (!string.IsNullOrWhiteSpace(reply.Prtg.Text)) { result.AppendLine($"<text>{reply.Prtg.Text}</text>"); } result.AppendLine("</prtg>"); return result.ToString(); }