golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.43k stars 17.71k forks source link

encoding/json: json.Valid([]byte("1")) should be false #66739

Closed shuqingzai closed 7 months ago

shuqingzai commented 7 months ago

Go version

go version go1.21.9 darwin/amd64

Output of go env in your module/workspace:

-> % go env
GO111MODULE='on'
GOARCH='amd64'
GOBIN='/Users/shuqingzai/Code/go/bin'
GOCACHE='/Users/shuqingzai/Library/Caches/go-build'
GOENV='/Users/shuqingzai/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/shuqingzai/Code/go/pkg/mod'
GONOPROXY='github.com/easy-mall,git.maiyuan.online'
GONOSUMDB='github.com/easy-mall,git.maiyuan.online'
GOOS='darwin'
GOPATH='/Users/shuqingzai/Code/go'
GOPRIVATE='github.com/easy-mall,git.maiyuan.online'
GOPROXY='https://goproxy.cn,https://goproxy.io,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.9'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/shuqingzai/Code/go-project/maiyuan/scheduler-goframe/go.mod'
GOWORK='/Users/shuqingzai/Code/go-project/maiyuan/scheduler-goframe/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/c6/3tgt7cxj7q9b8_tg45jr3c300000gp/T/go-build1393534880=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    strs := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "a", "b", "A", "B"}
    for _, str := range strs {
        fmt.Println(json.Valid([]byte(str)))
    }
}

What did you see happen?

See: https://go.dev/play/p/68tuuGoocva?v=goprev

What did you expect to see?

Numeric strings should not validate

Rican7 commented 7 months ago

Why? The number 1 is a valid JSON value.

Numeric strings should not validate

That's not what you're testing, but even still a string (numeric or otherwise) is also a valid JSON value.

seankhliao commented 7 months ago

it's a valid json value