瀏覽代碼

jsonpb: Introduce Marshaler.EnumsAsInts.

If set, this overrides EnumsAsStrings, and forces encoding enum values
as their numeric values.
David Symonds 10 年之前
父節點
當前提交
0ea3c033ce
共有 2 個文件被更改,包括 8 次插入4 次删除
  1. 7 3
      jsonpb/jsonpb.go
  2. 1 1
      jsonpb/jsonpb_test.go

+ 7 - 3
jsonpb/jsonpb.go

@@ -35,7 +35,7 @@ protocol buffer and JSON objects.
 
 Compared to encoding/json, this library:
  - encodes int64, uint64 as strings
- - optionally encodes enums as strings
+ - optionally encodes enums as integers
 */
 package jsonpb
 
@@ -59,7 +59,11 @@ var (
 // Marshaler is a configurable object for converting between
 // protocol buffer objects and a JSON representation for them
 type Marshaler struct {
+	// Whether to render enum values as integers, as opposed to string values.
+	EnumsAsInts bool
+
 	// Use string values for enums (as opposed to integer values)
+	// This is DEPRECATED and will become the default.
 	EnumsAsString bool
 
 	// A string to indent each level by. The presence of this field will
@@ -188,9 +192,9 @@ func (m *Marshaler) marshalValue(out *errWriter, v reflect.Value,
 
 	// Handle enumerations.
 	protoInfo := structField.Tag.Get("protobuf")
-	if m.EnumsAsString && strings.Contains(protoInfo, ",enum=") {
+	if m.EnumsAsString && !m.EnumsAsInts && strings.Contains(protoInfo, ",enum=") {
 		// Unknown enum values will are stringified by the proto library as their
-		// value. Such values should _not_ be quoted or they will be intrepreted
+		// value. Such values should _not_ be quoted or they will be interpreted
 		// as an enum string instead of their value.
 		enumStr := v.Interface().(fmt.Stringer).String()
 		var valStr string

+ 1 - 1
jsonpb/jsonpb_test.go

@@ -271,7 +271,7 @@ var marshalingTests = []struct {
 	{"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
 	{"enum-string flat object", Marshaler{EnumsAsString: true},
 		&pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
-	{"enum-value pretty object", Marshaler{Indent: " "},
+	{"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
 		&pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
 	{"unknown enum value object", marshalerAllOptions,
 		&pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},