Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json.Rawmessage decodes null as "null" #40495

Closed
ohait opened this issue Jul 30, 2020 · 5 comments
Closed

json.Rawmessage decodes null as "null" #40495

ohait opened this issue Jul 30, 2020 · 5 comments

Comments

@ohait
Copy link

ohait commented Jul 30, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.1 linux/amd64

Does this issue reproduce with the latest release?

i can reproduce on go version go1.14 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/oha/.cache/go-build"
GOENV="/home/oha/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/oha/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/oha/sdk/go1.13.1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/oha/sdk/go1.13.1/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build838880304=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
        "encoding/json"
        "testing"
)

type Frame struct {
        Data json.RawMessage `json:"data"`
}

func TestJson(t *testing.T) {
        f := Frame{Data: nil}
        t.Logf("%#v (%q)", f, f.Data)

        j, _ := json.Marshal(f)
        t.Logf("json: %s", j)

        var f2 Frame
        json.Unmarshal(j, &f2)
        t.Logf("%#v (%q)", f2, f2.Data)
}

What did you expect to see?

f.Data and f2.Data should be the same (nil)

What did you see instead?

=== RUN   TestJson
--- PASS: TestJson (0.00s)
    json_test.go:14: main.Frame{Data:json.RawMessage(nil)} ("")
    json_test.go:17: json: {"data":null}
    json_test.go:21: main.Frame{Data:json.RawMessage{0x6e, 0x75, 0x6c, 0x6c}} ("null")
PASS

json.Unmarshal decode null as "null"

@ohait
Copy link
Author

ohait commented Jul 30, 2020

note, if json:"data,omitempty" than everything works as expected

@DisposaBoy
Copy link

I think you're just seeing the result of using the %q format which quotes things. Maybe you meant to use %s.

@ohait
Copy link
Author

ohait commented Jul 30, 2020

i included also the %#v which shows how json.RawMessage(nil) is turned into json.RawMessage{0x6e, 0x75, 0x6c, 0x6c} which are the ascii of null

@seankhliao
Copy link
Member

I believe this is working as intended, json.RawMessage specifically means not to parse the data and store it as is in json

@ohait
Copy link
Author

ohait commented Jul 30, 2020

I believe this is working as intended, json.RawMessage specifically means not to parse the data and store it as is in json

oh yes, i see it now. if further Unmarshalled, it returns nil.

@ohait ohait closed this as completed Jul 30, 2020
@golang golang locked and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants