Skip to content

Commit

Permalink
Handle simplified (single quoted) JSON
Browse files Browse the repository at this point in the history
Java strings are easier to write when you don't have to escape double
quotes all the time. If the JSON parsing fails due to an invalid single
quote character, try again with all the quotes replaced with double
quotes.
  • Loading branch information
joshka committed Aug 27, 2018
1 parent 31f7965 commit d82fdb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Expand Up @@ -9,6 +9,8 @@
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.json.stream.JsonParsingException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.stream.Stream;
Expand All @@ -23,6 +25,18 @@ public void accept(JsonSource jsonFileSource) {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
try {
return getArguments(value);
} catch (JsonParsingException e) {
// attempt to parse simplified json e.g. "{'key':value'}"
if (e.getMessage().contains("Unexpected char 39")) {
return getArguments(value.replace("'", "\""));
}
throw e;
}
}

private Stream<? extends Arguments> getArguments(String value) throws IOException {
try (Reader reader = new StringReader(value)) {
return values(reader).map(Arguments::of);
}
Expand Down
Expand Up @@ -65,4 +65,11 @@ void arrayOfNumbers(JsonNumber number) {
void arrayOfStrings(JsonString string) {
assertThat(string.getString()).startsWith("value");
}

@ParameterizedTest
@JsonSource("{'key':'value'}")
@DisplayName("handles simplified json")
void simplifiedJson(JsonObject object) {
assertThat(object.getString("key")).isEqualTo("value");
}
}

0 comments on commit d82fdb7

Please sign in to comment.