
Golang's json.rawmessage is a powerful tool for efficient JSON data processing. It allows you to work with JSON data in a more flexible and efficient way.
The json.rawmessage type is a type alias for []byte, which means it's a slice of bytes that represents a JSON message. This is useful because it allows you to manipulate the JSON data as a binary string, rather than having to parse it into a Go struct.
To use json.rawmessage effectively, you need to understand how to work with it in your Go code. This includes how to encode and decode JSON data using the json package.
Recommended read: Golang Go
Explicit Unmarshaling
Explicit unmarshaling can be achieved using the json.RawMessage type, which stores raw data as a []byte. This allows you to control the unmarshaling of data explicitly.
By using json.RawMessage, you can delay unmarshaling until after branching out based on the value of Type, but this requires explicit marshaling of Msg first.
However, another approach is to unmarshal into a raw map, which can be done by passing the JSON data just once. This method is reusable and only unmarshals each piece of data once, but it doesn't have a significant impact on performance.
Check this out: Convert a Map to Json Golang
Unmarshaling Explicitly

You can explicitly control the unmarshaling of data by using json.RawMessage, which stores the raw data as a []byte.
This allows you to delay unmarshaling until after you've branched out based on the value of Type, but it does require you to explicitly marshal Msg first, or use separate EnvelopeIn and EnvelopeOut types.
Using json.RawMessage lets you postpone the unmarshaling, which can be beneficial in certain situations.
Explore further: Read a Custom Resource Using Cynamic Client Golang
Invalid Unmarshal Error
An InvalidUnmarshalError can occur if an invalid argument is passed to Unmarshal.
This is because the argument to Unmarshal must be a non-nil pointer. If a nil pointer is passed, Unmarshal will throw an error.
In essence, Unmarshal expects a valid pointer to work with. Anything less will result in an error.
See what others are reading: Golang Create Error
Raw Message Type
The Raw Message Type is a powerful tool in Go's JSON handling capabilities.
It's called RawMessage and it's a raw encoded JSON object.
RawMessage implements Marshaler and Unmarshaler, allowing it to be used to delay JSON decoding or precompute a JSON encoding.
Discover more: Golang Read Json File
This can be particularly useful when dealing with large JSON messages.
For example, RawMessage can be used to delay parsing part of a JSON message, which can help improve performance.
The MarshalJSON method returns *m as the JSON encoding of m, making it a convenient way to work with JSON data.
Unmarshaling into a raw map is another approach that can be taken with RawMessage.
This involves passing the JSON data over just once, which can help optimize performance.
However, as the article notes, this approach doesn't seem to have a significant impact on performance, at least in this particular example.
Worth a look: Define a Map of Custom Schema Data Type Golang
UnmarshalJSON Function
The UnmarshalJSON function is a crucial part of working with JSON data in Go. It sets *m to a copy of the data.
This function is used to unmarshal JSON data into a RawMessage. UnmarshalJSON sets *m to a copy of data, which is exactly what we need to work with our JSON data.
The UnmarshalJSON function is a method of the RawMessage type, and it's used to unmarshal JSON data into a RawMessage.
Additional reading: Golang Reflect to Call Function in Package
Using Raw Message
RawMessage can be used to delay JSON decoding or precompute a JSON encoding.
RawMessage is a raw encoded JSON object that implements Marshaler and Unmarshaler.
It can be used to delay parsing part of a JSON message, like in Example 1, where MarshalJSON returns *m as the JSON encoding of m.
This approach allows you to keep the benefits of JSON while optimizing performance.
You can also use RawMessage to unmarshal into a raw map, as shown in Example 2, which can be reused and unmarshals each piece of data only once.
使用场景
In some business scenarios, you might need a generic model that adapts to different structures depending on the context.
This is where json.RawMessage comes in handy, allowing you to define a field as a raw message that can be adapted to various structures.
With json.RawMessage, you can define a field as a raw message that can be adapted to various structures.
Additional reading: Golang Message
You can use json.RawMessage to store data in a database and then read it back out, restoring the original model.
This approach is particularly useful when you need to store data in a database and then read it back out, restoring the original model.
For instance, you can define a field as a raw message that can be adapted to different structures depending on the scenario.
json.RawMessage can be used to store data in a database, making it a versatile tool for handling complex data structures.
复用预计算的Json值
Using Raw Message allows us to delay JSON decoding or precompute a JSON encoding. This can be beneficial in certain situations.
RawMessage is a raw encoded JSON object that implements Marshaler and Unmarshaler. It can be used to reuse precomputed JSON values.
The example shows that we can use RawMessage to reuse a precomputed JSON value. It does this by comparing the precomputed value with a RawMessage instance, which are identical in this case.
This approach is useful when we need to reuse a precomputed JSON value in our code.
Discover more: Declate a Map of String and Value as Map Golang
Unmarshal to Raw Map
Using json.RawMessage can delay JSON decoding, but it's not without its downsides. You'll need to explicitly marshal Msg first or create separate EnvelopeIn and EnvelopeOut types.
A RawMessage is a raw encoded JSON object that implements Marshaler and Unmarshaler. This allows you to delay JSON decoding or precompute a JSON encoding. MarshalJSON returns *m as the JSON encoding of m.
This approach can be useful for parsing part of a JSON message by instructing the parser to skip certain fields and keeping them in their raw []byte shape.
Featured Images: pexels.com


