private void eventForwarderFromRenderer(string eventType, NKEvent nke) { var handshake = new NKRemotingMessage(); handshake.command = NKRemotingMessage.Command.NKEvent; var eventSerialized = NKData.jsonSerialize(nke); handshake.args = new string[] { eventType, eventSerialized }; writeObject(asyncPipe, handshake); }
private static string serialize(object obj) { IFormatProvider invariant = System.Globalization.CultureInfo.InvariantCulture; Type t = obj.GetType(); TypeInfo ti = t.GetTypeInfo(); if (obj == null) { return("undefined"); } if (obj != null && obj.GetType().GetTypeInfo().IsPrimitive) { // The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. if (obj is Char) { return("'" + Convert.ToString(((Char)obj), invariant) + "'"); } if (obj is Boolean) { return((Boolean)obj ? "true" : "false"); } return(Convert.ToString(obj, System.Globalization.CultureInfo.InvariantCulture)); } if (obj is string) { var str = NKData.jsonSerialize((string)obj); return(str); // str.Substring(1, str.Length - 2); } if (obj is DateTime) { return("\"" + ((DateTime)obj).ToString("u") + "\""); } if (typeof(IDictionary).GetTypeInfo().IsAssignableFrom(ti)) { var genericKey = ti.GenericTypeArguments[0]; if (typeof(string).GetTypeInfo().IsAssignableFrom(genericKey.GetTypeInfo())) { var dict = (IDictionary <string, object>)obj; return("{" + string.Join(", ", dict.Keys.Select(k => "\"" + k + "\":" + serialize(dict[k]))) + "}"); } } if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(ti)) { return("[" + string.Join(", ", ((IEnumerable <dynamic>)obj).Select(o => serialize(o))) + "]"); } return(Convert.ToString(obj, System.Globalization.CultureInfo.InvariantCulture)); }