JsonTreeModel API  v0.1.0
A Qt tree model for visualizing and manipulating a JSON document.
JsonTreeModel Class Reference

The JsonTreeModel class provides a data model for a JSON document. More...

#include <jsontreemodel.h>

Inheritance diagram for JsonTreeModel:

Public Types

enum  ScalarColumnSearchMode { NoSearch, QuickSearch, ComprehensiveSearch }
 This enum controls how setJson() updates the model's column headers. More...
 

Public Member Functions

 JsonTreeModel (QObject *parent=nullptr)
 Constructs an empty JsonTreeModel with the given parent. More...
 
 ~JsonTreeModel () override
 Destroys the JsonTreeModel and frees its memory. More...
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex parent (const QModelIndex &index) const override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 Returns the number of rows under the given parent. More...
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 Returns the number of columns in the model. More...
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 Returns data under the given index for the specified role. More...
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
void setJson (const QJsonArray &array, ScalarColumnSearchMode searchMode=QuickSearch)
 Sets the whole model's internal data structure to the given JSON array. More...
 
void setJson (const QJsonObject &object, ScalarColumnSearchMode searchMode=QuickSearch)
 Sets the whole model's internal data structure to the given JSON object. More...
 
QJsonValue json (const QModelIndex &index=QModelIndex()) const
 Returns the JSON value under the given index. More...
 
void setScalarColumns (const QStringList &columns)
 Sets the JSON objects' scalar members that are shown by the model. More...
 
QStringList scalarColumns () const
 Returns the names of the JSON objects' scalar members that are shown by the model. More...
 

Detailed Description

The JsonTreeModel class provides a data model for a JSON document.

JsonTreeModel represents an arbitrary JSON document as a tree. It supports unlimited nesting of JSON arrays and JSON objects. For compactness, scalar members of JSON objects are placed under named columns.

  • Column 0 shows the structure of the JSON document. It contains array index numbers and object member names.
  • Column 1 shows the scalar elements of JSON arrays.
  • Columns 2 and above show the scalar members of JSON objects. The header text for these columns are the names of the object members, either discovered during the call to setJson(), or set manually via setScalarColumns(). These columns are called the named scalar columns.

For example, the following JSON document contains an array of similar objects which can be compacted into a table:

[
{
"First Name": "Hua",
"Last Name": "Li",
"Phone Number": "+86 21 51748525",
"Country": "China"
}, {
"First Name": "Gildong",
"Last Name": "Hong",
"Phone Number": "+82 31 712 0045",
"Country": "South Korea"
}, {
"First Name": "Tarou",
"Last Name": "Yamada",
"Phone Number": "+81 3 6264 4500",
"Country": "Japan"
}, {
"First Name": "Jane",
"Last Name": "Doe",
"Phone Number": "+1 408 906 8400",
"Country": "USA"
}, {
"First Name": "Erika",
"Last Name": "Mustermann",
"Phone Number": "+49 30 63923257",
"Country": "Germany"
}, {
"First Name": "Pyotr",
"Last Name": "Ivanov",
"Phone Number": "+7 921 097 7252",
"Country": "Russia"
}, {
"First Name": "Kari",
"Last Name": "Nordmann",
"Phone Number": "+47 21 08 04 20",
"Country": "Norway"
}
]
example_table.png
Note
Use QTableView::hideColumn() or QTreeView::hideColumn() to hide the empty Scalar column

Here is a more hierarchical example:

{
"Server Properties": {
"Server ID": "314159",
"Client IP Addresses": [
"192.168.0.10",
"192.168.0.11",
"192.168.0.12"
]
},
"Analog Inputs": [
{
"Channel Name": "Transducer X",
"Analog Input Type": "Voltage",
"Scale": 1,
"Offset": 0,
"High Resolution": false
},
{
"Channel Name": "Sensor Y",
"Analog Input Type": "Current",
"Scale": 6.25,
"Offset": -25,
"High Resolution": true
}
]
}
example_tree.png

Member Enumeration Documentation

◆ ScalarColumnSearchMode

This enum controls how setJson() updates the model's column headers.

See also
setJson(), setScalarColumns()
Enumerator
NoSearch 

setJson() leaves the model headers unchanged. The caller should manually update the column headers via setScalarColumns().

QuickSearch 

setJson() does a quick scan on the new JSON document. The model scans every member of JSON objects, but only scans the first element of JSON arrays.

ComprehensiveSearch 

setJson() does a full scan on the new JSON document. The model scans every member of JSON objects and JSON arrays. All scalar members will be found and displayed, but this could be expensive for large JSON documents.

Constructor & Destructor Documentation

◆ JsonTreeModel()

JsonTreeModel::JsonTreeModel ( QObject *  parent = nullptr)
explicit

Constructs an empty JsonTreeModel with the given parent.

◆ ~JsonTreeModel()

JsonTreeModel::~JsonTreeModel ( )
inlineoverride

