static void connect() { try { ahnparams ahn = new ahnparams(); ahn.src = @"SIP/[email protected]"; ahn.dest = @"7801234567"; //Pass it to our static reflector, which will build JSONReflector jsonReflector = new JSONReflector(ahn); //JSONSharp converts the class to JSON format //Console.WriteLine(jsonReflector.ToString()); NetworkCredential myCred = new NetworkCredential("jicksta", "roflcopterz"); CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri("http://192.168.1.62:5000"), "Basic", myCred); WebRequest request = WebRequest.Create("http://192.168.1.62:5000/launch_call_rpc"); request.Credentials = myCache; string postData = "["+jsonReflector.ToString()+"]"; //had to add the [] for it to work with restful_rpc just like php json Console.WriteLine(postData); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); } catch (Exception e) { throw e; } }
public static string ObjectToJSON(object obj) { JSONReflector r = new JSONReflector(obj); return(r.ToString()); }
private JSONValue GetJSONValue(object objValue) { if (objValue == null) { return null; } Type thisType = objValue.GetType(); JSONValue jsonValue = null; if (thisType == typeof(System.Int32)) { jsonValue = new JSONNumberValue(Convert.ToInt32(objValue)); } else if (thisType == typeof(System.Single)) { jsonValue = new JSONNumberValue(Convert.ToSingle(objValue)); } else if (thisType == typeof(System.Double)) { jsonValue = new JSONNumberValue(Convert.ToDouble(objValue)); } else if (thisType == typeof(System.Decimal)) { jsonValue = new JSONNumberValue(Convert.ToDecimal(objValue)); } else if (thisType == typeof(System.Byte)) { jsonValue = new JSONNumberValue(Convert.ToByte(objValue)); } else if (thisType == typeof(System.String)) { jsonValue = new JSONStringValue(Convert.ToString(objValue)); } else if (thisType == typeof(System.Boolean)) { jsonValue = new JSONBoolValue(Convert.ToBoolean(objValue)); } else if (thisType.BaseType == typeof(System.Enum)) { jsonValue = new JSONStringValue(Enum.GetName(thisType, objValue)); } else if (thisType.IsArray) { List<JSONValue> jsonValues = new List<JSONValue>(); Array arrValue = (Array)objValue; for (int x = 0; x < arrValue.Length; x++) { JSONValue jsValue = this.GetJSONValue(arrValue.GetValue(x)); jsonValues.Add(jsValue); } jsonValue = new JSONArrayCollection(jsonValues); } else if ( typeof(IDictionary<string, string>).IsAssignableFrom(thisType)) { Dictionary<JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary<JSONStringValue, JSONValue>(); foreach (KeyValuePair<string, string> pair in (IDictionary<string, string>)objValue) { JSONStringValue jsonParameterName = new JSONStringValue(pair.Key); JSONStringValue jsonParameterValue = new JSONStringValue(pair.Value); if (jsonParameterValue != null) { jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue); } } jsonValue = new JSONObjectCollection(jsonNameValuePairs); } else if (thisType.IsSubclassOf(typeof(System.Object))) { jsonValue = new JSONReflector(objValue); } return jsonValue; }
static void ReflectiveWay() { //Construct my own custom object Tavern tavern = new Tavern(); tavern.Business = "Tractor Tavern"; tavern.Address = "5213 Ballard Ave NW"; tavern.City = "Seattle"; tavern.State = "WA"; tavern.Zipcode = 98107; tavern.Latitude = 47.665663; tavern.Longitude = -122.382343; tavern.CoverCharge = true; tavern.Url = "http://tractortavern.citysearch.com/"; tavern.AddPaymentMethod(PaymentMethod.Cash); tavern.AddPaymentMethod(PaymentMethod.Visa); tavern.AddPaymentMethod(PaymentMethod.Mastercard); tavern.AddPaymentMethod(PaymentMethod.AmericanExpress); //Pass it to our static reflector, which will build JSONReflector jsonReflector = new JSONReflector(tavern); // The ToString() is the compact representation of the object's JSON output Console.WriteLine("JSONReflector.ToString()"); Console.WriteLine("==============================="); Console.WriteLine(jsonReflector.ToString()); Console.WriteLine("==============================="); Console.WriteLine(); // PrettyPrint() is great for readability Console.WriteLine("JSONReflector.PrettyPrint()"); Console.WriteLine("==============================="); Console.WriteLine(jsonReflector.PrettyPrint()); Console.WriteLine("==============================="); }
public static string ObjectToJSON(object obj) { JSONReflector r = new JSONReflector(obj); return r.ToString(); }