Skip to content

Commit

Permalink
Add utf16 decoding to json dataflatten (#1389)
Browse files Browse the repository at this point in the history
Co-authored-by: seph <[email protected]>
  • Loading branch information
Micah-Kolide and directionless authored Oct 17, 2023
1 parent 59ad0a6 commit e9e8069
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/dataflatten/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ package dataflatten

import (
"encoding/json"
"errors"
"fmt"
"os"

"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)

func JsonFile(file string, opts ...FlattenOpts) ([]Row, error) {
rawdata, err := os.ReadFile(file)
if err != nil {
return nil, err
}

if json.Valid(rawdata) {
return Json(rawdata, opts...)
}

// We don't have valid json data, so try to convert possible utf16 data to utf8.
rawdata, _, err = transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder(), rawdata)
if err != nil {
return nil, errors.New("invalid json. (Despite attempted transform from utf16 to utf8")
}

return Json(rawdata, opts...)
}

Expand Down

0 comments on commit e9e8069

Please sign in to comment.