Skip to content

Commit 3876e78

Browse files
committed
First version: 1.0
0 parents  commit 3876e78

14 files changed

Lines changed: 16484 additions & 0 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json linguist-vendored

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project.sublime-project
2+
project.sublime-workspace
3+
build

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/jsonic"]
2+
path = lib/jsonic
3+
url = https://github.com/rohanrhu/jsonic

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Oğuzhan Eroğlu <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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

examples/heroes/heroes.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"squadName": "Super \"hero\" squad",
3+
"homeTown": "Metro City",
4+
"formed": 2016,
5+
"secretBase": "Super tower",
6+
"active": true,
7+
"members": [
8+
{
9+
"name": "Molecule Man",
10+
"age": 29,
11+
"secretIdentity": "Dan Jukes",
12+
"powers": [
13+
"Radiation resistance",
14+
"Turning tiny",
15+
"Radiation blast"
16+
]
17+
},
18+
{
19+
"name": "Madame Uppercut",
20+
"age": 39,
21+
"secretIdentity": "Jane Wilson",
22+
"powers": [
23+
"Million tonne punch",
24+
"Damage resistance",
25+
"Superhuman reflexes"
26+
]
27+
},
28+
{
29+
"name": "Eternal Flame",
30+
"age": 1000000,
31+
"secretIdentity": "Unknown",
32+
"powers": [
33+
"Immortality",
34+
"Heat Immunity",
35+
"Inferno",
36+
"Teleportation",
37+
"Interdimensional travel"
38+
]
39+
}
40+
]
41+
}

examples/heroes/heroes.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import jsonic
4+
5+
from datetime import datetime
6+
t0 = datetime.now()
7+
8+
root = jsonic.from_file("heroes.json")
9+
10+
print("Root Type: %d" % root.type)
11+
print("Squad: %s" % root.iterKey("squadName"))
12+
print("Hometown: %s" % root.iterKey("homeTown"))
13+
print("Formed: %d" % root.iterKey("formed"))
14+
print("Active: %d" % root.iterKey("active"))
15+
16+
members = root.iterKey("members")
17+
18+
print("Members: (%d total)" % members.len())
19+
while True:
20+
member = members.iterItem()
21+
if not member: break
22+
23+
name = member.iterKey("name")
24+
age = member.iterKey("age")
25+
powers = member.iterKey("powers")
26+
27+
print("\tName: %s" % name)
28+
print("\tAge: %s" % age)
29+
print("\tPowers (%d total):" % powers.len())
30+
while True:
31+
power = powers.iterItem()
32+
if not power:break
33+
34+
print("\t\t%s" % power)
35+
36+
print()
37+
38+
t1 = datetime.now()
39+
dt = t1 - t0
40+
print("Time elapsed: %ds, %dus" % (dt.seconds, dt.microseconds))

0 commit comments

Comments
 (0)