public void SetUp() { root = new Root(); root["/"] = new Node("node_1"); root["/node_1"] = new Node("node_2"); serializer = new JsonSerializer(typeof(Node)); }
void GetRespond(HttpListenerRequest request, HttpListenerResponse response) { string[] rawUrl = request.RawUrl.Split('.'); string url = rawUrl[0]; string format = "json"; if (rawUrl.Length == 2) { format = rawUrl[1]; } Node node = srv[url]; Serializer serializer = null; switch (format) { case "json": serializer = new JsonSerializer(node.GetType()); response.ContentType = "application/json"; break; case "xml": serializer = new XmlSerializer(node.GetType()); response.ContentType = "application/xml"; break; default: response.StatusCode = 400; break; } if (response.StatusCode == 200) { string data = serializer.Serialize(node); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data); response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); response.OutputStream.Flush(); response.OutputStream.Close(); } }