QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
QgsJsonUtils Class Reference

Helper utilities for working with JSON and GeoJSON conversions. More...

#include <qgsjsonutils.h>

Static Public Member Functions

static void addCrsInfo (json &value, const QgsCoordinateReferenceSystem &crs)
 Add crs information entry in json object regarding old GeoJSON specification format if it differs from OGC:CRS84 or EPSG:4326.
 
static Q_INVOKABLE QString encodeValue (const QVariant &value)
 Encodes a value to a JSON string representation, adding appropriate quotations and escaping where required.
 
static QString exportAttributes (const QgsFeature &feature, QgsVectorLayer *layer=nullptr, const QVector< QVariant > &attributeWidgetCaches=QVector< QVariant >())
 Exports all attributes from a QgsFeature as a JSON map type.
 
static json exportAttributesToJsonObject (const QgsFeature &feature, QgsVectorLayer *layer=nullptr, const QVector< QVariant > &attributeWidgetCaches=QVector< QVariant >(), bool useFieldFormatters=true)
 Exports all attributes from a QgsFeature as a json object.
 
static QgsGeometry geometryFromGeoJson (const json &geometry)
 Parses a GeoJSON "geometry" value to a QgsGeometry object.
 
static QgsGeometry geometryFromGeoJson (const QString &geometry)
 Parses a GeoJSON "geometry" value to a QgsGeometry object.
 
static json jsonFromVariant (const QVariant &v)
 Converts a QVariant v to a json object.
 
static QVariant jsonToVariant (const json &value)
 Converts a JSON value to a QVariant, in case of parsing error an invalid QVariant is returned.
 
static Q_INVOKABLE QVariantList parseArray (const QString &json, QMetaType::Type type=QMetaType::Type::UnknownType)
 Parse a simple array (depth=1)
 
Q_INVOKABLE static Q_DECL_DEPRECATED QVariantList parseArray (const QString &json, QVariant::Type type)
 Parse a simple array (depth=1)
 
static QVariant parseJson (const QString &jsonString)
 Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned.
 
static QVariant parseJson (const std::string &jsonString)
 Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and an error is logged to the message log.
 
static QVariant parseJson (const std::string &jsonString, QString &error)
 Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and the error argument is populated accordingly.
 
static QgsFeatureList stringToFeatureList (const QString &string, const QgsFields &fields=QgsFields(), QTextCodec *encoding SIP_PYARGREMOVE6=nullptr)
 Attempts to parse a GeoJSON string to a collection of features.
 
static QgsFields stringToFields (const QString &string, QTextCodec *encoding SIP_PYARGREMOVE6=nullptr)
 Attempts to retrieve the fields from a GeoJSON string representing a collection of features.
 

Detailed Description

Helper utilities for working with JSON and GeoJSON conversions.

Definition at line 320 of file qgsjsonutils.h.

Member Function Documentation

◆ addCrsInfo()

void QgsJsonUtils::addCrsInfo ( json &  value,
const QgsCoordinateReferenceSystem crs 
)
static

Add crs information entry in json object regarding old GeoJSON specification format if it differs from OGC:CRS84 or EPSG:4326.

According to new specification RFC 7946, coordinate reference system for all GeoJSON coordinates is assumed to be OGC:CRS84 but when user specifically request a different CRS, this method adds this information in the JSON output

Definition at line 925 of file qgsjsonutils.cpp.

◆ encodeValue()

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

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

Parameters
valuevalue to encode
Returns
encoded value

Definition at line 285 of file qgsjsonutils.cpp.

◆ exportAttributes()

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

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

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

Definition at line 322 of file qgsjsonutils.cpp.

◆ exportAttributesToJsonObject()

json QgsJsonUtils::exportAttributesToJsonObject ( const QgsFeature feature,
QgsVectorLayer layer = nullptr,
const QVector< QVariant > &  attributeWidgetCaches = QVector<QVariant>(),
bool  useFieldFormatters = true 
)
static

Exports all attributes from a QgsFeature as a json object.

