|
| 1 | +# python-jsonic |
| 2 | +Python bindings for [Jsonic](https://github.com/rohanrhu/jsonic) JSON reader library. |
| 3 | + |
| 4 | +## Install |
| 5 | +PIP: |
| 6 | +```bash |
| 7 | +pip install jsonic |
| 8 | +``` |
| 9 | + |
| 10 | +Git: |
| 11 | +```bash |
| 12 | +git clone https://github.com/rohanrhu/python-jsonic |
| 13 | +cd python-jsonic |
| 14 | +python setup.py install |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Import |
| 20 | +```python |
| 21 | +import jsonic |
| 22 | +``` |
| 23 | + |
| 24 | +### Types and Functions |
| 25 | + |
| 26 | +#### Function: from_file |
| 27 | +Read file and returns `Jsonic()` object. |
| 28 | +```python |
| 29 | +root = jsonic.from_file("file.json") |
| 30 | +``` |
| 31 | + |
| 32 | +#### Type: Jsonic |
| 33 | +```python |
| 34 | +root = jsonic.Jsonic("[1, 2, 3, 4]") |
| 35 | +``` |
| 36 | + |
| 37 | +##### Member: Jsonic.type |
| 38 | +Type member is useable for checking object and array types. Except object and array types, you will get regular python `str`, `float`, `bool` or `jsonic.Null` object. |
| 39 | + |
| 40 | +###### Type Checking |
| 41 | +###### Types |
| 42 | +`jsonic.TYPE_OBJECT`<br> |
| 43 | +`jsonic.TYPE_ARRAY` |
| 44 | + |
| 45 | +`Jsonic.type` is useable for objects or arrays. Object and array values returns as `Jsonic()` objects. Null values returns as `Jsonic.Null` object. Otherwise it returns as regular python types. |
| 46 | + |
| 47 | +##### Member: Jsonic.version |
| 48 | +Version of python-jsonic. |
| 49 | + |
| 50 | +##### Member: Jsonic.json |
| 51 | +JSON String. |
| 52 | + |
| 53 | +##### Type: Jsonic.Null |
| 54 | +JSON Null type. |
| 55 | + |
| 56 | +##### Method: root() |
| 57 | +Returns JSON root's value if `root.type` is not an array or object. Otherwise it returns None. |
| 58 | +```python |
| 59 | +root = jsonic.Jsonic("1234") |
| 60 | +print(root.root()) # 1234 |
| 61 | + |
| 62 | +root = jsonic.Jsonic("foo") |
| 63 | +print(root.root()) # "foo" |
| 64 | + |
| 65 | +root = jsonic.Jsonic("null") |
| 66 | +print(root.root()) # jsonic.Null |
| 67 | + |
| 68 | +root = jsonic.Jsonic("{}") |
| 69 | +print(root.root()) # None |
| 70 | +print(root.type) # jsonic.TYPE_OBJECT |
| 71 | + |
| 72 | +root = jsonic.Jsonic("[]") |
| 73 | +print(root.root()) # None |
| 74 | +print(root.type) # jsonic.TYPE_ARRAY |
| 75 | +``` |
| 76 | + |
| 77 | +##### Method: len() |
| 78 | +Gets length of array. |
| 79 | + |
| 80 | +##### Method: key(key) |
| 81 | +Returns the key's value. |
| 82 | + |
| 83 | +##### Method: item(index) |
| 84 | +Returns item of an index on array. |
| 85 | + |
| 86 | +#### Method: iterItem(index=0) |
| 87 | +Iterates array item from last iterated item times index. |
| 88 | + |
| 89 | +```python |
| 90 | +root = jsonic.Jsonic("[1, 2, 3, 4]") |
| 91 | +print(array.iter()) # 1 |
| 92 | +print(array.iter()) # 2 |
| 93 | +print(array.iter(1)) # 4 |
| 94 | +``` |
| 95 | + |
| 96 | +#### Method: iterKey(key) |
| 97 | +Iterates object key from last iterated object. |
| 98 | +```python |
| 99 | +root = jsonic.Jsonic("{a: 1, b: 2, c: 3, d: 4}") |
| 100 | +print(array.iter("a")) # "a" |
| 101 | +print(array.iter("b")) # "b" |
| 102 | +print(array.iter("c")) # "c" |
| 103 | +print(array.iter("b")) # None |
| 104 | +``` |
| 105 | + |
| 106 | +#### Method: reset() |
| 107 | +Resets iteration current. |
| 108 | + |
| 109 | +## Example |
| 110 | +An example for reading JSON data |
| 111 | + |
| 112 | +```python |
| 113 | +import jsonic |
| 114 | + |
| 115 | +root = jsonic.from_file("heroes.json") |
| 116 | + |
| 117 | +print("Root Type: %d" % root.type) |
| 118 | +print("Squad: %s" % root.iterKey("squadName")) |
| 119 | +print("Hometown: %s" % root.iterKey("homeTown")) |
| 120 | +print("Formed: %d" % root.iterKey("formed")) |
| 121 | +print("Active: %d" % root.iterKey("active")) |
| 122 | + |
| 123 | +members = root.iterKey("members") |
| 124 | + |
| 125 | +print("Members: (%d total)" % members.len()) |
| 126 | +while True: |
| 127 | + member = members.iterItem() |
| 128 | + if not member: break |
| 129 | + |
| 130 | + name = member.iterKey("name") |
| 131 | + age = member.iterKey("age") |
| 132 | + powers = member.iterKey("powers") |
| 133 | + |
| 134 | + print("\tName: %s" % name) |
| 135 | + print("\tAge: %s" % age) |
| 136 | + print("\tPowers (%d total):" % powers.len()) |
| 137 | + while True: |
| 138 | + power = powers.iterItem() |
| 139 | + if not power:break |
| 140 | + |
| 141 | + print("\t\t%s" % power) |
| 142 | + |
| 143 | + print() |
| 144 | +``` |
| 145 | + |
| 146 | +Example JSON (heroes.json): |
| 147 | +```json |
| 148 | +{ |
| 149 | + "squadName": "Super hero squad", |
| 150 | + "homeTown": "Metro City", |
| 151 | + "formed": 2016, |
| 152 | + "secretBase": "Super tower", |
| 153 | + "active": true, |
| 154 | + "members": [ |
| 155 | + { |
| 156 | + "name": "Molecule Man", |
| 157 | + "age": 29, |
| 158 | + "secretIdentity": "Dan Jukes", |
| 159 | + "powers": [ |
| 160 | + "Radiation resistance", |
| 161 | + "Turning tiny", |
| 162 | + "Radiation blast" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "name": "Madame Uppercut", |
| 167 | + "age": 39, |
| 168 | + "secretIdentity": "Jane Wilson", |
| 169 | + "powers": [ |
| 170 | + "Million tonne punch", |
| 171 | + "Damage resistance", |
| 172 | + "Superhuman reflexes" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "name": "Eternal Flame", |
| 177 | + "age": 1000000, |
| 178 | + "secretIdentity": "Unknown", |
| 179 | + "powers": [ |
| 180 | + "Immortality", |
| 181 | + "Heat Immunity", |
| 182 | + "Inferno", |
| 183 | + "Teleportation", |
| 184 | + "Interdimensional travel" |
| 185 | + ] |
| 186 | + } |
| 187 | + ] |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## Syntax Checking |
| 192 | +This library does not check JSON syntax, so you may get `SIGSEGV` or maybe infinite loops for **corrupt JSONs**. Likewise in some cases of corrupt JSONs, it would work as properly. |
| 193 | + |
| 194 | +## Performance |
| 195 | +There are some example JSONs and reading examples in `examples/` folder for profiling the performance. |
| 196 | + |
| 197 | +## C Library |
| 198 | +You can use [Jsonic](https://github.com/rohanrhu/jsonic) JSON reader library for C/C++. |
| 199 | + |
| 200 | +## License |
| 201 | +MIT |
0 commit comments