• Stars
    star
    2
  • Language
    C
  • License
    MIT License
  • Created about 5 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python bindings for Jsonic JSON reader library.

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

More Repositories

1

gdb-frontend

โ˜• GDBFrontend is an easy, flexible and extensible gui debugger. Try it on https://debugme.dev
JavaScript
2,813
star
2

cebsocket

Lightweight WebSocket library for C.
C
85
star
3

nodes.js

๐ŸŒŒ nodes.js is a nodes/particles animation useable for backgrounds
JavaScript
27
star
4

gdb-frontend-live

GDBFrontendLive is a server that creates GDBFrontend instances and provides online sharable IDEs for each individiual users. ๐Ÿ›ธ๐Ÿ‘ฝ๐ŸŒŒ
C
24
star
5

jsonic

โญ• Tricky, super fast and dumb JSON library for C/C++
C
20
star
6

virtual-joystick

Virtual Joystick plugin for Godot Engine
GDScript
13
star
7

GodotCarouselMenu

Horizontal carousel menu for Godot 4
GDScript
13
star
8

vegetables

Multiplayer deathmatch shooter game with cute vegetable characters.
GDScript
13
star
9

MoneroSharp

Monero library for C#
C#
12
star
10

react-kitten

React desktop environment for the web. React-ish and highly customizable. ๐Ÿฑ
TypeScript
9
star
11

jquery.datepicker

โŒš a futuristic datepicker for web
JavaScript
9
star
12

libhash

A fast and efficient non-iterating hashmap library
C
6
star
13

node-Win32Volume

Node.js, set volume level or mute functions for Win32 platform.
C++
4
star
14

json-tcp-socket

JSON messaging over TCP sockets for Node.js
JavaScript
3
star
15

pokerunicorn-server

PokerUnicorn Server
C
3
star
16

BigCat

Big number library for Godot Engine (GDScript)
GDScript
3
star
17

3DAudioVisualization

3D audio visualization thing in Godot Engine
GDScript
3
star
18

GodotBorderRadius

Pixel-sized border radius shader for Godot 4
GDScript
2
star
19

CatPong

Low-level multiplayer pong game with pure TCP protocol and SDL2 graphics for the Cat tutorial on meowingcat.io.
C
2
star
20

snake

Snake is a snake game implementation on HTML5.
JavaScript
2
star
21

meow

Quick screen recorder to GIF
JavaScript
2
star
22

HexBinDecConverter

Hexadecimal, Binary and Decimal number converter plugin for Sublime Text 3
Python
1
star
23

semserv

High-performance async semaphore service useable with long string ipc keys stored in memory.
C
1
star
24

WinDrag

Linux Desktop Environments-Like Alt+Drag Window Mover for Windows
C#
1
star
25

catcrypt

Simple RSA public key encryption library for C/C++.
C
1
star
26

sound-level-protect

Maximum sound level protection for headphones on Win32 platform.
C++
1
star