caltechlibrary/datatools

json encoding, decoding is doing odd things

rsdoiel opened this issue · 1 comments

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
}

Fixed in release 1.2.4.