Skip to content

json encoding, decoding is doing odd things #16

@rsdoiel

Description

@rsdoiel

Over the development of Go the json package has evolved. It used to be sensible to use Marshal()/MarshalIndent() or Unmarshal() most of the time when writing JSON processing code. This is not longer the case. In the case of decoding JSON you want to create a custom decoder so you can handle JSON Numbers sensibly. Likewise you want to use custom encoders to you can avoid characters magically turning to Unicode code point notation because someone thought HTML entities were a problem (note, they are not, they can easily be used as is based on the JSON spec). I need to review all places I use the json package to see where these two things are causing problems for our datatools cli.

A custom Unmarshal() should probably look something like

func jsonUnmarshal(src []byte, obj interface{}) error {
        dec := json.NewDecoder(bytes.NewReader(src))
        dec.UseNumber()
        err := dec.Decode(&obj)
        if err != nil && err != io.EOF {
                return err
        }
        return nil
}

An MarshalIndent should probably look like

func MarshalIndent(obj interface{}, prefix string, indent string) ([]byte, error) {
        buf := []byte{}
        w := bytes.NewBuffer(buf)
        enc := json.NewEncoder(w)
        enc.SetEscapeHTML(false)
        enc.SetIndent(prefix, indent)
        err := enc.Encode(obj)
        if err != nil {
                return nil, err
        }
        return w.Bytes(), err
}

Metadata

Metadata

Assignees

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