public object ToObject(string json, Type type) { JsonParser jp = new JsonParser(json); object decoded = jp.Decode(); if (decoded is ArrayList) { ArrayList tmp = decoded as ArrayList; if ((tmp == null) || (tmp.Count <= 0)) { return(null); } Type elemType = null; if (type.IsGenericType) { elemType = type.GetGenericArguments()[0]; } else if (type.IsArray) { elemType = type.GetElementType(); } else { elemType = type; } Array createdArray = CreateArray(tmp, type, elemType, null) as Array; return(createdArray); } Dictionary <string, object> ht = decoded as Dictionary <string, object>; if (ht == null) { return(null); } return(ParseDictionary(ht, null, type)); }
private string makeRequest(string method, string body, out Dictionary<string, object> responseObj, bool encrypt = true) { string uri = "http://tuner.pandora.com/services/json/"; uri += "?method=" + method; if (authToken != null) uri += "&auth_token=" + HttpUtility.UrlEncode(authToken); if (partnerID != null) uri += "&partner_id=" + partnerID; if (userID != null) uri += "&user_id=" + userID; WebRequest req = HttpWebRequest.Create(uri); req.Method = "POST"; if (encrypt) body = PandoraCrypt.Encrypt(body); byte[] bodyBytes = Encoding.UTF8.GetBytes(body); req.ContentLength = bodyBytes.Length; req.ContentType = "application/json"; Stream dataStream = req.GetRequestStream(); dataStream.Write(bodyBytes, 0, bodyBytes.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)req.GetResponse(); string status = null; if (response.StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string stringResponse = reader.ReadToEnd(); JsonParser parser = new JsonParser(stringResponse, false); Dictionary<string, object> outerResponse = (Dictionary<string, object>)parser.Decode(); if (outerResponse.ContainsKey("stat") && (string)outerResponse["stat"] == "ok") { status = "ok"; responseObj = (Dictionary<string, object>)outerResponse["result"]; } else { status = outerResponse["stat"] as string; responseObj = null; } response.Close(); } else { responseObj = null; } return status; }
public void GetBookmarks(Folder folder, AsyncCallback callback) { String url = "https://www.instapaper.com/api/1/bookmarks/list"; String[] scriptParams = { url, "folder_id", folder.Id }; callApi(url, scriptParams, new AsyncCallback(delegate(IAsyncResult res) { String respResult = readResult(); List<Bookmark> bookmarks = new List<Bookmark>(); JsonParser parser = new JsonParser(respResult); var parsedBookmarksResult = parser.Decode(); ArrayList bookmarkArray = parsedBookmarksResult as ArrayList; foreach (Dictionary<String, Object> bookmark in bookmarkArray) { if ((bookmark["type"] as String).Equals("bookmark")) { Bookmark current = new Bookmark(this); current.Id = (String)bookmark["bookmark_id"]; current.Title = (String)bookmark["title"]; current.Url = (String)bookmark["url"]; current.Starred = ((String)bookmark["starred"]).Equals("1"); bookmarks.Add(current); } } folder.Bookmarks = bookmarks; callback.Invoke(res); })); }
private void __verifyDone(IAsyncResult asyn) { JsonParser p = new JsonParser(readResult()); var decoded = p.Decode(); Console.WriteLine(decoded.GetType() == typeof(System.Collections.ArrayList)); ArrayList arr = decoded as ArrayList; Console.WriteLine(arr[0].GetType()); Dictionary<String, Object> vals = arr[0] as Dictionary<String, Object>; foreach(String k in vals.Keys) Console.WriteLine(k + " " + vals[k]); }
public void VerifyCredentials() { String url = "https://www.instapaper.com/api/1/account/verify_credentials"; String[] scriptParams = { url }; createRequest(url, scriptParams); // do the request try { currentRequest.BeginGetResponse(new AsyncCallback(__verifyDone), null); } catch (WebException we) { Console.WriteLine("========RESPONSE ERROR========="); Stream s = we.Response.GetResponseStream(); StreamReader sr2 = new StreamReader(s); String response = sr2.ReadToEnd(); JsonParser p = new JsonParser(response); var decoded = p.Decode(); Console.WriteLine(decoded.GetType()); sr2.Close(); s.Close(); Console.WriteLine("========EXCEPTION MESSAGE========="); Console.WriteLine(we); Console.WriteLine("========/RESPONSE ERROR========="); readResult(); } }
public void GetFolderList(User user, AsyncCallback callback) { String url = "https://www.instapaper.com/api/1/folders/list"; String[] scriptParams = { url }; callApi(url, scriptParams, delegate(IAsyncResult result){ String respResult = readResult(); List<Folder> folders = new List<Folder>(); // manually add the predefined folders Folder unread = new Folder(this); unread.Id = Folder.UNREAD; unread.Title = "Unread"; Folder starred = new Folder(this); starred.Id = Folder.STARRED; starred.Title = "Starred"; Folder archive = new Folder(this); archive.Id = Folder.ARCHIVE; archive.Title = "Archive"; folders.Add(unread); folders.Add(starred); folders.Add(archive); JsonParser parser = new JsonParser(respResult); var parsedFolderResult = parser.Decode(); ArrayList folderArray = parsedFolderResult as ArrayList; foreach (Dictionary<String, Object> folder in folderArray) { Folder current = new Folder(this); current.Id = folder["folder_id"] as String; current.Title = folder["title"] as String; folders.Add(current); } user.Folders = folders; callback.Invoke(result); }); }