question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Core] Specify the serializer for a certain object

See original GitHub issue

Describe your feature request

The default serializer has many issues with certain objects. One case is the pydantic model. We can specify a flag __ray_serializer__ in these object types to enforce ray using pickle as the default serializer instead. This avoids changing other functions like ray.put, ray.remote and xxxx.remote(...). This could also cover our major cases and is super flexible.

One example could be:

from pydantic import BaseModel, validator

class UserModel(BaseModel):
    __ray_serializer__ = 'pickle'
    name: str
    username: str
    password1: str
    password2: str

    @validator('name')
    def name_must_contain_space(cls, v):
        if ' ' not in v:
            raise ValueError('must contain a space')
        return v.title()

    @validator('password2')
    def passwords_match(cls, v, values, **kwargs):
        if 'password1' in values and v != values['password1']:
            raise ValueError('passwords do not match')
        return v

    @validator('username')
    def username_alphanumeric(cls, v):
        assert v.isalnum(), 'must be alphanumeric'
        return v

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
suquarkcommented, Oct 31, 2020

yeah, a trivial solution is to overload __reduce__ with pickle.dumps, so cloudpickle then serialize the pickled content again.

def __reduce__(self):
   return pickle.loads, (pickle.dumps(self),)
0reactions
simon-mocommented, Oct 31, 2020

I think @pcmoritz has a great idea here:

Hey Simon, I think you can circumvent the requirement to pickle the class for reduce if you do something like this:

In [10]: def unpickle(*args):
    ...:     return # ...
    ...: class Foo(object):
    ...:   def __init__(self, file_name = 'file.txt'):
    ...:     self.file_name = file_name
    ...:     self.f = open(self.file_name, 'w')
    ...: 
    ...:   def __reduce__(self):
    ...:     return (unpickle, (self.file_name, ))

I.e. instead of putting the class as the first argument for the return value of reduce, put a custom function.

Now I just need to define unparse as:

def unparse(*args):
    from package import Foo
    return Foo(*args)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serialize and deserialize JSON using C# - .NET
NET objects as JSON (serialize); Serialize to UTF-8; Serialization behavior ... NET Core app, some default behaviors are different.
Read more >
Json.net serialize only certain properties - Stack Overflow
If you have a property on your object that is null or the default value, you can let json.net ignore it and NOT...
Read more >
System.Text.Json: How to serialize only certain properties ...
We want that when we serialize it with our own JsonSerializer we add our customization to only allow certain properties. 2- For security...
Read more >
Serializing Django objects — Django 4.1.3 documentation
Each field of the object is serialized as a <field> -element sporting the fields “type” and “name”. The text content of the element...
Read more >
Object serialization - R3 Documentation
The primary way Corda's AMQP serialization framework instantiates objects is via a specified constructor. This is used to first determine which properties ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found