Creating WSME object with non-validated dictionary

134 views
Skip to first unread message

Michał Dulko

unread,
Dec 5, 2013, 11:55:39 AM12/5/13
to pytho...@googlegroups.com
I want to create an WSME object for use with Pecan (to create a JSON REST API) that will store some data and a dictionary of user-definied parameters which are custom. This means I can't create another object for options and use it here like this:

class Foo():
    id = int
    name = text
    options = Bar

I tried:
class Foo():
    id = int
    name = text
    options = dict

Unfortunately this solution fails as dict is not serializable. How can I achieve custom dictionary in WSME object? The dictionary can contain different types of data (float, int, bool, string).

Christophe de Vienne

unread,
Dec 5, 2013, 2:39:40 PM12/5/13
to pytho...@googlegroups.com
Hello,



Le 05/12/2013 17:55, Michał Dulko a écrit :
> [...]
>
> How can I achieve custom dictionary in WSME object? The dictionary can
> contain different types of data (float, int, bool, string).

WSME does not support attribute with non-fixed types. That includes
dictionnaries.

So you have to specify what types will your dictionnary will carry, for
example : {str: int} is a dictionnary with string keys and integer
values (see the doc [1] for an example).

If you must have 'variable' type for the values, one way is to define
complex type with one attribute for each potential type :
class ValueHolder(Base):
as_int = int
as_str = str
# and so on

and use it in your dictionnary :
{str: ValueHolder}

Json serialized values would look like :
{"somekey": {"as_int": 5}, "someotherkey": {"as_str": "some text"}}


Cheers,

Christophe

[1] https://wsme.readthedocs.org/en/latest/types.html#native-types

Reply all
Reply to author
Forward
0 new messages