-
instead of storing objects in cache of pointers, add a brand field to an object to mark as visited
-
if it's a reflect.Ptr return "ptr (xxxx) -> " + getPrettyString(*deref)
-
TODO: different log levels for different output files
-
the command line can set a file descriptor, so reading files in app is not a bad idea
-
lock section, so that 5 lines of logs can all be in order
In Go, json.Marshal
returns an error in a few specific scenarios where the data structure provided to it cannot be serialized into JSON. These scenarios include:
-
Unsupported Types: Go's
json
package does not support the serialization of certain types. If you try to marshal channels, functions, or complex numbers,json.Marshal
will return an error. -
Cyclic References: If the data structure contains cyclic references (i.e., a struct that directly or indirectly references itself),
json.Marshal
will return an error. JSON cannot represent cyclic data structures. -
Invalid UTF-8 Strings: If a string or a slice of bytes contains invalid UTF-8 sequences and is set to be marshaled into a JSON string,
json.Marshal
may return an error since JSON strings must be valid UTF-8. -
Marshaler Errors: If a type implements the
json.Marshaler
interface and itsMarshalJSON
method returns an error,json.Marshal
will propagate that error. -
Pointer to Uninitialized Struct: If you pass a pointer to an uninitialized struct (a nil pointer),
json.Marshal
will return an error. -
Large Floating-Point Values: Extremely large floating-point values (like
math.Inf
ormath.NaN
) can causejson.Marshal
to return an error, as they do not have a direct representation in JSON. -
Unsupported Map Key Types: In Go, a map can have keys of nearly any type, but JSON only supports string keys in objects. If you try to marshal a map with non-string keys (like
map[int]string
),json.Marshal
will return an error.
It's important to note that json.Marshal
does not return an error for marshaling private (unexported) struct fields. Instead, it silently ignores them. To include private fields in the JSON output, you either need to export these fields (make their first letter uppercase) or provide a custom marshaling method.
Understanding these conditions can help in ensuring that the data structures used with json.Marshal
are compatible with JSON's serialization requirements.
Add a todo here 123.