Skip to content

feat(core): Support serialization of opaque objects #75

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

Merged
merged 1 commit into from
May 17, 2025

Conversation

potatomashed
Copy link
Collaborator

@potatomashed potatomashed commented May 17, 2025

Serialization/deserialization support for mlc.Opaque. Part of #73.

For any MLC object, it now supports:

  • .json(...) method adds argument fn_opaque_serialize, which allows users to supply a customized serialization function;
  • .from_json(...) adds argument fn_opaque_deserialize, which allows users to supply a customized deserialization function.

By default, the serialization and deserialization methods are:

@mlc.Func.register("mlc.Opaque.default.serialize")
def _default_serialize(opaques: list[Any]) -> str:
    return jsonpickle.dumps(list(opaques))

@mlc.Func.register("mlc.Opaque.default.deserialize")
def _default_deserialize(json_str: str) -> list[Any]:
    return jsonpickle.loads(json_str)

jsonpickle is not a perfect library - usually pickle or cloudpickle could be substantially better in terms of feature completeness, but it actually gives a balance between serializability and readability.

@potatomashed potatomashed marked this pull request as ready for review May 17, 2025 06:06
@potatomashed potatomashed merged commit 45fae3e into mlc-ai:main May 17, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant