diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 3c20e09..fab89b4 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -33,6 +33,8 @@ type Generator struct { StubsOnly bool LeaveTemps bool NoFormat bool + + SimpleBytes bool } // writeStub outputs an initial stubs for marshalers/unmarshalers so that the package @@ -120,6 +122,9 @@ func (g *Generator) writeMain() (path string, err error) { if g.NoStdMarshalers { fmt.Fprintln(f, " g.NoStdMarshalers()") } + 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 1cd30bb..02d17f8 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 byte inseat base64 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") @@ -72,6 +73,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 021933a..2d87794 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -51,7 +51,7 @@ var primitiveStringDecoders = map[reflect.Kind]string{ } var customDecoders = map[string]string{ - "json.Number": "in.JsonNumber()", + "json.Number": "in.JsonNumber()", } // genTypeDecoder generates decoding code for the type t, but uses unmarshaler interface if implemented by t. @@ -88,7 +88,7 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags fieldTags, indent int) error { ws := strings.Repeat(" ", indent) // Check whether type is primitive, needs to be done after interface check. - if dec := customDecoders[t.String()]; dec != "" { + if dec := customDecoders[t.String()]; dec != "" { fmt.Fprintln(g.out, ws+out+" = "+dec) return nil } else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString { @@ -109,7 +109,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 48cba15..ce3ec31 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -128,7 +128,12 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT vVar := g.uniqueVarName() if t.Elem().Kind() == reflect.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 {") @@ -157,7 +162,11 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT iVar := g.uniqueVarName() if t.Elem().Kind() == reflect.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 eb0d70b..c83e03b 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -35,6 +35,7 @@ type Generator struct { noStdMarshalers bool omitEmpty bool + simpleBytes bool fieldNamer FieldNamer // package path to local alias map for tracking imports @@ -115,6 +116,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] {