diff --git a/README.md b/README.md index 95997ae..c88848d 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ Usage of easyjson: generate marshaler/unmarshalers for all structs in a file -build_tags string build tags to add to generated file + -byte + use simple bytes instead of Base64Bytes for slice of bytes -leave_temps do not delete temporary files -no_std_marshalers diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 134244b..9d1c2e3 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -35,6 +35,7 @@ type Generator struct { StubsOnly bool LeaveTemps bool NoFormat bool + SimpleBytes bool } // writeStub outputs an initial stub for marshalers/unmarshalers so that the package @@ -125,6 +126,9 @@ func (g *Generator) writeMain() (path string, err error) { if g.DisallowUnknownFields { fmt.Fprintln(f, " g.DisallowUnknownFields()") } + if g.SimpleBytes { + fmt.Fprintln(f, " g.SimpleBytes()") + } sort.Strings(g.Types) for _, v := range g.Types { diff --git a/easyjson/main.go b/easyjson/main.go index d4035f7..26e95cb 100644 --- a/easyjson/main.go +++ b/easyjson/main.go @@ -22,6 +22,7 @@ var lowerCamelCase = flag.Bool("lower_camel_case", false, "use lowerCamelCase na var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON funcs") var omitEmpty = flag.Bool("omit_empty", false, "omit empty fields by default") var allStructs = flag.Bool("all", false, "generate marshaler/unmarshalers for all structs in a file") +var simpleBytes = flag.Bool("byte", false, "use simple bytes instead of Base64Bytes for slice of bytes") var leaveTemps = flag.Bool("leave_temps", false, "do not delete temporary files") var stubs = flag.Bool("stubs", false, "only generate stubs for marshaler/unmarshaler funcs") var noformat = flag.Bool("noformat", false, "do not run 'gofmt -w' on output file") @@ -74,6 +75,7 @@ func generate(fname string) (err error) { OutName: outName, StubsOnly: *stubs, NoFormat: *noformat, + SimpleBytes: *simpleBytes, } if err := g.Run(); err != nil { diff --git a/gen/decoder.go b/gen/decoder.go index 1f5919f..2a17cc7 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -129,7 +129,12 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+" in.Skip()") fmt.Fprintln(g.out, ws+" "+out+" = nil") fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()") + if g.simpleBytes { + fmt.Fprintln(g.out, ws+" "+out+" = []byte(in.String())") + } else { + fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()") + } + fmt.Fprintln(g.out, ws+"}") } else { diff --git a/gen/encoder.go b/gen/encoder.go index 6274d4f..c31f11b 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -140,7 +140,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT vVar := g.uniqueVarName() if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "uint8" { - fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")") + if g.simpleBytes { + fmt.Fprintln(g.out, ws+"out.String(string("+in+"))") + } else { + fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")") + } } else { if !assumeNonEmpty { fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilSliceAsEmpty) == 0 {") @@ -169,7 +173,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT iVar := g.uniqueVarName() if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "uint8" { - fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])") + if g.simpleBytes { + fmt.Fprintln(g.out, ws+"out.String(string("+in+"[:]))") + } else { + fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])") + } } else { fmt.Fprintln(g.out, ws+"out.RawByte('[')") fmt.Fprintln(g.out, ws+"for "+iVar+" := range "+in+" {") diff --git a/gen/generator.go b/gen/generator.go index 4a8f5a8..344e516 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -37,6 +37,7 @@ type Generator struct { omitEmpty bool disallowUnknownFields bool fieldNamer FieldNamer + simpleBytes bool // package path to local alias map for tracking imports imports map[string]string @@ -121,6 +122,11 @@ func (g *Generator) OmitEmpty() { g.omitEmpty = true } +// SimpleBytes triggers generate output bytes as slice byte +func (g *Generator) SimpleBytes() { + g.simpleBytes = true +} + // addTypes requests to generate encoding/decoding funcs for the given type. func (g *Generator) addType(t reflect.Type) { if g.typesSeen[t] {