2.1.3. JsonLibΒΆ

Data and UI models used for JSON rendering.

Description

Pyfx models JSON as a tree of JSONSimpleNode and creates a urwid widget (JSONListBox) to help rendering the tree on the screen.

Public Interfaces

Inside this json_lib module, there are four exposed classes in this module, namely JSONListBox, JSONListWalker, JSONNodeFactory and JSONNodeCreator.

  • JSONListBox:

    A component handles keypress and mouse events. It converts those signals into actual action on the UI widgets.

  • JSONListWalker:

    A separate component handles JSON tree traversal. It determines the prev and next UI widget inside JSONListBox.

  • JSONNodeFactory:

    The facade class which composes different JSONNodeCreator to perform type deduction on the value and create the node in the tree.

  • JSONNodeCreator:

    The public factory interface to create a specific JSONSimpleNode instance if matches the actual type.

Data Modeling

The JSON data is loaded into memory as a tree. The leaf node of the tree represents the values inside the JSON data ( represented by JSONSimpleNode), while the non-leaf node of the tree represents a logical group of values such as object or array. For example,

1[
2   {
3     "Name": "John",
4     "Age": 18
5   }
6]

Both values (John and 18) are leaf nodes, while all the other parenthesis are non-leaf nodes.

Examples

  1. Create a JSONListBox widget that can be used in urwid TUI.

1from pyfx.json_lib import JSONListBox
2from pyfx.json_lib import JSONListWalker
3
4data = [1]
5# create JSONListBox from data
6listbox = JSONListBox(JSONListWalker(data))
  1. Uses a customize rendering widgets for an user-defined class.

 1from pyfx.json_lib import JSONListBox
 2from pyfx.json_lib import JSONListWalker
 3from pyfx.json_lib.json_node_factory import JSONNodeFactory
 4from pyfx.json_lib.json_node_creator import JSONNodeCreator
 5
 6class UserNodeCreator(JSONNodeCreator):
 7    def create_node(self, key, value, **kwargs):
 8        return StringNode(key, str(value), **kwargs)
 9
10# create a customize JSONNodeFactory
11node_factory = JSONNodeFactory()
12node_factory.register(UserNodeCreator())
13
14# create JSONListBox from data using the customize node_factory
15listbox = JSONListBox(JSONListWalker(data, node_factory=node_factory))

Pre-Defined Constants for Configurations

There are some predefined constants inside this json_lib module.

  1. A predefined keys to interact with JSONListBox.

Keys

Description

up

move cursor up one line

down

move cursor down one line

e

expand all

c

collapse all

enter

toggle expansion

  1. A predefined themes to control the display of the values.

Themes

Description

json.key

the color for every object key

json.string

the color for string value in JSON

json.integer

the color for integer value in JSON

json.numeric

the color for numeric value in JSON

json.bool

the color for bool value in JSON

json.null

the color for null value in JSON

json.focused

the color when the cursor is focused on the current line when browsing JSON

Modules

pyfx.view.json_lib.array

Node implementation for JSON array type.

pyfx.view.json_lib.json_composite_end_node

Ending representation of a non-leaf node in the tree.

pyfx.view.json_lib.json_composite_node

Contains the data model for a non-leaf JSON tree node.

pyfx.view.json_lib.json_listbox

A urwid.ListBox implementation specifically for rendering JSON structure.

pyfx.view.json_lib.json_listwalker

A urwid.ListWalker implementation specifically for walking JSON tree.

pyfx.view.json_lib.json_node_creator

A public interface used to create a subclass of JSONSimpleNode.

pyfx.view.json_lib.json_node_factory

Contains a factory class that handles JSONSimpleNode type deduction and node creation.

pyfx.view.json_lib.json_simple_node

Contains the data model for a leaf JSON tree node.

pyfx.view.json_lib.json_widget

Contains the basic UI model for a JSON tree node.

pyfx.view.json_lib.object

Node implementation for JSON object type.

pyfx.view.json_lib.primitive

Node implementation of primitive JSON types.