diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java index 56edc4b488..dde3b542bf 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java @@ -313,7 +313,7 @@ private static void fillField( protoMsg.setField( fieldDescriptor, BigDecimalByteStringEncoder.encodeToNumericByteString( - new BigDecimal(((Number) val).doubleValue()))); + new BigDecimal(String.valueOf(val)))); return; } } else if (fieldSchema.getType() == TableFieldSchema.Type.BIGNUMERIC) { @@ -333,7 +333,7 @@ private static void fillField( protoMsg.setField( fieldDescriptor, BigDecimalByteStringEncoder.encodeToBigNumericByteString( - new BigDecimal(((Number) val).doubleValue()))); + new BigDecimal(String.valueOf(val)))); return; } } @@ -547,7 +547,7 @@ private static void fillRepeatedField( protoMsg.addRepeatedField( fieldDescriptor, BigDecimalByteStringEncoder.encodeToNumericByteString( - new BigDecimal(((Number) val).doubleValue()))); + new BigDecimal(String.valueOf(val)))); added = true; } } else if (fieldSchema != null @@ -568,7 +568,7 @@ private static void fillRepeatedField( protoMsg.addRepeatedField( fieldDescriptor, BigDecimalByteStringEncoder.encodeToBigNumericByteString( - new BigDecimal(((Number) val).doubleValue()))); + new BigDecimal(String.valueOf(val)))); added = true; } } diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java index e69e8f1bb0..c24a49f858 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java @@ -27,6 +27,7 @@ import com.google.protobuf.Message; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Collections; import java.util.Map; import java.util.logging.Logger; import org.json.JSONArray; @@ -1311,4 +1312,58 @@ public void testNullRepeatedField() throws Exception { JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json); assertTrue(protoMsg.getAllFields().isEmpty()); } + + @Test + public void testDoubleAndFloatToNumericConversion() { + TableSchema ts = + TableSchema.newBuilder() + .addFields( + 0, + TableFieldSchema.newBuilder() + .setName("numeric") + .setType(TableFieldSchema.Type.NUMERIC) + .build()) + .build(); + TestNumeric expectedProto = + TestNumeric.newBuilder() + .setNumeric( + BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("24.678"))) + .build(); + JSONObject json = new JSONObject(); + json.put("numeric", new Double(24.678)); + DynamicMessage protoMsg = + JsonToProtoMessage.convertJsonToProtoMessage(TestNumeric.getDescriptor(), ts, json); + assertEquals(expectedProto, protoMsg); + json.put("numeric", new Float(24.678)); + protoMsg = JsonToProtoMessage.convertJsonToProtoMessage(TestNumeric.getDescriptor(), ts, json); + assertEquals(expectedProto, protoMsg); + } + + @Test + public void testDoubleAndFloatToRepeatedBigNumericConversion() { + TableSchema ts = + TableSchema.newBuilder() + .addFields( + 0, + TableFieldSchema.newBuilder() + .setName("bignumeric") + .setType(TableFieldSchema.Type.BIGNUMERIC) + .setMode(TableFieldSchema.Mode.REPEATED) + .build()) + .build(); + TestBignumeric expectedProto = + TestBignumeric.newBuilder() + .addBignumeric( + BigDecimalByteStringEncoder.encodeToBigNumericByteString(new BigDecimal("24.678"))) + .build(); + JSONObject json = new JSONObject(); + json.put("bignumeric", Collections.singletonList(new Double(24.678))); + DynamicMessage protoMsg = + JsonToProtoMessage.convertJsonToProtoMessage(TestBignumeric.getDescriptor(), ts, json); + assertEquals(expectedProto, protoMsg); + json.put("bignumeric", Collections.singletonList(new Float(24.678))); + protoMsg = + JsonToProtoMessage.convertJsonToProtoMessage(TestBignumeric.getDescriptor(), ts, json); + assertEquals(expectedProto, protoMsg); + } }