Serializing a Parameterized Object

Can someone point me to any additional param documentation for learning the intricacies of the library? I would specifically like to look at anything related to serialization but anything other than the basic examples from the param website would be appreciated. I don’t find alot of documentation in the code so learning the internal function has been a bit challenging.

I was looking at adding the ability to serialize param.Parameterized objects as JSON so that I could easily store object state to something like a MongoDB database. I am familiar with marshmallow and was going to attempt to add this functionality to my local param classes since param is essentially doing most of the schema definition for these classes already.

When I started digging into the Github code to determine how I might go about implementing something like that I came across issue #422 where the serializer.py was refactored. I wasn’t aware that this file existed and don’t see any examples of its intended use. It sounds like what I was going for and I would prefer to use something that is officially supported rather than something I simply make work based on my limited understanding of this library. However I can’t find any examples of how it is supposed to be used.

Thanks,
Mike

1 Like

Hi @tallhamer

I’m just listening in. Better documentation and understanding serialization of param is also something I would like to have.

@ceball. Do you have some advice here?

ps. I forwarded the question to the HoloViz core devs. I hope somebody pops by.

Hi

I did indeed implement JSON serialization support in param which as you note isn’t properly documented yet. One reason for this is that while serialization of parameters, our more general plans for serializing parameterized objects (including potentially nested parameterized objects) wasn’t completed.

Here is what you can do now for example:

class Foo(param.Parameterized):
   a = param.Number(default=5, bounds=(-1,10))
   b = param.List(default=[1,2], class_=int)

>> Foo.param.serialize_parameters()
'{"name": "Foo", "a": 5, "b": [1, 2]}'

>> Foo.param.schema() # JSON schema
{'name': {'anyOf': [{'type': 'string'}, {'type': 'null'}],
  'description': 'String identifier for this object.',
  'title': 'Name'},
 'a': {'type': 'number', 'minimum': -1, 'maximum': 10, 'title': 'A'},
 'b': {'type': 'array', 'items': {'type': 'integer'}, 'title': 'B'}}

Obviously there are some things that you can’t JSON serialize well (e.g callables) but quite a few of the parameters (and their options) are supported. Hope that helps!

1 Like