ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

decoding json null as a string fails #182

Closed seanchann closed 7 years ago

seanchann commented 8 years ago

hi: add printf in code:

   func (d *jsonDecDriver) appendStringAsBytes() {
        if d.tok == 0 {
        var b byte
        r := d.r
        for b = r.readn1(); jsonIsWS(b); b = r.readn1() {
        }
        d.tok = b
    }
    if d.tok != '"' {
        fmt.Printf("got error . d %+v \r\n d.d:%+v  d.h:%+v d.r:%+v"+
            "d.b:%v d.bs:%v\r\n"+
            "d.d.raw:%v d.d.b:%v d.d.rb.b:%v\r\n"+
            "d.r.b:\r\n",
            *d, *d.d, *d.h, d.r,
            string(d.b[:]), string(d.bs),
            string(d.d.raw()), string(d.d.b[:]), string(d.d.rb.b))
        d.d.errorf("json: expect char '%c' but got char '%c'", '"', d.tok)
    }
    d.tok = 0

    v := d.bs[:0] 
   }
seanchann commented 8 years ago

I got error print:

error.txt

ugorji commented 8 years ago

I have zero idea what you are trying to do here, or what your point is.

First of all - what is the bug? You need to put a reproducer that I can run with "go run main.go" or "go test", then tell me what you expect to get, and then tell me what you receive. This way, I can reproduce your error and fix, or I can advise why what you saw is correct and close as a User Error.

seanchann commented 8 years ago

complete test code:

package main

import (
    "fmt"

    "k8s.io/kubernetes/pkg/api"
    "k8s.io/kubernetes/pkg/api/unversioned"

    "github.com/ugorji/go/codec"
)

type User struct {
    unversioned.TypeMeta `json:",inline"`
    api.ObjectMeta       `json:"metadata,omitempty"`

    Spec interface{} `json:"spec,omitempty"`
}

func main() {
    user:= &User{}
    var ss = `{"apiVersion":"v1","kind":"User","metadata":{"annotations":{"area_id":"800000","city":"深圳市","city_id":"440300","cnISP":"cnISPCTCC","country":"中国","country_id":"CN","county_id":"-1","isp_id":"100017","proto":null,"publicip":"113.86.18.48","refreshTime":"2016-11-20 08:00:16.468426102 +0000 UTC","region_id":"440000","userSpace":"develop","version":"03.00.00.69","wanip":null},"creationTimestamp":"2016-11-20T08:53:55Z","name":"78a351157a5c","uid":"de87bf5a-aefe-11e6-ac40-00155d3d7203"},"spec":{"detailInfo":{"description":"78:A3:51:15:7A:5C","downloadTraffic":2.79152799e+08,"email":"78a351157a5c@gmail.com","enableOTA":true,"expireTime":"2017-10-19T17:11:29Z","id":108,"managePasswd":"ca709d5580bc7ec8327133234424e982c74","name":"78a351157a5c","passwd":"95067bef91746c3a390f90bdf3445d6a7e9","regIPAddr":"54.191.184.140","regTime":"2016-11-20T08:52:12Z","status":true,"trafficLimit":5.36870912e+10,"uploadTraffic":6.911085e+06},"userService":{"nodes":{"06df9e50cdd7":{"host":"199.203.99.67","user":{"downloadTraffic":2.6743306e+07,"enableOTA":true,"id":108,"method":"aes-256-cfb","name":"78a351157a5c","password":"95067bef91746c3a3904552340bd10d6a7e9","port":33185,"uploadTraffic":636262}},"faadf65a6352":{"host":"104.206.61.91","user":{"downloadTraffic":3.1574614e+07,"enableOTA":true,"id":108,"method":"aes-256-cfb","name":"78a351157a5c","password":"95067bef9174ldkajf23390f90bd10d6a7e9","port":40674,"uploadTraffic":758111}}}}}}`

    var s = []byte(ss)
    var h codec.JsonHandle
    err := codec.NewDecoderBytes(s, &h).Decode(user)
    fmt.Printf("err:%v User is now: %v of type: %T\n", err, user, user)
}
ugorji commented 8 years ago

