batou icon indicating copy to clipboard operation
batou copied to clipboard

Object of type NetLoc is not JSON serializable

Open frlan opened this issue 3 years ago • 3 comments

Having this code:

import batou.utils
import batou.componet
import batou.lib.file

class MyComponent(batou.component.Component):

	def configure(self):
		self.address = batou.utils.Address(self.host.fqdn, 1234)
		self += batou.lib.file.JSONContent(
			'my.json', 
			data={
			    "myAddress": self.haproxy.address.connect
			})

Leads to an

ERROR: TypeError('Object of type NetLoc is not JSON serializable')

To solve this, an explicit converting to a string did work -- but unsure whether we should default to some str representation here as we have e.g. for Jinja-templating.

frlan avatar Jul 21 '21 09:07 frlan

We can do this by implementing a custom json encoder in JSONContent.

ctheune avatar Aug 19 '21 09:08 ctheune

Note that deserialization will likely not be possible in a roundtrip style.

ctheune avatar Aug 19 '21 09:08 ctheune

I can see the same issue with YAMLContent

frlan avatar May 24 '23 07:05 frlan