aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/xml/read.go
diff options
context:
space:
mode:
authorLeigh McCulloch <leighmcc@gmail.com>2018-12-11 05:32:50 +0000
committerIan Lance Taylor <iant@golang.org>2018-12-12 14:18:06 +0000
commitd70b0ece2f208ff99efce87c07f5171617c29a13 (patch)
tree08c57d1c0607e87b76a25fcdf124f4abfc20c6a4 /src/encoding/xml/read.go
parent321a3d6e85e6440b5c753b3fd6a0e028dcc08143 (diff)
downloadgo-d70b0ece2f208ff99efce87c07f5171617c29a13.tar.gz
go-d70b0ece2f208ff99efce87c07f5171617c29a13.zip
encoding/xml, encoding/json: docs and examples using custom marshalers
Both the encoding/xml and encoding/json packages support custom marshalers for JSON and XML, as well as the basic encoding.TextMarshaler and encoding.TextUnmarshaler interfaces, but the docs and examples for these are missing. There are docs for how to use encoding.TextMarshaler and encoding.TextUnmarshaler in encoding/json, but not encoding/xml. There are no examples for how to use them with either json or xml. This commit includes docs for encoding/xml and examples for both encoding/json and encoding/xml. There is an example using custom marshalers MarshalJSON and UnmarshalJSON in encoding/json, but not MarshalXML and UnmarshalXML in encoding/json. These docs are more so necessary for encoding/xml because the complexities of XML documents is significantly greater than JSON documents which more often leads to the need for custom marshaling. The encoding/json package includes an example of how to write a custom marshaler, and this commit includes the same example for the xml package. All examples are mirrored off the existing custom marshaler example in encoding/json. Fixes #6859 Change-Id: Ic93abc27c0b4d5e48dea6ede4e20b1bedca4ab39 Reviewed-on: https://go-review.googlesource.com/c/76350 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/encoding/xml/read.go')
-rw-r--r--src/encoding/xml/read.go5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/encoding/xml/read.go b/src/encoding/xml/read.go
index 36c7ba6311..12102bc804 100644
--- a/src/encoding/xml/read.go
+++ b/src/encoding/xml/read.go
@@ -92,6 +92,11 @@ import (
//
// * A struct field with tag "-" is never unmarshaled into.
//
+// If Unmarshal encounters a field type that implements the Unmarshaler
+// interface, Unmarshal calls its UnmarshalXML method to produce the value from
+// the XML element. Otherwise, if the value implements
+// encoding.TextUnmarshaler, Unmarshal calls that value's UnmarshalText method.
+//
// Unmarshal maps an XML element to a string or []byte by saving the
// concatenation of that element's character data in the string or
// []byte. The saved []byte is never nil.