From 2f6d05dc51b695ccd5c8e3fefb31a789b210f5d1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 12 Feb 2018 17:05:32 +0100 Subject: [PATCH] Fix parsing of String value in json Closes gh-11992 --- .../org/springframework/boot/json/BasicJsonParser.java | 10 +++------- .../boot/json/AbstractJsonParserTests.java | 9 ++++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java index e3ffc4af0f..f07418f72d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.util.StringUtils; * * @author Dave Syer * @author Jean de Klerk + * @author Stephane Nicoll * @since 1.2.0 * @see JsonParserFactory */ @@ -112,12 +113,7 @@ public class BasicJsonParser implements JsonParser { for (String pair : tokenize(json)) { String[] values = StringUtils.trimArrayElements(StringUtils.split(pair, ":")); String key = trimLeadingCharacter(trimTrailingCharacter(values[0], '"'), '"'); - Object value = null; - if (values.length > 0) { - String string = trimLeadingCharacter( - trimTrailingCharacter(values[1], '"'), '"'); - value = parseInternal(string); - } + Object value = parseInternal(values[1]); map.put(key, value); } return map; diff --git a/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java index 33dd2e8a96..433ad09d09 100644 --- a/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,13 @@ public abstract class AbstractJsonParserTests { assertThat(map.get("spam")).isEqualTo(1.23d); } + @Test + public void stringContainingNumber() { + Map map = this.parser.parseMap("{\"foo\":\"123\"}"); + assertThat(map).hasSize(1); + assertThat(map.get("foo")).isEqualTo("123"); + } + @Test public void emptyMap() { Map map = this.parser.parseMap("{}");