Fallback to raw string value, when json field inside geojson isn't decodable#1470
Open
jbylina wants to merge 1 commit intoToblerity:maint-1.10from
Open
Fallback to raw string value, when json field inside geojson isn't decodable#1470jbylina wants to merge 1 commit intoToblerity:maint-1.10from
jbylina wants to merge 1 commit intoToblerity:maint-1.10from
Conversation
382d2ab to bd4e7c8 Compare bd4e7c8 to 33001a3 Compare sgillies requested changes Jan 2, 2025
Comment on lines +417 to +418
| except json.JSONDecodeError: | ||
| return val |
Member
There was a problem hiding this comment.
Suggested change
| except json.JSONDecodeError: | |
| return val | |
| except json.JSONDecodeError as error: | |
| warnings.warn(f"JSON field value not decoded, returning as string: {val=}, {error=}", FeatureWarning) | |
| log.info("JSON field value not decoded, returning as string: val=%r, error=%r", val, error) | |
| return val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Expected behavior and actual behavior.
I expected to successfully read the following geojson file by Fiona and leave the handling of mixed types geojson properties to lib user.
{ "type": "FeatureCollection", "features": [ {"type": "Feature", "properties": { "a": [ ]}, "geometry": { "type": "Point", "coordinates": [ [-74, 5]] } }, {"type": "Feature", "properties": { "a": ""}, "geometry": { "type": "Point", "coordinates": [ [-74, 5]] } } ] }Actual behavior:
Steps to reproduce the problem.
Run the following script using the latest Fiona.
Operating system
macOS 15.1.1
Fiona and GDAL version and provenance
Fiona 1.10.1 from pip
GDAL 3.10.0 installed via Homebrew
Comment
When I flip lines, so that
[ ]geojson property isn't first, the code doesn't fail.{ "type": "FeatureCollection", "features": [ {"type": "Feature", "properties": { "a": ""}, "geometry": { "type": "Point", "coordinates": [ [-74, 5]] } }, {"type": "Feature", "properties": { "a": [ ]}, "geometry": { "type": "Point", "coordinates": [ [-74, 5]] } } ] }Fix
I propose to fall back to raw string value if json field property isn't decodable. When we look at different json values allowed by RFC, and how it's handled by json.loads() the string is the only one that requires different handling.
When geojson is processed, at the point of calling JSONField.get() we know that geojson is a valid json so the above cases are the only possible ones.
json.loads("[")is impossible, so the decoding exception is possible only in case of the string.