Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON serializeability with PROJJSON? #1361

Open
hobu opened this issue Nov 20, 2023 · 7 comments
Open

Add JSON serializeability with PROJJSON? #1361

hobu opened this issue Nov 20, 2023 · 7 comments

Comments

@hobu
Copy link

hobu commented Nov 20, 2023

CRS objects are not JSON serializable:

import pyproj
import json

crs = pyproj.CRS.from_user_input("EPSG:3857")

json.dumps(crs) #throws exception

I would have expected this to Just Work ™️ by using PROJJSON as the JSON i/o capability. This should work as lossless i/o in all be the rarest/weirdest situations, right @rouault?

@hobu hobu added the proposal Idea for a new feature. label Nov 20, 2023
@rouault
Copy link

rouault commented Nov 20, 2023

This should work as lossless i/o in all be the rarest/weirdest situations, right @rouault?

yes

@snowman2 snowman2 added this to the 3.7.0 milestone Nov 20, 2023
@snowman2 snowman2 added help wanted and removed proposal Idea for a new feature. labels Nov 20, 2023
@jjimenezshaw
Copy link
Contributor

@hobu Can json.dumps(crs) work without passing an encoder in the cls parameter?

@meliber
Copy link

meliber commented Nov 28, 2023

The CRS has to_json and to_json_dict methods which can return JSON string or dict which is consistent with PROJJSON.

@snowman2
Copy link
Member

Summary of options I have thought of:

Option 1: Use CRS.to_json #1361 (comment)

Option 2: Use encoder #1361 (comment)

import json
from pyproj import CRS

class PROJEncoder(json.JSONEncoder):
    def default(self, obj):
        try:
            return crs.to_json_dict()
        except AttributeError:
            pass 
        return super().default(obj)

crs = CRS.from_user_input("EPSG:3857")
json.dumps(crs, cls=PROJEncoder)

Option 3: Ask Python json library to support a class method like __json__ so encoders are not needed anymore (possibly with a PEP) and then update pyproj to support it....

@snowman2 snowman2 removed this from the 3.7.0 milestone Feb 17, 2024
@djhoese
Copy link
Contributor

djhoese commented Feb 17, 2024

The __json__ solution would need to be split into an encode and decode method, right?

@snowman2
Copy link
Member

The __json__ solution would need to be split into an encode and decode method, right?

Possiblity. Only thinking about one-way to encode to JSON at the moment as I don't think some things round-trip easily.

@hobu
Copy link
Author

hobu commented Feb 17, 2024

I suppose Option #3 has been asked many times and there's some good reason for not implementing something like it over the years.

Hopefully this ticket generates enough google juice for the next person and points them to use CRS.to_json(). I'm happy to close this if there's no action to be taken on the pyproj side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants