Python bindings for Jsonic JSON reader library.
Project description
python-jsonic
Python bindings for Jsonic JSON reader library.
Install
PIP:
pip install pyjsonic
Git:
git clone https://github.com/rohanrhu/python-jsonic
cd python-jsonic
python setup.py install
Usage
Import
import jsonic
Types and Functions
Function: from_file
Read file and returns Jsonic()
object.
root = jsonic.from_file("file.json")
Type: Jsonic
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.
root = jsonic.Jsonic("1234")
print(root.root()) # 1234
root = jsonic.Jsonic("\"foo\"")
print(root.root()) # foo
root = jsonic.Jsonic("true")
print(root.root()) # True
root = jsonic.Jsonic("null")
print(root.root()) # jsonic.Null
root = jsonic.Jsonic("{}")
print(root.root()) # None
print(root.type) # jsonic.TYPE_OBJECT
root = 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.
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.
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
import jsonic
root = 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: break
name = 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:break
print("\t\t%s" % power)
print()
Example JSON (heroes.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 JSON reader library for C/C++.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pyjsonic-1.10.tar.gz
.
File metadata
- Download URL: pyjsonic-1.10.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e496deb743f6751e4f294341da570298a1bcaca354e7a20248ef3fce2eff24e |
|
MD5 | faa2e5db225ee1f3667e253383103f91 |
|
BLAKE2b-256 | 11b987462e541f60cd475fff98ae9e6518677dbd569e20aed6d33f48fe9ce8f5 |