Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rohanrhu/python-jsonic
Python bindings for Jsonic JSON reader library.
https://github.com/rohanrhu/python-jsonic
json json-parser json-reader-library python python-json zero-allocation
Last synced: 22 days ago
JSON representation
Python bindings for Jsonic JSON reader library.
- Host: GitHub
- URL: https://github.com/rohanrhu/python-jsonic
- Owner: rohanrhu
- License: mit
- Created: 2019-10-09T11:51:24.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-04T17:25:42.000Z (about 3 years ago)
- Last Synced: 2024-11-26T23:31:21.542Z (26 days ago)
- Topics: json, json-parser, json-reader-library, python, python-json, zero-allocation
- Language: C
- Homepage: https://pypi.org/project/pyjsonic/
- Size: 85 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python-jsonic
Python bindings for [Jsonic](https://github.com/rohanrhu/jsonic) JSON reader library.## Install
PIP:
```bash
pip install pyjsonic
```Git:
```bash
git clone https://github.com/rohanrhu/python-jsonic
cd python-jsonic
python setup.py install
```## Usage
### Import
```python
import jsonic
```### Types and Functions
#### Function: from_file
Read file and returns `Jsonic()` object.
```python
root = jsonic.from_file("file.json")
```#### Type: Jsonic
```python
root = jsonic.Jsonic("[1, 2, 3, 4]")
```##### Member: Jsonic.type
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.###### Type Checking
###### Types
`jsonic.TYPE_OBJECT`
`jsonic.TYPE_ARRAY``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.
##### Member: Jsonic.version
Version of python-jsonic.##### Member: Jsonic.json
JSON String.##### Type: Jsonic.Null
JSON Null type.##### Method: root()
Returns JSON root's value if `root.type` is not an array or object. Otherwise it returns None.
```python
root = jsonic.Jsonic("1234")
print(root.root()) # 1234root = jsonic.Jsonic("\"foo\"")
print(root.root()) # fooroot = jsonic.Jsonic("true")
print(root.root()) # Trueroot = jsonic.Jsonic("null")
print(root.root()) # jsonic.Nullroot = jsonic.Jsonic("{}")
print(root.root()) # None
print(root.type) # jsonic.TYPE_OBJECTroot = jsonic.Jsonic("[]")
print(root.root()) # None
print(root.type) # jsonic.TYPE_ARRAY
```##### Method: len()
Gets length of array.##### Method: key(key)
Returns the key's value.##### Method: item(index)
Returns item of an index on array.#### Method: iterItem(index=0)
Iterates array item from last iterated item times index.```python
root = jsonic.Jsonic("[1, 2, 3, 4]")
print(array.iterItem()) # 1
print(array.iterItem()) # 2
print(array.iterItem(1)) # 4
print(array.iterItem()) # None
array.reset()
print(array.iterItem()) # 1
```#### Method: iterKey(key)
Iterates object key from last iterated object.
```python
root = jsonic.Jsonic("{\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}")
print(array.iterKey("a")) # 1
print(array.iterKey("b")) # 2
print(array.iterKey("c")) # 3
print(array.iterKey("b")) # None
array.reset()
print(array.iterKey("b")) # 2
```#### Method: reset()
Resets iteration current.## Example
An example for reading JSON data```python
import jsonicroot = jsonic.from_file("heroes.json")
print("Root Type: %d" % root.type)
print("Squad: %s" % root.iterKey("squadName"))
print("Hometown: %s" % root.iterKey("homeTown"))
print("Formed: %d" % root.iterKey("formed"))
print("Active: %d" % root.iterKey("active"))members = root.iterKey("members")
print("Members: (%d total)" % members.len())
while True:
member = members.iterItem()
if not member: breakname = member.iterKey("name")
age = member.iterKey("age")
powers = member.iterKey("powers")print("\tName: %s" % name)
print("\tAge: %s" % age)
print("\tPowers (%d total):" % powers.len())
while True:
power = powers.iterItem()
if not power:breakprint("\t\t%s" % power)
print()
```Example JSON (heroes.json):
```json
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
```## Syntax Checking
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.## Performance
There are some example JSONs and reading examples in `examples/` folder for profiling the performance.## C Library
You can use [Jsonic](https://github.com/rohanrhu/jsonic) JSON reader library for C/C++.## License
MIT