← Back to context

Comment by thayne

22 days ago

> You don’t get runtime errors for marshalling pointers to JSON

https://pkg.go.dev/encoding/json/v2#Marshal says otherwise:

> The Go map key must encode as a JSON string, otherwise this results in a SemanticError

> All other Go types (e.g., complex numbers, channels, and functions) have no default representation and result in a SemanticError.

v1 has the same constraints.

> A Go pointer is encoded as a JSON null if nil, otherwise it is the recursively JSON-encoded representation of the underlying value.

The doc you linked to literally proves me right.

  • That just shows there are pointer values that can be marshaled, not that all such values won't error. If you have a non-nil pointer that points to an object that transitively contains one of the unsupported types, you will get an error.

    • I read that differently. If you have a non-nil pointer then you get the address of the pointer irrespective of what that pointer points too.

      1 reply →