This class encodes and decodes JSON strings. Spec. details, see http://www.json.org/ JSON uses Arrays and Objects. These correspond here to the datatypes ArrayList and Hashtable. All numbers are parsed to doubles.
        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 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 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();
            }
        }
 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 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);
            });
        }
Exemple #6
0
        public object ToObject(string json, Type type)
        {
            _params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;

            Reflection.Instance.SetParameters(_params);

            object o = new JsonParser(json).Decode();

            if (o == null)
            {
                return(null);
            }
#if !SILVERLIGHT
            if (type != null && type == typeof(DataSet))
            {
                return(CreateDataset(o as Dictionary <string, object>, null));
            }

            if (type != null && type == typeof(DataTable))
            {
                return(CreateDataTable(o as Dictionary <string, object>, null));
            }
#endif
            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }

            if (o is List <object> )
            {
                if (type != null && t == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(o, type));
                }

                if (type != null && t == typeof(List <>)) // deserialize to generic list
                {
                    return(RootList(o, type));
                }

                if (type == typeof(Hashtable))
                {
                    return(RootHashTable((List <object>)o));
                }
                else
                {
                    return((o as List <object>).ToArray());
                }
            }

            if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Exemple #7
0
        public object ToObject(string json, Type type)
        {
            //_params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            _usingglobals = _params.UsingGlobalTypes;
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _usingglobals = false;
            }

            var o = new JsonParser(json, _params.AllowNonQuotedKeys).Decode();

            if (o == null)
            {
                return(null);
            }

            /*if (type != null)
             * {
             *      if (type == typeof(DataSet))
             *              return CreateDataset(o as Dictionary<string, object>, null);
             *      else if (type == typeof(DataTable))
             *              return CreateDataTable(o as Dictionary<string, object>, null);
             * }*/
            if (o is IDictionary)
            {
                if (type != null && t == typeof(Dictionary <,>))                // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else                 // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }
            else if (o is List <object> )
            {
                if (type != null)
                {
                    if (t == typeof(Dictionary <,>))                    // kv format
                    {
                        return(RootDictionary(o, type));
                    }
                    else if (t == typeof(List <>))                    // deserialize to generic list
                    {
                        return(RootList(o, type));
                    }
                    else if (type.IsArray)
                    {
                        return(RootArray(o, type));
                    }
                    else if (type == typeof(Hashtable))
                    {
                        return(RootHashTable((List <object>)o));
                    }
                }
                else                 //if (type == null)
                {
                    var l = (List <object>)o;
                    if (l.Count > 0 && l[0].GetType() == typeof(Dictionary <string, object>))
                    {
                        var globals = new Dictionary <string, object>();
                        var op      = new List <object>();
                        // try to get ___types
                        foreach (var i in l)
                        {
                            op.Add(ParseDictionary((Dictionary <string, object>)i, globals, null, null));
                        }
                        return(op);
                    }
                    return(l.ToArray());
                }
            }
            else if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Exemple #8
0
        public object ToObject(string json, Type type, IList <string> warnings)
        {
            //_params.FixValues();
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = Reflection.Instance.GetGenericTypeDefinition(type);
            }
            _usingglobals = _params.UsingGlobalTypes;
            if (typeof(IDictionary).IsAssignableFrom(t) || typeof(List <>).IsAssignableFrom(t))
            {
                _usingglobals = false;
            }

            object o = new JsonParser(json, true, warnings).Decode(type);

            if (o == null)
            {
                return(null);
            }
#if !SILVERLIGHT
            if (type != null)
            {
                if (type == typeof(DataSet))
                {
                    return(CreateDataset(o as Dictionary <string, object>, null));
                }
                else if (type == typeof(DataTable))
                {
                    return(CreateDataTable(o as Dictionary <string, object>, null));
                }
            }
#endif
            if (o is IDictionary)
            {
                if (type != null && typeof(Dictionary <,>).IsAssignableFrom(t)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }
            else if (o is List <object> )
            {
                if (type != null)
                {
                    if (typeof(Dictionary <,>).IsAssignableFrom(t)) // kv format
                    {
                        return(RootDictionary(o, type));
                    }
                    else if (t == typeof(List <>)) // deserialize to generic list
                    {
                        return(RootList(o, type));
                    }
                    else if (type.IsArray)
                    {
                        return(RootArray(o, type));
                    }
                    else if (type == typeof(Hashtable))
                    {
                        return(RootHashTable((List <object>)o));
                    }
                }
                else //if (type == null)
                {
                    List <object> l = (List <object>)o;
                    if (l.Count > 0 && l[0].GetType() == typeof(Dictionary <string, object>))
                    {
                        Dictionary <string, object> globals = new Dictionary <string, object>();
                        List <object> op = new List <object>();
                        // try to get $types
                        foreach (var i in l)
                        {
                            op.Add(ParseDictionary((Dictionary <string, object>)i, globals, null, null));
                        }
                        return(op);
                    }
                    return(l.ToArray());
                }
            }
            else if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }
Exemple #9
0
        public object ToObject(string json, Type type)
        {
            _params = Parameters;
            _params.FixValues();
            Reflection.Instance.ShowReadOnlyProperties = _params.ShowReadOnlyProperties;
            Type t = null;

            if (type != null && type.IsGenericType)
            {
                t = type.GetGenericTypeDefinition();
            }
            if (t == typeof(Dictionary <,>) || t == typeof(List <>))
            {
                _params.UsingGlobalTypes = false;
            }
            _usingglobals = _params.UsingGlobalTypes;

            object o = new JsonParser(json, Parameters.IgnoreCaseOnDeserialize).Decode();

            if (type != null && type == typeof(DataSet))
            {
                return(CreateDataset(o as Dictionary <string, object>, null));
            }

            if (type != null && type == typeof(DataTable))
            {
                return(CreateDataTable(o as Dictionary <string, object>, null));
            }

            if (o is IDictionary)
            {
                if (type != null && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) // deserialize a dictionary
                {
                    return(RootDictionary(o, type));
                }
                else // deserialize an object
                {
                    return(ParseDictionary(o as Dictionary <string, object>, null, type, null));
                }
            }

            if (o is List <object> )
            {
                if (type != null && type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) // kv format
                {
                    return(RootDictionary(o, type));
                }

                if (type != null && type.GetGenericTypeDefinition() == typeof(List <>)) // deserialize to generic list
                {
                    return(RootList(o, type));
                }
                else
                {
                    return((o as List <object>).ToArray());
                }
            }

            if (type != null && o.GetType() != type)
            {
                return(ChangeType(o, type));
            }

            return(o);
        }