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

[]byte marshaled to null #272

Closed
ash2k opened this issue May 6, 2018 · 6 comments · Fixed by #371
Closed

[]byte marshaled to null #272

ash2k opened this issue May 6, 2018 · 6 comments · Fixed by #371

Comments

@ash2k
Copy link
Contributor

ash2k commented May 6, 2018

One of the fuzz tests in Kubernetes caught an issue:
This library:

{"metadata":{"selfLink":"Ǭ`/F苫ŽȢ噯榋C","resourceVersion":"4597642297830476577"},"items":[{"metadata":{"name":"玿j情1 蔝","generateName":"萀枦đ ","uid":".愥+ǽķ蓧u岏V","resourceVersion":"13323996552230461162","creationTimestamp":null},"data":{"輣 兀zʩL:Ģ梸ïĭ":"MŞFƤ趻Ă癆萃猷S幸4"},"binaryData":{"Ȟ櫤薯WDZĥȂ":null}}]}

encoding/json:

{"metadata":{"selfLink":"Ǭ`/F苫ŽȢ噯榋C","resourceVersion":"4597642297830476577"},"items":[{"metadata":{"name":"玿j情1 蔝","generateName":"萀枦đ ","uid":".愥+ǽķ蓧u岏V","resourceVersion":"13323996552230461162","creationTimestamp":null},"data":{"輣 兀zʩL:Ģ梸ïĭ":"MŞFƤ趻Ă癆萃猷S幸4"},"binaryData":{"Ȟ櫤薯WDZĥȂ":""}}]}

Notice how at the very end of the json the value in the map is not the same.
BinaryData field is declared like this:

BinaryData map[string][]byte `json:"binaryData,omitempty"`

https://github.com/kubernetes/kubernetes/blob/8f20a815e5ef27d4d247e42bff90e42706397ca2/staging/src/k8s.io/api/core/v1/types.go#L4742

Trying to migrate all encoding/json uses to this library in this PR kubernetes/kubernetes#63284

This is using commit 2ddf6d7

@ash2k ash2k changed the title Empty []byte marshaled to null []byte marshaled to null May 6, 2018
@panjunjie
Copy link

我也碰到这种情况了:
jsonStr:={"file":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAGUExURf///wAAAFXC034AAAABdFJOUwBA5thmAAAADElEQVR42mJgAAgwAAACAAFPbVnhAAAAAElFTkSuQmCC"}
json 字符串,其中 file 属性 是 []byte 对象,使用 json-iterator 转换成对象,file 是空值:
json.Unmarshal([]byte(jsonStr), &fileStruct)

目前只能切换回自带的 "encoding/json"

@thockin
Copy link
Collaborator

thockin commented May 16, 2018

https://play.golang.org/p/nx9cyeks1ne

shows that stdlib json encodes a nil slice as "null" but decodes "" and "null" as a non-nil empty slice

https://play.golang.org/p/lOSZs9_fzAB

corroborates that []int follows the same pattern

@ash2k - in my experience, if you write the testcase for json-iterator that shows this behavioral difference, they will fix it :) The test case should be trivial. https://github.com/json-iterator/go/blob/master/type_tests/map_test.go I am not sure why it doesn't get caught already, truthfully.

@taowen
Copy link
Contributor

taowen commented May 26, 2018

sorry for the delay, the base64 decode issue has been fixed

@taowen taowen closed this as completed May 26, 2018
@stevenjohnstone
Copy link

I'm not sure this has been fixed:

package main

import (
        "encoding/json"
        "fmt"

        jsoniter "github.com/json-iterator/go"
)

var json2 = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {

        m := map[string][]byte{
                "foo": []byte{},
        }

        data2, err := json2.Marshal(m)
        if err != nil {
                panic(err)
        }
        fmt.Printf("jsoniter output       %s (%v)\n", string(data2), data2)

        data, err := json.Marshal(m)
        if err != nil {
                panic(err)
        }
        fmt.Printf("encoding/json output   %s (%v)\n", string(data), data)

}
$ go run main.go 
jsoniter output       {"foo":null} ([123 34 102 111 111 34 58 110 117 108 108 125])
encoding/json output   {"foo":""} ([123 34 102 111 111 34 58 34 34 125])

@maiamcc
Copy link

maiamcc commented May 1, 2019

I'm also seeing []byte{} being marshaled to null rather than "".

(For completeness, my example provides a nil byte array as well, which I would expect to be marshaled to "null".)

Any chance of this getting addressed?

package main

import (
	"fmt"

	jsoniter "github.com/json-iterator/go"
)

func main() {
	data := map[string][]byte{
		"stuff":  []byte{}, // expect to be marshaled to ""
		"things": nil, // expect to be marshaled to "null"
	}
	jsoner := jsoniter.Config{}.Froze()
	j, err := jsoner.Marshal(data)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(j))
}
$ go run main.go
{"things":null,"stuff":null}

@nikhita
Copy link
Contributor

nikhita commented Jun 3, 2019

Fixed in #371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants