From f922371fa838b169265830d8e5cd004710452f75 Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Fri, 24 Nov 2017 04:23:12 -0800 Subject: [PATCH] Let JsonValueReader.nextString read numbers. (#390) * Let JsonValueReader.nextString read numbers. This adds parity with JsonUtf8Reader and lets big number literals in JSON be read in as strings in Java. * Remove trailing 0 in float literal. --- .../java/com/squareup/moshi/JsonValueReader.java | 16 +++++++++++++--- .../com/squareup/moshi/JsonUtf8ReaderTest.java | 12 ++++++++++++ .../com/squareup/moshi/JsonValueReaderTest.java | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/moshi/src/main/java/com/squareup/moshi/JsonValueReader.java b/moshi/src/main/java/com/squareup/moshi/JsonValueReader.java index 9b3206f..ebc5ff4 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonValueReader.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonValueReader.java @@ -152,9 +152,19 @@ final class JsonValueReader extends JsonReader { } @Override public String nextString() throws IOException { - String peeked = require(String.class, Token.STRING); - remove(); - return peeked; + Object peeked = (stackSize != 0 ? stack[stackSize - 1] : null); + if (peeked instanceof String) { + remove(); + return (String) peeked; + } + if (peeked instanceof Number) { + remove(); + return peeked.toString(); + } + if (peeked == JSON_READER_CLOSED) { + throw new IllegalStateException("JsonReader is closed"); + } + throw typeMismatch(peeked, Token.STRING); } @Override public int selectString(Options options) throws IOException { diff --git a/moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java b/moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java index 2eb28b7..3b8b68a 100644 --- a/moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java +++ b/moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java @@ -336,6 +336,18 @@ public final class JsonUtf8ReaderTest { assertEquals("-0", reader.nextString()); } + @Test public void numberToStringCoersion() throws Exception { + JsonReader reader = newReader("[0, 9223372036854775807, 2.5, 3.010, \"a\", \"5\"]"); + reader.beginArray(); + assertThat(reader.nextString()).isEqualTo("0"); + assertThat(reader.nextString()).isEqualTo("9223372036854775807"); + assertThat(reader.nextString()).isEqualTo("2.5"); + assertThat(reader.nextString()).isEqualTo("3.010"); + assertThat(reader.nextString()).isEqualTo("a"); + assertThat(reader.nextString()).isEqualTo("5"); + reader.endArray(); + } + @Test public void quotedNumberWithEscape() throws IOException { JsonReader reader = newReader("[\"12\u00334\"]"); reader.setLenient(true); diff --git a/moshi/src/test/java/com/squareup/moshi/JsonValueReaderTest.java b/moshi/src/test/java/com/squareup/moshi/JsonValueReaderTest.java index 7b35f2a..5efd8e9 100644 --- a/moshi/src/test/java/com/squareup/moshi/JsonValueReaderTest.java +++ b/moshi/src/test/java/com/squareup/moshi/JsonValueReaderTest.java @@ -17,6 +17,7 @@ package com.squareup.moshi; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -423,6 +424,19 @@ public final class JsonValueReaderTest { } } + @Test public void numberToStringCoersion() throws Exception { + JsonReader reader = + new JsonValueReader(Arrays.asList(0, 9223372036854775807L, 2.5d, 3.01f, "a", "5")); + reader.beginArray(); + assertThat(reader.nextString()).isEqualTo("0"); + assertThat(reader.nextString()).isEqualTo("9223372036854775807"); + assertThat(reader.nextString()).isEqualTo("2.5"); + assertThat(reader.nextString()).isEqualTo("3.01"); + assertThat(reader.nextString()).isEqualTo("a"); + assertThat(reader.nextString()).isEqualTo("5"); + reader.endArray(); + } + @Test public void tooDeeplyNestedArrays() throws IOException { Object root = Collections.emptyList(); for (int i = 0; i < 32; i++) {