Skip to content

Thanks to encoding hooks, why not supporting arbitrary objects as dict keys? #568

Closed

Description

@shangoue

Description

First, thanks for this great library!
I would like to serialize a dict whose keys are instances of a class coming from another library. I am already able to serialize these objects using enc_hook parameter to convert them as strings. But if these objects are converted to strings, why we cannot use them as keys for json dict ?

Here is a minimalist example:

from typing import Any, Dict, Type
import msgspec

class OpCode:
    pass

def enc_opcode(obj: Any):
    if isinstance(obj, OpCode):
        return "opcode"
    raise NotImplementedError(f"Cannot serialize {type(obj)}")

data = {}
data[OpCode] = 1
msgspec.json.encode(data, enc_hook=enc_opcode)

Activity

  1. jcrist commented on Nov 7, 2023

    @jcrist
    Member

    Thanks for opening this; you've found a bug! This currently works correctly for msgspec.msgpack.decode and msgspec.convert, but fails for msgspec.json.decode. Will fix.

  2. user529481 commented on Aug 14, 2024

    @user529481

    Hi @jcrist, I got a chance to look at the fix and the corresponding tests. I copied your test below and made Custom inherit msgspec.Struct. Encoding still works but now decoding throws the following error

    msgspec.ValidationError: Expected `object`, got `str` - at `key` in `$`
    

    This is the code I used to generate the error (taken from your tests)

    import msgspec
    from typing import Dict
    
    class Custom(msgspec.Struct):
        value : int
    
        def __hash__(self):
            return hash(self.value)
    
        def __eq__(self, other):
            return self.value == other.value
    
    def dec_hook(typ, obj):
        if typ == Custom:
            return Custom(obj)
        raise NotImplementedError
    
    msg = msgspec.json.encode(
        {Custom("a"): 1, Custom("b"): 2}, enc_hook=lambda x: x.value
    )
    assert msg == b'{"a":1,"b":2}'
    
    obj = msgspec.json.decode(msg, type=Dict[Custom, int], dec_hook=dec_hook)
    assert obj == {Custom("a"): 1, Custom("b"): 2}

    Is this expected behavior? Would it be difficult to support msgspec Structs as dictionary keys?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions