1

Besides functions in Go, are there any other types that cannot be marshaled using json.Marhsal?

If I pass a func to json.Marshal, I get:

json: unsupported type: func()

are there any other data types in Golang that can't be serialized / marshaled? I am writing a mini-library and want to know if I need to check for other things which can't be serialized.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • You should ask the opposite: what objects supports marshaling? I believe Go docs covers this, have a look there. – Chen A. Feb 13 '20 at 08:31
  • 1
    Just read the package doc it explains in detail what can and cannot be marshaled. – Volker Feb 13 '20 at 08:39

1 Answers1

2

From json.Marshal():

Channel, complex, and function values cannot be encoded in JSON. Attempting to encode such a value causes Marshal to return an UnsupportedTypeError.

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.

Going further, not mentioned in the doc, but unsafe.Pointer also cannot be marshaled.

Same goes for any composite type containing an unsupported type (e.g. slice of channels, struct holding an unsafe.Pointer exported field etc.).

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827