Override the JS Context Serializer¶
Problem¶
The default JSON serializer does not know how to encode Pydantic models or other custom types that you publish through @context(serialize=True).
Solution¶
Point NEXT_FRAMEWORK["JS_CONTEXT_SERIALIZER"] at a serializer class, or pass serializer= on a single @context decorator.
Walkthrough¶
Use the Bundled Pydantic Serializer¶
The framework ships PydanticJsContextSerializer.
Point the setting at it.
NEXT_FRAMEWORK = {
"JS_CONTEXT_SERIALIZER": "next.static.PydanticJsContextSerializer",
}
Context functions can now return Pydantic models directly.
from pydantic import BaseModel
from next.pages import context
class NoteOut(BaseModel):
id: int
title: str
@context("featured", serialize=True)
def featured() -> NoteOut:
return NoteOut(id=1, title="Hello")
Write a Custom Serializer¶
A serializer is any class with a dumps method that returns a JSON string.
See JavaScript Context for the protocol and a minimal CompactSerializer example, then point JS_CONTEXT_SERIALIZER at your dotted path.
Per Key Override¶
Pass serializer= on a single @context so only that key uses a different encoder. Everything else keeps the project default.
See the Per Key Serializer section in JavaScript Context for a concrete snippet.
Verification¶
Reload a page and inspect window.Next.context in the browser console.
The values that could not serialise before now appear as proper JSON.
See Also¶
See also
JavaScript Context for the topic guide.
Context for the serialize flag.