Please give me a standalone reproducer that didn't include me downloading and third-party packages, then state output you get and West you expect. Thanks.

seanchann commented 8 years ago
package main

import (
    "fmt"

    "github.com/ugorji/go/codec"
)

type TypeMeta struct {
    Kind       string `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"`
    APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,2,opt,name=apiVersion"`
}

type ObjectMeta struct {
    Name                       string            `json:"name,omitempty"`
    GenerateName               string            `json:"generateName,omitempty"`
    Namespace                  string            `json:"namespace,omitempty"`
    SelfLink                   string            `json:"selfLink,omitempty"`
    UID                        interface{}       `json:"uid,omitempty"`
    ResourceVersion            string            `json:"resourceVersion,omitempty"`
    Generation                 int64             `json:"generation,omitempty"`
    CreationTimestamp          interface{}       `json:"creationTimestamp,omitempty"`
    DeletionTimestamp          interface{}       `json:"deletionTimestamp,omitempty"`
    DeletionGracePeriodSeconds *int64            `json:"deletionGracePeriodSeconds,omitempty"`
    Labels                     map[string]string `json:"labels,omitempty"`
    Annotations                map[string]string `json:"annotations,omitempty"`
    OwnerReferences            interface{}       `json:"ownerReferences,omitempty"`
    Finalizers                 []string          `json:"finalizers,omitempty"`
    ClusterName                string            `json:"clusterName,omitempty"`
}

type User struct {
    TypeMeta   `json:",inline"`
    ObjectMeta `json:"metadata,omitempty"`

    Spec interface{} `json:"spec,omitempty"`
}

func main() {
    user := &User{}
    var ss = `{"apiVersion":"v1","kind":"User","metadata":{"annotations":{"area_id":"800000","city":"深圳市","city_id":"440300","cnISP":"cnISPCTCC","country":"中国","country_id":"CN","county_id":"-1","isp_id":"100017","proto":null,"publicip":"113.86.18.48","refreshTime":"2016-11-20 08:00:16.468426102 +0000 UTC","region_id":"440000","userSpace":"develop","version":"03.00.00.69","wanip":null},"creationTimestamp":"2016-11-20T08:53:55Z","name":"78a351157a5c","uid":"de87bf5a-aefe-11e6-ac40-00155d3d7203"},"spec":{"detailInfo":{"description":"78:A3:51:15:7A:5C","downloadTraffic":2.79152799e+08,"email":"78a351157a5c@gmail.com","enableOTA":true,"expireTime":"2017-10-19T17:11:29Z","id":108,"managePasswd":"ca709d5580bc7ec8327133234424e982c74","name":"78a351157a5c","passwd":"95067bef91746c3a390f90bdf3445d6a7e9","regIPAddr":"54.191.184.140","regTime":"2016-11-20T08:52:12Z","status":true,"trafficLimit":5.36870912e+10,"uploadTraffic":6.911085e+06},"userService":{"nodes":{"06df9e50cdd7":{"host":"199.203.99.67","user":{"downloadTraffic":2.6743306e+07,"enableOTA":true,"id":108,"method":"aes-256-cfb","name":"78a351157a5c","password":"95067bef91746c3a3904552340bd10d6a7e9","port":33185,"uploadTraffic":636262}},"faadf65a6352":{"host":"104.206.61.91","user":{"downloadTraffic":3.1574614e+07,"enableOTA":true,"id":108,"method":"aes-256-cfb","name":"78a351157a5c","password":"95067bef9174ldkajf23390f90bd10d6a7e9","port":40674,"uploadTraffic":758111}}}}}}`

    var s = []byte(ss)
    var h codec.JsonHandle
    err := codec.NewDecoderBytes(s, &h).Decode(user)
    fmt.Printf("err:%v User is now: %v of type: %T\n", err, user, user)
}