@@ -201,7 +201,8 @@ public String nextString() throws IOException {
201201 * @see com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT
202202 */
203203private String scalarToString () {
204- switch (delegate .getCurrentBsonType ()) {
204+ final BsonType type =delegate .getCurrentBsonType ();
205+ switch (type ) {
205206case STRING :
206207return delegate .readString ();
207208case SYMBOL :
@@ -219,8 +220,7 @@ private String scalarToString() {
219220case OBJECT_ID :
220221return delegate .readObjectId ().toHexString ();
221222default :
222- throw new IllegalStateException (
223- "Unknown scalar type to be converted to string: " +delegate .getCurrentBsonType ());
223+ throw new IllegalStateException ("Unknown scalar type to be converted to string: " +type );
224224 }
225225 }
226226
@@ -236,7 +236,10 @@ public void nextNull() throws IOException {
236236
237237@ Override
238238public double nextDouble ()throws IOException {
239- switch (delegate .getCurrentBsonType ()) {
239+ final BsonType type =delegate .getCurrentBsonType ();
240+ switch (type ) {
241+ case DOUBLE :
242+ return delegate .readDouble ();
240243case INT32 :
241244return delegate .readInt32 ();
242245case INT64 :
@@ -248,43 +251,49 @@ public double nextDouble() throws IOException {
248251case TIMESTAMP :
249252return delegate .readTimestamp ().getValue ();
250253default :
251- return delegate . readDouble ( );
254+ throw new IllegalStateException ( String . format ( "Expected numeric bson type (double) but got %s (as json:%s)" , type , toGsonToken ( type )) );
252255 }
253256 }
254257
255258@ Override
256259public long nextLong ()throws IOException {
257- switch (delegate .getCurrentBsonType ()) {
258- case DOUBLE :
259- return (long )delegate .readDouble ();
260+ BsonType type =delegate .getCurrentBsonType ();
261+ switch (type ) {
262+ case INT64 :
263+ return delegate .readInt64 ();
260264case INT32 :
261265return delegate .readInt32 ();
266+ case DOUBLE :
267+ return (long )delegate .readDouble ();
262268case DECIMAL128 :
263269return delegate .readDecimal128 ().bigDecimalValue ().longValueExact ();
264270case DATE_TIME :
265271return delegate .readDateTime ();
266272case TIMESTAMP :
267273return delegate .readTimestamp ().getValue ();
268274default :
269- return delegate . readInt64 ( );
275+ throw new IllegalStateException ( String . format ( "Expected numeric bson type (long) but got %s (as json:%s)" , type , toGsonToken ( type )) );
270276 }
271277 }
272278
273279@ Override
274280public int nextInt ()throws IOException {
275- switch (delegate .getCurrentBsonType ()) {
276- case DOUBLE :
277- return (int )delegate .readDouble ();
281+ final BsonType type =delegate .getCurrentBsonType ();
282+ switch (type ) {
283+ case INT32 :
284+ return delegate .readInt32 ();
278285case INT64 :
279286return (int )delegate .readInt64 ();
287+ case DOUBLE :
288+ return (int )delegate .readDouble ();
280289case DECIMAL128 :
281290return delegate .readDecimal128 ().bigDecimalValue ().intValueExact ();
282291case DATE_TIME :
283292return (int )delegate .readDateTime ();
284293case TIMESTAMP :
285294return (int )delegate .readTimestamp ().getValue ();
286295default :
287- return delegate . readInt32 ( );
296+ throw new IllegalStateException ( String . format ( "Expected numeric bson type (int) but got %s (as json:%s)" , type , toGsonToken ( type )) );
288297 }
289298 }
290299