Skip to content

Not compatible with stdlib for nil maps and slices #56

@thockin

Description

@thockin
package main                                                                                                        

import (
    "encoding/json"
    "fmt"
    "reflect"

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

type Ttest map[string]string

func main() {
    fn := func(name string, marshal func(interface{}) ([]byte, error), obj interface{}) []byte {
        jb, err := marshal(obj)
        if err != nil {
            fmt.Printf("%s failed to marshal:\n  error: %v\n", name, err)
            return nil 
        }   
        return jb
    }   
    var obj1 Ttest
    jb1 := fn("json", json.Marshal, &obj1)
    var obj2 Ttest
    jb2 := fn("jsoniter", jsoniter.Marshal, &obj2)
    if !reflect.DeepEqual(jb1, jb2) {
        fmt.Printf("results differ:\n  expected: %q\n       got: %q\n", string(jb1), string(jb2))
    }   
}

yields:

results differ:
  expected: "null"
       got: "{}"

Changing map to slice also produces differences:

results differ:
  expected: "null"
       got: "[]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions