QgsJsonUtils class

Helper utilities for working with JSON and GeoJSON conversions.

Contents

Public static functions

static auto encodeValue(const QVariant& value) -> QString
Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.
static auto exportAttributes(const QgsFeature& feature, QgsVectorLayer* layer = nullptr, const QVector<QVariant>& attributeWidgetCaches = QVector<QVariant>()) -> QString
Exports all attributes from a QgsFeature as a JSON map type.
static auto parseArray(const QString& json, QVariant::Type type) -> QVariantList
Parse a simple array (depth=1).
static auto stringToFeatureList(const QString& string, const QgsFields& fields, QTextCodec* encoding) -> QgsFeatureList
Attempts to parse a GeoJSON string to a collection of features.
static auto stringToFields(const QString& string, QTextCodec* encoding) -> QgsFields
Attempts to retrieve the fields from a GeoJSON string representing a collection of features.

Function documentation

static QString QgsJsonUtils::encodeValue(const QVariant& value)

Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.

Parameters
value value to encode
Returns encoded value

static QString QgsJsonUtils::exportAttributes(const QgsFeature& feature, QgsVectorLayer* layer = nullptr, const QVector<QVariant>& attributeWidgetCaches = QVector<QVariant>())

Exports all attributes from a QgsFeature as a JSON map type.

Parameters
feature feature to export
layer optional associated vector layer. If specified, this allows richer export utilising settings like the layer's fields widget configuration.
attributeWidgetCaches optional widget configuration cache. Can be used to speed up exporting the attributes for multiple features from the same layer.

static QVariantList QgsJsonUtils::parseArray(const QString& json, QVariant::Type type)

Parse a simple array (depth=1).

Parameters
json the JSON to parse
type the type of the elements

static QgsFeatureList QgsJsonUtils::stringToFeatureList(const QString& string, const QgsFields& fields, QTextCodec* encoding)

Attempts to parse a GeoJSON string to a collection of features.

Parameters
string GeoJSON string to parse
fields fields collection to use for parsed features
encoding text encoding
Returns list of parsed features, or an empty list if no features could be parsed

static QgsFields QgsJsonUtils::stringToFields(const QString& string, QTextCodec* encoding)

Attempts to retrieve the fields from a GeoJSON string representing a collection of features.

Parameters
string GeoJSON string to parse
encoding text encoding
Returns retrieved fields collection, or an empty list if no fields could be determined from the string