File tree 3 files changed +9
-14
lines changed
3 files changed +9
-14
lines changed Original file line number Diff line number Diff line change @@ -278,7 +278,8 @@ class Parser(
278
278
279
279
val valueMap : Map [FieldDescriptor , PValue ] = (for {
280
280
fd <- cmp.scalaDescriptor.fields
281
- jsValue <- values.get(serializedName(fd)) if ! jsValue.isNull
281
+ jsValue <- values.get(ScalapbJsonCommon .jsonName(fd)).orElse(values.get(fd.asProto.getName))
282
+ if ! jsValue.isNull
282
283
} yield (fd, parseValue(fd, jsValue))).toMap
283
284
284
285
PMessage (valueMap)
@@ -331,6 +332,7 @@ class Parser(
331
332
}
332
333
333
334
object JsonFormat {
335
+
334
336
import com .google .protobuf .wrappers
335
337
import scalapb_json .ScalapbJsonCommon ._
336
338
Original file line number Diff line number Diff line change @@ -76,19 +76,6 @@ class CodecSpec extends AnyFreeSpec with Matchers {
76
76
// Using asJson with an implicit printer includes the default value.
77
77
g.asJson mustBe Json .obj(" numberOfStrings" -> Json .fromInt(0 ))
78
78
}
79
-
80
- " decode using an implicit parser w/ non-standard settings" in {
81
- implicit val parser : Parser = new Parser (preservingProtoFieldNames = true )
82
-
83
- // Use the snake-case naming to define a Guitar Json object.
84
- val j = Json .obj(" number_of_strings" -> Json .fromInt(42 ))
85
-
86
- // Using the regular JsonFormat parser decodes to the defaultInstance.
87
- JsonFormat .fromJson[Guitar ](j) mustBe Guitar .defaultInstance
88
-
89
- // Using as[T] with an implicit parser decodes back to the original value (42).
90
- j.as[Guitar ] mustBe Right (Guitar (42 ))
91
- }
92
79
}
93
80
94
81
" GeneratedEnum" - {
Original file line number Diff line number Diff line change @@ -338,6 +338,12 @@ class JsonFormatSpec extends AnyFlatSpec with Matchers with OptionValues {
338
338
new Parser ().fromJsonString[MyTest ](""" {"optEnum":2}""" ) must be(MyTest (optEnum = Some (MyEnum .V2 )))
339
339
}
340
340
341
+
342
+ " TestProto" should " parse original field names" in {
343
+ new Parser ().fromJsonString[MyTest ](""" {"opt_enum":1}""" ) must be(MyTest (optEnum = Some (MyEnum .V1 )))
344
+ new Parser ().fromJsonString[MyTest ](""" {"opt_enum":2}""" ) must be(MyTest (optEnum = Some (MyEnum .V2 )))
345
+ }
346
+
341
347
" PreservedTestJson" should " be TestProto when parsed from json" in {
342
348
new Parser (preservingProtoFieldNames = true ).fromJsonString[MyTest ](PreservedTestJson ) must be(TestProto )
343
349
}
You can’t perform that action at this time.
0 commit comments