{ "info": { "author": "Evan Sandhaus", "author_email": "evan@nytimes.com", "bugtrack_url": null, "classifiers": [], "description": "---------------------------\nSafeJSON version 0.9 beta\n---------------------------\n(c)\t2013 The New York Times Company\n\nCreated by Evan Sandhaus\n\n---------\nOverview:\n---------\nSafeJSON provides replacements for the 'load' and 'loads' methods in the standard Python 'json' module. These new methods return objects that will never raise 'IndexError' or 'KeyError' exceptions for 'missing' values. Instead a special value 'SafeNone' is returned. 'SafeNone' can itself be subscripted with any key or index. Doing so will return another SafeNone object, so that SafeNone[] == SafeNone. In practice, this should allow developers to dramatically reduce the number of checks required to deal with JSON in the wild.\n\n--------------\nImportant Note\n--------------\nRather than raise exceptions for missing values, 'safeJSON' returns a special value 'SafeNone' instead. This value can be treated in code as identical to 'None' with one major exception. The SafeNone object is not identical to None object so checks of the following form will evaluate to False.\n\n\tif SafeNone is None:\n\nSafeNone does have equality with None, however, so the following check will evaluate to True\n\t\n\tif SafeNone == None:\n\nAdditionally SafeNone implements all methods in the interface for python lists and dicts EXCEPT methods that attempt to store or modify the SafeNone object. These methods will raise an Exception.\n\nSo you may make statements like this:\n\n\tx = SafeNone['some_key']\n\nBut statements like this will raise exeptions:\n\t\n\tSafeNone['some_key'] = x\n\nThe following methods will raise exceptions when invoked on SafeNone\n\t\n\t*\t__setitem__\n\t*\t__imul__\n\t*\t__setslice__\n\t*\t__setitem__\n\t*\tappend\n\t*\textend\n\t*\tfromkeys\n\t*\tinsert\n\t*\tsetdefault\n\t*\tupdate\n\n------------\nInstallation\n------------\nTo install safeJSON simply change into the directory containing this README and type the following command\n\n\tpython setup.py install\n\n----------------------\nRationale and Example:\n----------------------\nThe standard approach to parsing JSON in python involves importing the 'json' module and using the 'load' or 'loads' method to parse a python object out of a JSON source. For example:\n\n\timport json\n\n\tJSON_STRING = \"\"\"\n\t\t{\"results\": [{\n\t \"child_items\": [{\n\t \"name\": \"child item 1\"\n\t }], \n\t \"name\": \"result 1\"\n\t }]}\n\t\"\"\"\n\to = json.loads(JSON_STRING)\n\nNow suppose we want to print out the value for the 'name' attribute of the first 'child_item' for the first 'result'. We could simply write:\n\n\tprint o['results'][0]['child_items'][0]['name']\n\nBut this is risky. In the wild it is generally not advisable to access fields in a JSON object without first verifying that such fields exist. After all, accessing a field that does not exist will raise an 'IndexError' or 'KeyError'. As such, we should modify our code to look something like this.\n\n\tif 'results' in o and len(o['results']) > 0:\n\t\tresult = o['results'][0]\n\t\tif 'child_items' in result and len(result['child_items']) > 0:\n\t\t\tchildItem = result['child_items'][0]\n\t\t\tif 'name' in childItem:\n\t\t\t\tprint childItem['name']\n\nThis code is cumbersome to type and easy to mess up. Moreover the tediousness of coding these kinds of checks likely leads some developers to omit them altogether, resulting in brittle code that makes dangerously narrow assumptions about the kind of input it's likely to encounter.\n\nsafeJSON solves this problem by introducing 'loads' and 'load' methods that return objects that will never raise 'IndexError' or 'KeyError' exceptions. As such we can write the above code as\n\t\n\timport safeJSON\n\to = safeJson.loads(JSON_STRING)\n\tprint o['results'][0]['child_items'][0]['name']\n\nIf we want to suppress output in the event that a specified value does not exist, we can introduce a simple check that will always evaluate to False if the value does not exist.\n\t\n\tif o['results'][0]['child_items'][0]['name']:\n\t\tprint o['results'][0]['child_items'][0]['name']\n\nWe can even iterate over missing items without raising an exception. Suppose, for the above example, we wanted to iterate over all the 'child_items' of the second result (which does not exist). We could write:\n\n\tfor childItem in o['results'][1]['child_items']:\n\t\t#this code will never be reached\n\t\tpass \n\nIf we wanted to iterate over the key/value pairs in the first child_item of the second result (which again does not exist), we could write\n\n\tfor key, value in o['results'][1]['child_items'][0].items():\n\t\t#this code will never be reached\n\t\tpass \n\nWe can even check the length of a 'missing' list or dictionary. \n\t\n\tx = len(o['results'][1]['child_items'])\n\tprint x # will print 0\n\nIn this case the length will always be zero.\n\n\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": null, "license": "http://www.apache.org/licenses/LICENSE-2.0", "maintainer": null, "maintainer_email": null, "name": "safeJSON", "package_url": "https://pypi.org/project/safeJSON/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/safeJSON/", "project_urls": { "Download": "UNKNOWN", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/safeJSON/0.9b/", "requires_dist": null, "requires_python": null, "summary": "safeJSON simplifies the process of working with JSON object in python by suppressing both IndexError and KeyError exceptions on parsed objects.", "version": "0.9b" }, "last_serial": 799174, "releases": { "0.9b": [ { "comment_text": "", "digests": { "md5": "826504cc06029b928dd9af94267389d4", "sha256": "23a8429889e855cba36d7bd842747f73aa6fbc9afab7274196a8d49db6121f7f" }, "downloads": -1, "filename": "safeJSON-0.9b.tar.gz", "has_sig": false, "md5_digest": "826504cc06029b928dd9af94267389d4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4594, "upload_time": "2013-04-05T21:22:10", "url": "https://files.pythonhosted.org/packages/8c/d9/cdb5ba09de3eeeaa2e73b46aad81ace44026580082e5a6477414a3b36156/safeJSON-0.9b.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "826504cc06029b928dd9af94267389d4", "sha256": "23a8429889e855cba36d7bd842747f73aa6fbc9afab7274196a8d49db6121f7f" }, "downloads": -1, "filename": "safeJSON-0.9b.tar.gz", "has_sig": false, "md5_digest": "826504cc06029b928dd9af94267389d4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4594, "upload_time": "2013-04-05T21:22:10", "url": "https://files.pythonhosted.org/packages/8c/d9/cdb5ba09de3eeeaa2e73b46aad81ace44026580082e5a6477414a3b36156/safeJSON-0.9b.tar.gz" } ] }