Destroys the JsonTreeModel and frees its memory.

Member Function Documentation

◆ columnCount()

int JsonTreeModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const
override

Returns the number of columns in the model.

This number is the same for the entire model; the parent is irrelevant.

  • Column 0 shows the structure of the JSON document. It contains array index numbers and object member names.
  • Column 1 shows the scalar elements of JSON arrays.
  • Columns 2 and above are the named scalar columns, corresponding to scalarColumns().
See also
rowCount(), scalarColumns()

◆ data()

QVariant JsonTreeModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override

Returns data under the given index for the specified role.

Only valid when role is Qt::DisplayRole or Qt::EditRole.

If index.column() is 0, then this function returns the array index or object member name that corresponds to index.row(). This differs from json(), which returns the full JSON value if index.column() is 0.

Note
This function is designed for interfacing with Qt item views. To access data, use json().
See also
setData(), json()

◆ flags()

Qt::ItemFlags JsonTreeModel::flags ( const QModelIndex &  index) const
override

◆ headerData()

QVariant JsonTreeModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
override

Horizontal headers show the text of scalarColumns() for the third column onwards. Vertical headers show the text of column 0.

◆ index()

QModelIndex JsonTreeModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
override

◆ json()

QJsonValue JsonTreeModel::json ( const QModelIndex &  index = QModelIndex()) const

Returns the JSON value under the given index.

If the index is invalid, then this function returns the entire JSON document stored in the model.

If index.column() is 0, then this function returns the full JSON value at index.row(); the value could be a JSON object or JSON array. If index.column() is non-zero, this function returns a single scalar value.

Example:

example_table.png
  • Index (6, 2) returns the string "Kari".
  • Index (6, 1) returns an undefined QJsonValue.
  • Index (6, 0) returns the following object:
    {
    "First Name": "Kari",
    "Last Name": "Nordmann",
    "Phone Number": "+47 21 08 04 20",
    "Country": "Norway"
    }
  • Invalid indices return the whole document:
    [
    {
    "First Name": "Hua",
    "Last Name": "Li",
    "Phone Number": "+86 21 51748525",
    "Country": "China"
    }, {
    "First Name": "Gildong",
    "Last Name": "Hong",
    "Phone Number": "+82 31 712 0045",
    "Country": "South Korea"
    }, {
    "First Name": "Tarou",
    "Last Name": "Yamada",
    "Phone Number": "+81 3 6264 4500",
    "Country": "Japan"
    }, {
    "First Name": "Jane",
    "Last Name": "Doe",
    "Phone Number": "+1 408 906 8400",
    "Country": "USA"
    }, {
    "First Name": "Erika",
    "Last Name": "Mustermann",
    "Phone Number": "+49 30 63923257",
    "Country": "Germany"
    }, {
    "First Name": "Pyotr",
    "Last Name": "Ivanov",
    "Phone Number": "+7 921 097 7252",
    "Country": "Russia"
    }, {
    "First Name": "Kari",
    "Last Name": "Nordmann",
    "Phone Number": "+47 21 08 04 20",
    "Country": "Norway"
    }
    ]
See also
setJson(), data()

◆ parent()

QModelIndex JsonTreeModel::parent ( const QModelIndex &  index) const
override

◆ rowCount()

int JsonTreeModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
override

Returns the number of rows under the given parent.

If the parent represents a JSON array, then the row count equals the number of array elements. If the parent represents a JSON object, then the row count equals the number of child arrays and child objects combined.

See also
columnCount()

◆ scalarColumns()

QStringList JsonTreeModel::scalarColumns ( ) const
inline

Returns the names of the JSON objects' scalar members that are shown by the model.

See also
setScalarColumns()

◆ setData()

bool JsonTreeModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role = Qt::EditRole 
)
override

◆ setJson() [1/2]

void JsonTreeModel::setJson ( const QJsonArray &  array,
ScalarColumnSearchMode  searchMode = QuickSearch 
)

Sets the whole model's internal data structure to the given JSON array.

If searchMode is QuickSearch (default) or ComprehensiveSearch, this function also updates the column headers.

See also
json(), setData()

◆ setJson() [2/2]

void JsonTreeModel::setJson ( const QJsonObject &  object,
ScalarColumnSearchMode  searchMode = QuickSearch 
)

Sets the whole model's internal data structure to the given JSON object.

If searchMode is QuickSearch (default) or ComprehensiveSearch, this function also updates the column headers.

See also
json(), setData()

◆ setScalarColumns()

void JsonTreeModel::setScalarColumns ( const QStringList &  columns)

Sets the JSON objects' scalar members that are shown by the model.

The model's named scalar columns are set to the list of specified columns, in the listed order.

If a scalar member of a JSON object is named after one of these columns, it is shown under that column. Otherwise, the member is hidden.

See also
scalarColumns(), setJson()

The documentation for this class was generated from the following files: