Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom dict key types in JSON #602

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -13185,11 +13185,20 @@ json_encode_set(EncoderState *self, PyObject *obj)
return status;
}

static int json_encode_dict_key_noinline(EncoderState *, PyObject *);

static MS_INLINE int
json_encode_dict_key(EncoderState *self, PyObject *key) {
if (MS_LIKELY(PyUnicode_Check(key))) {
return json_encode_str(self, key);
}
return json_encode_dict_key_noinline(self, key);
}

static MS_NOINLINE int
json_encode_dict_key(EncoderState *self, PyObject *obj) {
json_encode_dict_key_noinline(EncoderState *self, PyObject *obj) {
PyTypeObject *type = Py_TYPE(obj);

/* PyUnicode_Type is handled inline in json_encode_dict */
if (type == &PyLong_Type) {
return json_encode_long_as_str(self, obj);
}
Expand Down Expand Up @@ -13220,6 +13229,18 @@ json_encode_dict_key(EncoderState *self, PyObject *obj) {
else if (PyType_IsSubtype(type, (PyTypeObject *)(self->mod->UUIDType))) {
return json_encode_uuid(self, obj);
}
else if (self->enc_hook != NULL) {
int status = -1;
PyObject *temp;
temp = CALL_ONE_ARG(self->enc_hook, obj);
if (temp == NULL) return -1;
if (!Py_EnterRecursiveCall(" while serializing an object")) {
status = json_encode_dict_key(self, temp);
Py_LeaveRecursiveCall();
}
Py_DECREF(temp);
return status;
}
else {
PyErr_SetString(
PyExc_TypeError,
Expand All @@ -13241,12 +13262,7 @@ json_encode_dict(EncoderState *self, PyObject *obj)
if (ms_write(self, "{", 1) < 0) return -1;
if (Py_EnterRecursiveCall(" while serializing an object")) return -1;
while (PyDict_Next(obj, &pos, &key, &val)) {
if (MS_LIKELY(PyUnicode_Check(key))) {
if (json_encode_str(self, key) < 0) goto cleanup;
}
else {
if (json_encode_dict_key(self, key) < 0) goto cleanup;
}
if (json_encode_dict_key(self, key) < 0) goto cleanup;
if (ms_write(self, ":", 1) < 0) goto cleanup;
if (json_encode_inline(self, val) < 0) goto cleanup;
if (ms_write(self, ",", 1) < 0) goto cleanup;
Expand Down Expand Up @@ -16513,6 +16529,9 @@ json_decode_dict_key_fallback(
else {
out = PyUnicode_DecodeUTF8(view, size, NULL);
}
if (MS_UNLIKELY(type->types & (MS_TYPE_CUSTOM | MS_TYPE_CUSTOM_GENERIC))) {
return ms_decode_custom(out, self->dec_hook, type, path);
}
return ms_check_str_constraints(out, type, path);
}
if (type->types & (
Expand Down
40 changes: 40 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,46 @@ class mystr(str):
msg = msgspec.json.encode({mystr("test"): 1})
assert msg == b'{"test":1}'

def test_encode_dict_custom_key(self):
class Custom:
def __init__(self, value):
self.value = value

msg = msgspec.json.encode(
{Custom("a"): 1, Custom("b"): 2}, enc_hook=lambda x: x.value
)
assert msg == b'{"a":1,"b":2}'

def enc_hook(x):
raise TypeError("Oh no!")

with pytest.raises(TypeError):
msgspec.json.encode({Custom("x"): 1}, enc_hook=enc_hook)

with pytest.raises(RecursionError):
msgspec.json.encode({Custom("x"): 1}, enc_hook=lambda x: x)

def test_decode_dict_custom_key(self):
class Custom:
def __init__(self, value):
self.value = value

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 = 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}

@pytest.mark.parametrize(
"s, error",
[
Expand Down
Loading