Context object not JSON serialisable

I’m facing a weird issue,

my script does several publishes using sgtk.util.register_publish() and then creates a version and links the various publishes to this version.

I have a context created with context_from_entity.

The register_publish functions work just fine and create publishes linked to my context.
The sg.create(“Version”, data) function errors out on the same context opbject with the following error:

TypeError: is not JSON serializable

Why?

This is the full log:

Traceback (most recent call last):
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\app_store\tk-multi-publish2\v2.5.0\python\tk_multi_publish2\api\plugins\publish_plugin_instance.py”, line 280, in _handle_plugin_error
yield
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\app_store\tk-multi-publish2\v2.5.0\python\tk_multi_publish2\api\plugins\publish_plugin_instance.py”, line 196, in run_publish
self._hook_instance.publish(settings, item)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\config\hooks\tk-multi-publish2\tk-nuke\editorial\ingest_tool\create_version_and_publish.py”, line 387, in publish
sg_version = sg.create(“Version”, data)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\shotgun.py”, line 1363, in create
record = self._call_rpc(“create”, params, first=True)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank\authentication\shotgun_wrapper.py”, line 63, in _call_rpc
return super(ShotgunWrapper, self)._call_rpc(*args, **kwargs)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\shotgun.py”, line 3291, in _call_rpc
encoded_payload = self._encode_payload(payload)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\shotgun.py”, line 3417, in encode_payload
wire = json.dumps(payload, ensure_ascii=False)
File "C:\Program Files\Nuke11.3v6\lib\json_init
.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File “C:\Program Files\Nuke11.3v6\lib\json\encoder.py”, line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File “C:\Program Files\Nuke11.3v6\lib\json\encoder.py”, line 270, in iterencode
return _iterencode(o, 0)
File “C:\Program Files\Nuke11.3v6\lib\json\encoder.py”, line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: is not JSON serializable

1 Like

Weird, the type name is empty, the error should be <type> is not JSON serializable.
What sort of objects does the payload contain?

1 Like

Just a dictionary of normal version keys:

“project”: item.context.project,
“entity”: item.properties[“context”],
“code”: item.properties[“file_head”],
“description”: item.description,
“sg_first_frame”: item.properties[“first_frame”],
“sg_last_frame”: item.properties[“last_frame”],
“frame_range”: item.properties[“framerange”],

1 Like

It’s a bit confusing that the api is used differently in the collector and the publisher, I often have to go back and forth to verify I’m using the right syntax.

Should that be item.properties["framerange"].value instead?
https://developer.shotgunsoftware.com/tk-multi-publish2/api.html#tk_multi_publish2.api.PluginSetting

You could try serializing individual objects before this step to see which one breaks it.

1 Like

No I know it works because if I comment out the

“entity”: item.properties[“context”]

the version creation is happy and works fine.

The context object was created using tk.context_from_entity() and it worked fine in the register_publish function before the version creation.

1 Like

Oh sorry, I missed this in your post, but yes, I see now.
I don’t think contexts are serializable, actually. register_publish might be doing some additional conversion.

2 Likes

Right I solved it.

I have to create a context object (context_from_entity) for the register_publish and a simple dict context for the api sg.create.

It’s a bit confusing indeed to figure out when to use which.

1 Like

Glad it worked. Just to be sure, there is the context.to_dict() function, which you could use.

2 Likes

Oh thats nice!

I’ll use that!

1 Like