Skip to content

Commit

Permalink
Correctly escape more json strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 9, 2016
1 parent 729c703 commit 819ed86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/core/qgsjsonutils.cpp
Expand Up @@ -113,9 +113,15 @@ QString QgsJSONUtils::encodeValue( const QVariant &value )

default:
case QVariant::String:
QString v = value.toString().replace( '"', "\\\"" )
QString v = value.toString()
.replace( '\\', "\\\\" )
.replace( '"', "\\\"" )
.replace( '\r', "\\r" )
.replace( '\b', "\\b" )
.replace( '\t', "\\t" )
.replace( '/', "\\/" )
.replace( '\n', "\\n" );

return v.prepend( '"' ).append( '"' );
}
}
7 changes: 6 additions & 1 deletion tests/src/python/test_qgsjsonutils.py
Expand Up @@ -92,7 +92,12 @@ def testEncodeValue(self):
self.assertEqual(QgsJSONUtils.encodeValue('string'), '"string"')
self.assertEqual(QgsJSONUtils.encodeValue('str\ning'), '"str\\ning"')
self.assertEqual(QgsJSONUtils.encodeValue('str\ring'), '"str\\ring"')
self.assertEqual(QgsJSONUtils.encodeValue('str"ing'), '"str\\"ing"')
self.assertEqual(QgsJSONUtils.encodeValue('str\bing'), '"str\\bing"')
self.assertEqual(QgsJSONUtils.encodeValue('str\ting'), '"str\\ting"')
self.assertEqual(QgsJSONUtils.encodeValue('str\\ing'), '"str\\\\ing"')
self.assertEqual(QgsJSONUtils.encodeValue('str\\ning'), '"str\\\\ning"')
self.assertEqual(QgsJSONUtils.encodeValue('str\n\\\\ing'), '"str\\n\\\\\\\\ing"')
self.assertEqual(QgsJSONUtils.encodeValue('str/ing'), '"str\\/ing"')

def testFeatureToGeoJSON(self):
""" test converting features to GeoJSON """
Expand Down

0 comments on commit 819ed86

Please sign in to comment.