Zope publisher converter for :json like it exists for :int

Hi everybody,

we often use suffix like :int or :boolean so parameters passed to URL are auto converted to correct python format by the Zope publisher.

This is nice and work in every ways, including JS data variables like "data-myfield:int" or so submitted by JS to call a Zope view.

As far as I can see, there is no :json converter.

Is there a package somewhere implementing it? Would it have sense to include it in the ZPublisher package? What we would like to do is have something like data-myfield:json="{'key1': 'value1', 'key2': 'value2'}" in a HTML content and be managed automatically when passed by JS function to called Zope view. Instead for now we receive a string and need to use json.loads to manage it.

Thank you and have a nice day,

Gauthier

1 Like

You may write something yourself...there is a registry for the type converters in Zope (see end of file)

Hi @zopyx

yes indeed I wrote one myself :wink: and it works as expected...

from ZPublisher.Converters import type_converters

import json

if 'field2json' not in type_converters:
    def field2json(v):
        v = json.loads(v)
        return v
    type_converters['json'] = field2json

Just wondered finally if it worth a PR in ZPublisher too or if it should handle only "real python type" that is not the case for json.

I will add an issue in ZPublisher and see.

Thank you for feedback!

Gauthier

2 Likes

Should be:

if 'json' not in type_converters:

Indeed :wink:

We added this to a local helpers package for now, will see if it may be integrated into ZPublisher Converters.py...

Thank you for review!

Gauthier

Hm, where do you put this code so it gets used by Zope's publishing machinery?

Hi @jugmac00

here is the code we added to a local package for now : imio.helpers/converters.py at master · IMIO/imio.helpers · GitHub

We put it there so it is not enabled by default, you just have to import the package in the init.py of another package : import imio.helpers.converters

I intend to write an issue tomorrow (friday) on the Zope/ZPublisher issue tracker to propose to integrate it there.

Thank you for interest,

Gauthier

1 Like

Thanks! I did not know you can call this from "regular" code, and I imagined you need to hook into Zope's startup ...

While I am not a heavy user of Zope's converter, they are very handy.

A couple of years ago I spent a lot of time with them and found a couple of issues ...