Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func genUnmarshalJSON(mtyp *marshalerType) Function {
input = Name(m.scope.newIdent("input"))
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name()))
dec = Name(m.scope.newIdent("dec"))
json = Name(m.scope.parent.packageName("encoding/json"))
json = Name("jsonIter")
)
fn := Function{
Receiver: recv,
Expand Down Expand Up @@ -80,7 +80,7 @@ func genMarshalJSON(mtyp *marshalerType) Function {
recv = m.receiver()
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name()))
enc = Name(m.scope.newIdent("enc"))
json = Name(m.scope.parent.packageName("encoding/json"))
json = Name("jsonIter")
)
fn := Function{
Receiver: recv,
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ import (
"io"
"os"
"reflect"
"slices"
"strings"

"github.com/garslo/gogen"
Expand Down Expand Up @@ -298,6 +299,12 @@ func generate(mtyp *marshalerType, cfg *Config) ([]byte, error) {
fmt.Fprintln(w)
mtyp.scope.writeImportDecl(w)
fmt.Fprintln(w)

if slices.Contains(cfg.Formats, "json") {
fmt.Fprintln(w, "var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary")
fmt.Fprintln(w)
}

if mtyp.override != nil {
writeUseOfOverride(w, mtyp.override, mtyp.scope.qualify)
}
Expand Down Expand Up @@ -360,7 +367,7 @@ func newMarshalerType(fs *token.FileSet, imp types.Importer, typ *types.Named) *
mtyp.scope.addReferences(styp)

// Add packages which are always needed.
mtyp.scope.addImport("encoding/json")
mtyp.scope.addImport("github.com/json-iterator/go")
mtyp.scope.addImport("errors")

for i := 0; i < styp.NumFields(); i++ {
Expand Down
17 changes: 17 additions & 0 deletions types_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"fmt"
"go/types"
"io"
"os"
"sort"
"strconv"

"golang.org/x/tools/go/packages"
)

// walkNamedTypes runs the callback for all named types contained in the given type.
Expand Down Expand Up @@ -215,6 +218,20 @@ func (s *fileScope) writeImportDecl(w io.Writer) {
// addImport loads a package and adds it to the import set.
func (s *fileScope) addImport(path string) {
pkg, err := s.imp.Import(path)
if err != nil {
// Fallback to module-aware importer via go/packages
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedImports | packages.NeedDeps,
// Preserve build environment so that go/packages can resolve modules.
Env: os.Environ(),
Tests: false,
}
pkgs, perr := packages.Load(cfg, path)
if perr == nil && len(pkgs) > 0 && pkgs[0].Types != nil {
pkg = pkgs[0].Types
err = nil
}
}
if err != nil {
panic(fmt.Errorf("can't import %q: %v", path, err))
}
Expand Down