diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-03-02 21:16:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-02 21:16:59 +0800 |
commit | 213b8f9af41aee0f0888818bceb500374409733d (patch) | |
tree | ee3823716e9d54aed875fd6d4c2ba43c5bd6dacd /common/hexutil/json_example_test.go | |
parent | 9184249b393e4e332ae6a2f5d774880a88a9bfd6 (diff) | |
parent | 280f08be84325924ea7628a8187fd84e7cc39145 (diff) | |
download | go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar.gz go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar.bz2 go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar.lz go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar.xz go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.tar.zst go-tangerine-213b8f9af41aee0f0888818bceb500374409733d.zip |
Merge pull request #3722 from fjl/hexutil-text-unmarshal
common/hexutil: implement TextMarshaler, TextUnmarshaler
Diffstat (limited to 'common/hexutil/json_example_test.go')
-rw-r--r-- | common/hexutil/json_example_test.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/common/hexutil/json_example_test.go b/common/hexutil/json_example_test.go new file mode 100644 index 000000000..80180d918 --- /dev/null +++ b/common/hexutil/json_example_test.go @@ -0,0 +1,45 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package hexutil_test + +import ( + "encoding/json" + "fmt" + + "github.com/ethereum/go-ethereum/common/hexutil" +) + +type MyType [5]byte + +func (v *MyType) UnmarshalText(input []byte) error { + return hexutil.UnmarshalFixedText("MyType", input, v[:]) +} + +func (v MyType) String() string { + return hexutil.Bytes(v[:]).String() +} + +func ExampleUnmarshalFixedText() { + var v1, v2 MyType + fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1)) + fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2)) + fmt.Println("v2:", v2) + // Output: + // v1 error: hex string has length 2, want 10 for MyType + // v2 error: <nil> + // v2: 0x0101010101 +} |