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
JSONNodeCreatorto perform type deduction on the value and create the node in the tree.
JSONNodeCreator:The public factory interface to create a specific
JSONSimpleNodeinstance 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
Create a
JSONListBoxwidget 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))
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.
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 |
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
Node implementation for JSON array type. |
|
Ending representation of a non-leaf node in the tree. |
|
Contains the data model for a non-leaf JSON tree node. |
|
A |
|
A |
|
A public interface used to create a subclass of JSONSimpleNode. |
|
Contains a factory class that handles |
|
Contains the data model for a leaf JSON tree node. |
|
Contains the basic UI model for a JSON tree node. |
|
Node implementation for JSON object type. |
|
Node implementation of primitive JSON types. |