Parameters
featurefeature to export
layeroptional associated vector layer. If specified, this allows richer export utilising settings like the layer's fields widget configuration.
attributeWidgetCachesoptional widget configuration cache. Can be used to speed up exporting the attributes for multiple features from the same layer.
useFieldFormattersWhether field formatters should be used (since QGIS 3.40)
Note
Not available in Python bindings
Since
QGIS 3.8

Definition at line 905 of file qgsjsonutils.cpp.

◆ geometryFromGeoJson() [1/2]

QgsGeometry QgsJsonUtils::geometryFromGeoJson ( const json &  geometry)
static

Parses a GeoJSON "geometry" value to a QgsGeometry object.

Returns a null geometry if the geometry could not be parsed.

Note
Not available in Python bindings.
Since
QGIS 3.36

Definition at line 698 of file qgsjsonutils.cpp.

◆ geometryFromGeoJson() [2/2]

QgsGeometry QgsJsonUtils::geometryFromGeoJson ( const QString &  geometry)
static

Parses a GeoJSON "geometry" value to a QgsGeometry object.

Returns a null geometry if the geometry could not be parsed.

Since
QGIS 3.36

Definition at line 709 of file qgsjsonutils.cpp.

◆ jsonFromVariant()

json QgsJsonUtils::jsonFromVariant ( const QVariant &  v)
static

Converts a QVariant v to a json object.

Note
Not available in Python bindings
Since
QGIS 3.8

Definition at line 723 of file qgsjsonutils.cpp.

◆ jsonToVariant()

QVariant QgsJsonUtils::jsonToVariant ( const json &  value)
static

Converts a JSON value to a QVariant, in case of parsing error an invalid QVariant is returned.

Note
Not available in Python bindings
Since
QGIS 3.36

Definition at line 806 of file qgsjsonutils.cpp.

◆ parseArray() [1/2]

QVariantList QgsJsonUtils::parseArray ( const QString &  json,
QMetaType::Type  type = QMetaType::Type::UnknownType 
)
static

Parse a simple array (depth=1)

Parameters
jsonthe JSON to parse
typeoptional variant type of the elements, if specified (and not Invalid), the array items will be converted to the type, and discarded if the conversion is not possible.

Definition at line 346 of file qgsjsonutils.cpp.

◆ parseArray() [2/2]

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

Parse a simple array (depth=1)

Parameters
jsonthe JSON to parse
typeoptional variant type of the elements, if specified (and not Invalid), the array items will be converted to the type, and discarded if the conversion is not possible.
Deprecated:
QGIS 3.38. Use the method with a QMetaType::Type argument instead.

Definition at line 415 of file qgsjsonutils.cpp.

◆ parseJson() [1/3]

QVariant QgsJsonUtils::parseJson ( const QString &  jsonString)
static

Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned.

Note
Not available in Python bindings
Since
QGIS 3.8

Definition at line 900 of file qgsjsonutils.cpp.

◆ parseJson() [2/3]

QVariant QgsJsonUtils::parseJson ( const std::string &  jsonString)
static

Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and an error is logged to the message log.

Note
Not available in Python bindings
Since
QGIS 3.8

Definition at line 778 of file qgsjsonutils.cpp.

◆ parseJson() [3/3]

QVariant QgsJsonUtils::parseJson ( const std::string &  jsonString,
QString &  error 
)
static

Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and the error argument is populated accordingly.

Note
Not available in Python bindings
Since
QGIS 3.24

Definition at line 791 of file qgsjsonutils.cpp.

◆ stringToFeatureList()

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

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

It is possible to specify fields to parse specific fields, if not provided, no fields will be included. An encoding can be specified which defaults to UTF-8 if it is nullptr.

Returns
a list of parsed features, or an empty list if no features could be parsed
See also
stringToFields()
Note
this function is a wrapper around QgsOgrUtils::stringToFeatureList()

Definition at line 269 of file qgsjsonutils.cpp.

◆ stringToFields()

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

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

An encoding can be specified which defaults to UTF-8 if it is nullptr.

Returns
retrieved fields collection, or an empty list if no fields could be determined from the string
See also
stringToFeatureList()
Note
this function is a wrapper around QgsOgrUtils::stringToFields()

Definition at line 277 of file qgsjsonutils.cpp.


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