简体   繁体   中英

What's the difference between UnmarshalText and UnmarshalJson?

In decode.go , it mentions:

// To unmarshal JSON into a value implementing the Unmarshaler interface,
// Unmarshal calls that value's UnmarshalJSON method, including
// when the input is a JSON null.
// Otherwise, if the value implements encoding.TextUnmarshaler
// and the input is a JSON quoted string, Unmarshal calls that value's
// UnmarshalText method with the unquoted form of the string.

What are the differences between UnmarshalText and UnmarshalJSON ? Which one is preferred?

Simply:

  • UnmarshalText unmarshals a text-encoded value.
  • UnmarshalJSON unmarshals a JSON-encoded value.

Which is preferred depends on what you're doing.

JSON encoding is defined by RFC 7159 . If you're consuming or producing JSON documents, you should use JSON encoding.

Text encoding has no standard, and is entirely implementation-dependent. Go implements Text-(un)marshalers for a few types, but there's no guarantee that any other application will understand these formats.

Text-encoding is most commonly used for things like URL query parameters, HTML forms, or other loosely-defined formats.

If you have a choice in the matter, using JSON is probably a better way to go. But again, it depends on what you're doing what makes the most sense.

As it relates to Go's JSON unmarshaler, the JSON unmarshaler will call a type's UnmarshalJSON method, if it's defined, and fall back to UnmarshalText if that is defined.

If you know you'll be using JSON, you should absolutely define an UnmarshalJSON function.

You would generally create an UnmarshalText only if you expected it to be used in non-JSON contexts, with the added benefit that the JSON unmarshaler would also use it, without having to duplicate it (if indeed the same implementation would work for JSON).

Per the documentation :

To unmarshal JSON into a value implementing the Unmarshaler interface, Unmarshal calls that value's UnmarshalJSON method, including when the input is a JSON null. Otherwise, if the value implements encoding.TextUnmarshaler and the input is a JSON quoted string, Unmarshal calls that value's UnmarshalText method with the unquoted form of the string.

Meaning: if you want to take some JSON and unmarshal it with some custom logic, you would use UnmarshalJSON . If you want to take the text in a string field of a JSON document and decode that in some special way (ie parse it rather than just write it into a string -typed field), you would use UnmarshalText . For example, net.IP implements UnmarshalText so that you can provide a string value like "ipAddress": "1.2.3.4" and unmarshal it into a net.IP field. If net.IP did not implement UnmarshalText , you would only be able to unmarshal the JSON representation of the underlying type ( []byte ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM