/// <summary> /// /// </summary> public static void SerializeRPC(string id, object error, object obj, Stream stream) { JSONWriter writer = new JSONWriter(new StreamWriter(stream)); // Start JSON output writer.WriteStartObject(); writer.WritePropertyName("result"); // Serialize result set if (obj is Moxiecode.Manager.Utils.ResultSet) { ResultSet rs = (ResultSet)obj; writer.WriteStartObject(); // Write header writer.WritePropertyName("header"); writer.WriteStartObject(); foreach (string key in rs.Header.Keys) { writer.WritePropertyName(key); writer.WriteValue(rs.Header[key]); } writer.WriteEndObject(); // Write columns writer.WritePropertyName("columns"); writer.WriteStartArray(); foreach (string col in rs.Columns) { writer.WriteValue(col); } writer.WriteEndArray(); // Write data writer.WritePropertyName("data"); writer.WriteStartArray(); foreach (ArrayList row in rs.Data) { writer.WriteStartArray(); foreach (object item in row) { WriteValue(writer, item); } writer.WriteEndArray(); } writer.WriteEndArray(); // Write config if (rs.Config != null) { writer.WritePropertyName("config"); writer.WriteStartObject(); foreach (string key in rs.Config.AllKeys) { writer.WritePropertyName(key); writer.WriteValue(rs.Config[key]); } writer.WriteEndObject(); } // End result writer.WriteEndObject(); } else { WriteValue(writer, obj); } // Write id writer.WritePropertyName("id"); writer.WriteValue(id); // Write error writer.WritePropertyName("error"); writer.WriteValue(null); // Close writer.WriteEndObject(); writer.Close(); }
private static void WriteValue(JSONWriter writer, object obj) { if (obj == null) { writer.WriteNull(); } if (obj is System.String) { writer.WriteValue((string)obj); } if (obj is System.Boolean) { writer.WriteValue((bool)obj); } if (obj is System.Double) { writer.WriteValue(Convert.ToDouble(obj)); } if (obj is System.Int32) { writer.WriteValue(Convert.ToInt32(obj)); } if (obj is System.Int64) { writer.WriteValue(Convert.ToInt64(obj)); } if (obj is ArrayList) { writer.WriteStartArray(); foreach (object val in ((ArrayList)obj)) { WriteValue(writer, val); } writer.WriteEndArray(); } if (obj is NameValueCollection) { writer.WriteStartObject(); string[] keys = GetReversedKeys(obj); foreach (string key in keys) { writer.WritePropertyName(key); WriteValue(writer, ((NameValueCollection)obj)[key]); } writer.WriteEndObject(); } if (obj is Hashtable) { writer.WriteStartObject(); string[] keys = GetReversedKeys(obj); foreach (string key in keys) { writer.WritePropertyName((string)key); WriteValue(writer, ((Hashtable)obj)[key]); } writer.WriteEndObject(); } }