summaryrefslogtreecommitdiff
path: root/src/encoding/json/decode.go
Commit message (Collapse)AuthorAgeFilesLines
* Revert "encoding/json: reduce unmarshal mallocs for unmapped fields"Brad Fitzpatrick2017-06-291-28/+34
| | | | | | | | | | | | | | | | This reverts commit df68afd07ce67727 (https://golang.org/cl/33276) Reason for revert: made other benchmarks worse Fixes #20693 (details) Updates #17914 Updates #10335 Change-Id: If451b620803ccb0536b89c76c4353d2185d57d7e Reviewed-on: https://go-review.googlesource.com/47211 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: clarify unmarshaling behaviour on bad fieldsAlberto Donizetti2017-06-051-1/+3
| | | | | | | | Fixes #19526 Change-Id: Ifaaf454e0e89fdf4309118c2e2e6ac0d0a43c39d Reviewed-on: https://go-review.googlesource.com/44711 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* encoding/json: reduce unmarshal mallocs for unmapped fieldsPascal S. de Kloe2017-03-201-34/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | JSON decoding performs poorly for unmapped and ignored fields. We noticed better performance when unmarshalling unused fields. The loss comes mostly from calls to scanner.error as described at #17914. benchmark old ns/op new ns/op delta BenchmarkIssue10335-8 431 408 -5.34% BenchmarkUnmapped-8 1744 1314 -24.66% benchmark old allocs new allocs delta BenchmarkIssue10335-8 4 3 -25.00% BenchmarkUnmapped-8 18 4 -77.78% benchmark old bytes new bytes delta BenchmarkIssue10335-8 320 312 -2.50% BenchmarkUnmapped-8 568 344 -39.44% Fixes #17914, improves #10335 Change-Id: I7d4258a94eb287c0fe49e7334795209b90434cd0 Reviewed-on: https://go-review.googlesource.com/33276 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* encoding/json: clarify documention for Unmarshal into a pointer.Tuo Shan2017-02-101-1/+2
| | | | | | | | Fixes #18730. Change-Id: If3ef28e62f7e449d4c8dc1dfd78f7d6f5a87ed26 Reviewed-on: https://go-review.googlesource.com/36478 Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: fix bad formatting introduced in CL 20356Russ Cox2016-10-241-2/+2
| | | | | | | Change-Id: I39a8b543e472e5ec5d4807a9b7f61657465c5ce5 Reviewed-on: https://go-review.googlesource.com/31816 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* encoding/json: fix decoding of null into Unmarshaler, TextUnmarshalerRuss Cox2016-10-171-5/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 1. Define behavior for Unmarshal of JSON null into Unmarshaler and TextUnmarshaler. Specifically, an Unmarshaler will be given the literal null and can decide what to do (because otherwise json.RawMessage is impossible to implement), and a TextUnmarshaler will be skipped over (because there is no text to unmarshal), like most other inappropriate types. Document this in Unmarshal, with a reminder in UnmarshalJSON about handling null. 2. Test all this. 3. Fix the TextUnmarshaler case, which was returning an unmarshalling error, to match the definition. 4. Fix the error that had been used for the TextUnmarshaler, since it was claiming that there was a JSON string when in fact the problem was NOT having a string. 5. Adjust time.Time and big.Int's UnmarshalJSON to ignore null, as is conventional. Fixes #9037. Change-Id: If78350414eb8dda712867dc8f4ca35a9db041b0c Reviewed-on: https://go-review.googlesource.com/30944 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* encoding/json: handle misspelled JSON literals in ,stringRuss Cox2016-10-131-1/+13
| | | | | | | | | | Fixes #15146. Change-Id: I229611b9cc995a1391681c492c4d742195c787ea Reviewed-on: https://go-review.googlesource.com/30943 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* encoding/json: add struct and field name to UnmarshalTypeError messageJirka Daněk2016-10-051-27/+55
| | | | | | | | | | | | | | The UnmarshalTypeError has two new fields Struct and Field, used when constructing the error message. Fixes #6716. Change-Id: I67da171480a9491960b3ae81893770644180f848 Reviewed-on: https://go-review.googlesource.com/18692 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* encoding/json: support maps with integer keysCaleb Spare2016-05-101-13/+39
| | | | | | | | | | | | | | | | | | | | This change makes encoding and decoding support integer types in map keys, converting to/from JSON string keys. JSON object keys are still sorted lexically, even though the keys may be integer strings. For backwards-compatibility, the existing Text(Un)Marshaler support for map keys (added in CL 20356) does not take precedence over the default encoding for string types. There is no such concern for integer types, so integer map key encoding is only used as a fallback if the map key type is not a Text(Un)Marshaler. Fixes #12529. Change-Id: I7e68c34f9cd19704b1d233a9862da15fabf0908a Reviewed-on: https://go-review.googlesource.com/22060 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* encoding/json: update docs to not use misuse the term "object"Brad Fitzpatrick2016-04-161-1/+1
| | | | | | | | | | | | | In JSON terminology, "object" is a collect of key/value pairs. But a JSON object is only one type of JSON value (others are string, number, array, true, false, null). This updates the Go docs (at least the public godoc) to not use "object" when we mean any JSON value. Change-Id: Ieb1c456c703693714d63d9d09d306f4d9e8f4597 Reviewed-on: https://go-review.googlesource.com/22003 Reviewed-by: Andrew Gerrand <adg@golang.org>
* encoding/json: allow non-string type keys for (un-)marshalAugusto Roman2016-04-051-8/+24
| | | | | | | | | | | | | | | | This CL allows JSON-encoding & -decoding maps whose keys are types that implement encoding.TextMarshaler / TextUnmarshaler. During encode, the map keys are marshaled upfront so that they can be sorted. Fixes #12146 Change-Id: I43809750a7ad82a3603662f095c7baf75fd172da Reviewed-on: https://go-review.googlesource.com/20356 Run-TryBot: Caleb Spare <cespare@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* all: single space after period.Brad Fitzpatrick2016-03-021-5/+5
| | | | | | | | | | | | | | | | | | | | The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* encoding/json: doc: Decode only writes to exported fieldsIan Lance Taylor2016-01-081-0/+1
| | | | | | | | Fixes #13867. Change-Id: I6c0a6c64369681840df60f63036c2eece27de8b8 Reviewed-on: https://go-review.googlesource.com/18394 Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: streamline, unexport valid Number checkingRuss Cox2015-12-041-89/+31
| | | | | | | | | | Followup to CL 12250. For #10281. Change-Id: If25d9cac92f10327bb355f2d11b00c625b464661 Reviewed-on: https://go-review.googlesource.com/17199 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* encoding/json: revise docs for Unmarshal into map, slice, arrayRuss Cox2015-12-031-5/+15
| | | | | | | | Fixes #12972. Change-Id: Id0611667e6149753c351c0c5e01211340a87d3fd Reviewed-on: https://go-review.googlesource.com/17230 Reviewed-by: David Crawshaw <crawshaw@golang.org>
* encoding/json: check if Number is validErik Dubbelboer2015-11-251-0/+121
| | | | | | | | | | json.Number is a special case which didn't have any checks and could result in invalid JSON. Fixes #10281 Change-Id: Ie3e726e4d6bf6a6aba535d36f6107013ceac913a Reviewed-on: https://go-review.googlesource.com/12250 Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: use reflect.SetBytes when decoding bytesHåvard Haugen2015-11-141-1/+1
| | | | | | | | | | | | | This allows slices of custom types with byte as underlying type to be decoded, fixing a regression introduced in CL 9371. Fixes #12921. Change-Id: I62a715eaeaaa912b6bc599e94f9981a9ba5cb242 Reviewed-on: https://go-review.googlesource.com/16303 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* encoding/json: spell "marshaling" and "unmarshaling" consistentlyAndrew Gerrand2015-09-231-1/+1
| | | | | | | | | Fixes #12431 Change-Id: I67c42bf2cd9285f471387248fd9c22a16b158349 Reviewed-on: https://go-review.googlesource.com/14150 Reviewed-by: Dmitri Shuralyov <shurcool@gmail.com> Reviewed-by: Rob Pike <r@golang.org>
* encoding/json: scanner: use byte, more consistentMarvin Stenger2015-09-211-1/+1
| | | | | | | | | | | | | The fields step and redoState of struct scanner are now defined as `func(s *scanner, c byte) int` instead of `func(s *scanner, c int) int`, since bytes are sufficient. Further changes improve the consistency in the scanner.go file. Change-Id: Ifb85f2130d728d2b936d79914d87a1f0b5c6ee7d Reviewed-on: https://go-review.googlesource.com/14801 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* encoding/json: revert "fix decoding of JSON null values"Russ Cox2015-07-301-1/+1
| | | | | | | | | | | | Fixes #11912. Fixes #11937. This reverts commit 1a99ba55df902a2657d1ccfc52a60024c22dba98. Change-Id: I32b76053fdabc59f28ca5bedf1b15c0baa8afae1 Reviewed-on: https://go-review.googlesource.com/12893 Reviewed-by: Didier Spezia <didier.06@gmail.com> Reviewed-by: David Crawshaw <crawshaw@golang.org>
* encoding/json: fix decoding of JSON null valuesDidier Spezia2015-07-221-1/+1
| | | | | | | | | | | | | | | | | | | | | JSON decoding currently fails for null values bound to any type which does implement the JSON Unmarshaler interface without checking for null values (such as time.Time). It also fails for types implementing the TextUnmarshaler interface. The expected behavior of the JSON decoding engine in such case is to process null by keeping the value unchanged without producing any error. Make sure null values are handled by the decoding engine itself, and never passed to the UnmarshalText or UnmarshalJSON methods. Fixes #9037 Change-Id: I261d85587ba543ef6f1815555b2af9311034d5bb Reviewed-on: https://go-review.googlesource.com/9376 Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: document and test overwrite of slice, map during UnmarshalRuss Cox2015-07-151-0/+7
| | | | | | | | Fixes #8837. Change-Id: Iaaecbb0b324004cb74b16b764126b01315e6a16e Reviewed-on: https://go-review.googlesource.com/12209 Reviewed-by: Andrew Gerrand <adg@golang.org>
* encoding/json: fix out of phase error unmarshaling non-string into ↵Russ Cox2015-07-151-0/+1
| | | | | | | | | | TextUnmarshaler Fixes #9650. Change-Id: I45b879124691e485b86c1e99a3227032283850d2 Reviewed-on: https://go-review.googlesource.com/12208 Reviewed-by: Andrew Gerrand <adg@golang.org>
* encoding/json: fix decoding of types with '[]byte' as underlying typeHåvard Haugen2015-05-151-1/+1
| | | | | | | | | | | | | | | | | | All slice types which have elements of kind reflect.Uint8 are marshalled into base64 for compactness. When decoding such data into a custom type based on []byte the decoder checked the slice kind instead of the slice element kind, so no appropriate decoder was found. Fixed by letting the decoder check slice element kind like the encoder. This guarantees that already encoded data can still be successfully decoded. Fixes #8962. Change-Id: Ia320d4dc2c6e9e5fe6d8dc15788c81da23d20c4f Reviewed-on: https://go-review.googlesource.com/9371 Reviewed-by: Peter Waldschmidt <peter@waldschmidt.com> Reviewed-by: Russ Cox <rsc@golang.org>
* encoding/json: add UnmarshalTypeError.OffsetAlex Plugaru2015-01-291-19/+20
| | | | | | | | | Fixes #9693 Change-Id: Ibf07199729bfc883b2a7e051cafd98185f912acd Reviewed-on: https://go-review.googlesource.com/3283 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* encoding/json: address go vet reportsEmil Hessman2014-12-281-1/+1
| | | | | | | | | | | | | | | | | | The error message for decoding a unquoted value into a struct field with the ,string option specified has two arguments when one is needed. Make the error message take one argument and add a test in order to cover the case when a unquoted value is specified. Also add error value as the missing argument for Fatalf call in test. Fixes the following go vet reports: decode.go:602: wrong number of args for format in Errorf call: 1 needed but 2 args decode_test.go:1088: missing argument for Fatalf("%v"): format reads arg 1, have only 0 args Change-Id: Id036e10c54c4a7c1ee9952f6910858ecc2b84134 Reviewed-on: https://go-review.googlesource.com/2109 Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
* encoding/json: fix handling of null with ,string fieldsRuss Cox2014-10-071-4/+36
| | | | | | | | | Fixes #8587. LGTM=bradfitz R=golang-codereviews, bradfitz CC=golang-codereviews, iant, r https://golang.org/cl/152270044
* encoding/json: don't panic on incorrect map argumentRobert Griesemer2014-10-011-2/+4
| | | | | | | | | Fixes #8305. LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/145680044
* build: move package sources from src/pkg to srcRuss Cox2014-09-081-0/+1050
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.