Skip to content

Commit 917e59c

Browse files
committed
fix: encloses enum values in quotes in JSON output
Ensures that enum values are properly serialized as strings within double quotes in the JSON output. This change adds quotes around enum names during serialization, resolving a formatting issue. Also adds test case for enum serialization. Closes #361.
1 parent ae0f436 commit 917e59c

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

proto-json/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<W: Write> Serializer<W> {
253253
BASE64_STANDARD.encode(v).paint(self.colors.string)
254254
)?,
255255
ReflectValueRef::Enum(d, v) => match d.value_by_number(*v) {
256-
Some(e) => write!(self.output, "{}", e.name())?,
256+
Some(e) => write!(self.output, "\"{}\"", e.name())?,
257257
None => write!(self.output, "{}", v)?,
258258
},
259259
ReflectValueRef::Message(msg) => self.write_msg(msg)?,

proto-json/src/tests/test.proto

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ message Message {
2020
repeated SubMessage repeated_msg = 8;
2121
optional SubMessage nested_msg = 9;
2222
optional int32 timestamp= 10 [(yara.field_options).fmt = "t"];
23+
optional Enum enum = 11;
24+
required uint32 flags = 12 [(yara.field_options).fmt = "flags:Flags"];
25+
26+
}
27+
28+
enum Enum {
29+
Foo = 1;
30+
Bar = 2;
2331
}
32+
33+
enum Flags {
34+
option (yara.enum_options).inline = true;
35+
FOO = 0x1;
36+
BAR = 0x2;
37+
BAZ = 0x4;
38+
}

proto-json/src/tests/testdata/1.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ str: "foo"
44
bool: true
55
bytes: "\xCA\xFE"
66
timestamp: 0
7+
enum: Foo
8+
flags: 3
79
repeated_msg {
810
i32: 456
911
str: "bar\nbar"

proto-json/src/tests/testdata/1.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@
2525
"foo": "bar"
2626
}
2727
},
28-
"timestamp": { "encoding": "timestamp", "value": 0 }
28+
"timestamp": { "encoding": "timestamp", "value": 0 },
29+
"enum": "Foo",
30+
"flags": 3
2931
}

0 commit comments

Comments
 (0)