diff --git a/core/shared/src/main/scala/scalapb_circe/JsonFormat.scala b/core/shared/src/main/scala/scalapb_circe/JsonFormat.scala index ad08816..e6c1f05 100644 --- a/core/shared/src/main/scala/scalapb_circe/JsonFormat.scala +++ b/core/shared/src/main/scala/scalapb_circe/JsonFormat.scala @@ -278,7 +278,8 @@ class Parser( val valueMap: Map[FieldDescriptor, PValue] = (for { fd <- cmp.scalaDescriptor.fields - jsValue <- values.get(serializedName(fd)) if !jsValue.isNull + jsValue <- values.get(ScalapbJsonCommon.jsonName(fd)).orElse(values.get(fd.asProto.getName)) + if !jsValue.isNull } yield (fd, parseValue(fd, jsValue))).toMap PMessage(valueMap) @@ -331,6 +332,7 @@ class Parser( } object JsonFormat { + import com.google.protobuf.wrappers import scalapb_json.ScalapbJsonCommon._ diff --git a/core/shared/src/test/scala/scalapb_circe/CodecSpec.scala b/core/shared/src/test/scala/scalapb_circe/CodecSpec.scala index e771b6f..c540aa1 100644 --- a/core/shared/src/test/scala/scalapb_circe/CodecSpec.scala +++ b/core/shared/src/test/scala/scalapb_circe/CodecSpec.scala @@ -76,19 +76,6 @@ class CodecSpec extends AnyFreeSpec with Matchers { // Using asJson with an implicit printer includes the default value. g.asJson mustBe Json.obj("numberOfStrings" -> Json.fromInt(0)) } - - "decode using an implicit parser w/ non-standard settings" in { - implicit val parser: Parser = new Parser(preservingProtoFieldNames = true) - - // Use the snake-case naming to define a Guitar Json object. - val j = Json.obj("number_of_strings" -> Json.fromInt(42)) - - // Using the regular JsonFormat parser decodes to the defaultInstance. - JsonFormat.fromJson[Guitar](j) mustBe Guitar.defaultInstance - - // Using as[T] with an implicit parser decodes back to the original value (42). - j.as[Guitar] mustBe Right(Guitar(42)) - } } "GeneratedEnum" - { diff --git a/core/shared/src/test/scala/scalapb_circe/JsonFormatSpec.scala b/core/shared/src/test/scala/scalapb_circe/JsonFormatSpec.scala index a791c87..2225f98 100644 --- a/core/shared/src/test/scala/scalapb_circe/JsonFormatSpec.scala +++ b/core/shared/src/test/scala/scalapb_circe/JsonFormatSpec.scala @@ -338,6 +338,11 @@ class JsonFormatSpec extends AnyFlatSpec with Matchers with OptionValues { new Parser().fromJsonString[MyTest]("""{"optEnum":2}""") must be(MyTest(optEnum = Some(MyEnum.V2))) } + "TestProto" should "parse original field names" in { + new Parser().fromJsonString[MyTest]("""{"opt_enum":1}""") must be(MyTest(optEnum = Some(MyEnum.V1))) + new Parser().fromJsonString[MyTest]("""{"opt_enum":2}""") must be(MyTest(optEnum = Some(MyEnum.V2))) + } + "PreservedTestJson" should "be TestProto when parsed from json" in { new Parser(preservingProtoFieldNames = true).fromJsonString[MyTest](PreservedTestJson) must be(TestProto) }