Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kashifrazzaqui/json-streamer

A fast streaming JSON parser for Python that generates SAX-like events using yajl
https://github.com/kashifrazzaqui/json-streamer

Last synced: 2 months ago
JSON representation

A fast streaming JSON parser for Python that generates SAX-like events using yajl

Awesome Lists containing this project

README

        

{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from jsonstreamer import JSONStreamer\n",
"\n",
"json_object = \"\"\"\n",
" {\n",
" \"fruits\":[\"apple\",\"banana\", \"cherry\"],\n",
" \"calories\":[100,200,50]\n",
" }\n",
"\"\"\"\n",
"\n",
"json_array = \"\"\"\n",
" [1,2,true,[4,5],\"a\"]\n",
"\"\"\"\n",
"\n",
"def _catch_all(event_name, *args):\n",
" print('\\t{} : {}'.format(event_name, args))\n",
"\n",
"print(\"\\nParsing the json object:\")\n",
"streamer = JSONStreamer()\n",
"streamer.add_catch_all_listener(_catch_all)\n",
"streamer.consume(json_object[0:10])\n",
"streamer.consume(json_object[10:])\n",
"streamer.close()\n",
"\n",
"print(\"\\nParsing the json array:\")\n",
"streamer = JSONStreamer()\n",
"streamer.add_catch_all_listener(_catch_all)\n",
"streamer.consume(json_array[0:5])\n",
"streamer.consume(json_array[5:])\n",
"streamer.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Parsing the json object:\n",
"\tdoc_start : ()\n",
"\tobject_start : ()\n",
"\tkey : ('fruits',)\n",
"\tarray_start : ()\n",
"\telement : ('apple',)\n",
"\telement : ('banana',)\n",
"\telement : ('cherry',)\n",
"\tarray_end : ()\n",
"\tkey : ('calories',)\n",
"\tarray_start : ()\n",
"\telement : (100,)\n",
"\telement : (200,)\n",
"\telement : (50,)\n",
"\tarray_end : ()\n",
"\tobject_end : ()\n",
"\tdoc_end : ()\n",
"\n",
"Parsing the json array:\n",
"\tdoc_start : ()\n",
"\tarray_start : ()\n",
"\telement : (1,)\n",
"\telement : (2,)\n",
"\telement : (True,)\n",
"\tarray_start : ()\n",
"\telement : (4,)\n",
"\telement : (5,)\n",
"\tarray_end : ()\n",
"\telement : ('a',)\n",
"\tarray_end : ()\n",
"\tdoc_end : ()\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from jsonstreamer import ObjectStreamer\n",
"\n",
"print(\"\\nParsing the json object:\")\n",
"object_streamer = ObjectStreamer()\n",
"object_streamer.add_catch_all_listener(_catch_all)\n",
"object_streamer.consume(json_object[0:9])\n",
"object_streamer.consume(json_object[9:])\n",
"object_streamer.close()\n",
"\n",
"print(\"\\nParsing the json array:\")\n",
"object_streamer = ObjectStreamer()\n",
"object_streamer.add_catch_all_listener(_catch_all)\n",
"object_streamer.consume(json_array[0:4])\n",
"object_streamer.consume(json_array[4:])\n",
"object_streamer.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Parsing the json object:\n",
"\tobject_stream_start : ()\n",
"\tpair : (('fruits', ['apple', 'banana', 'cherry']),)\n",
"\tpair : (('calories', [100, 200, 50]),)\n",
"\tobject_stream_end : ()\n",
"\n",
"Parsing the json array:\n",
"\tarray_stream_start : ()\n",
"\telement : (1,)\n",
"\telement : (2,)\n",
"\telement : (True,)\n",
"\telement : ([4, 5],)\n",
"\telement : ('a',)\n",
"\tarray_stream_end : ()\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class MyClass:\n",
" \n",
" def __init__(self):\n",
" self._obj_streamer = ObjectStreamer()\n",
" self._obj_streamer.auto_listen(self)\n",
" \n",
" def _on_object_stream_start(self):\n",
" print ('Root Object Started')\n",
" \n",
" def _on_pair(self, pair):\n",
" print('Key: {} - Value: {}'.format(pair[0],pair[1]))\n",
" \n",
" def consume(self, data):\n",
" self._obj_streamer.consume(data)\n",
" \n",
" \n",
"m = MyClass()\n",
"m.consume(json_object)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Root Object Started\n",
"Key: fruits - Value: ['apple', 'banana', 'cherry']\n",
"Key: calories - Value: [100, 200, 50]\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ob_streamer = ObjectStreamer()\n",
"\n",
"def pair_listener(pair):\n",
" print('Explicit listener: Key: {} - Value: {}'.format(pair[0],pair[1]))\n",
" \n",
"ob_streamer.add_listener('pair', pair_listener)\n",
"ob_streamer.consume(json_object)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Explicit listener: Key: fruits - Value: ['apple', 'banana', 'cherry']\n",
"Explicit listener: Key: calories - Value: [100, 200, 50]\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}