Skip to content

Commit

Permalink
Merge pull request #7 from mitchhentges/make-map-list-static
Browse files Browse the repository at this point in the history
Make map() and list() static
  • Loading branch information
mitchhentges committed Apr 5, 2016
2 parents 6b36d45 + a2c737a commit fed0374
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ _Lower is better:_
String mapString = "{\"fast\":true, \"super-neat\":true}";
String listString = "[1, 2, false]";
JsonParse parse = new JsonParse();
Map<String, Object> map = parse.map(mapString);
List<Object> list = parse.list(listString);
Map<String, Object> map = JsonParse.map(mapString);
List<Object> list = JsonParse.list(listString);
```

## Getting the dependency
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/ca/fuzzlesoft/JsonParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
*/
@SuppressWarnings("unchecked") //Because of reusing `currentContainer` for both maps and lists
public class JsonParse {

private JsonParse() {}

/**
* Converts jsonString into a {@link Map}
* @param jsonString parsed
* @return the contents of the jsonString
*/
public Map<String, Object> map(String jsonString) {
public static Map<String, Object> map(String jsonString) {
return (Map<String, Object>) parse(jsonString, Type.OBJECT);
}

Expand All @@ -24,7 +27,7 @@ public Map<String, Object> map(String jsonString) {
* @param jsonString parsed
* @return the contents of the jsonString
*/
public List<Object> list(String jsonString) {
public static List<Object> list(String jsonString) {
return (List<Object>) parse(jsonString, Type.ARRAY);
}

Expand Down
49 changes: 21 additions & 28 deletions src/test/java/ca/fuzzlesoft/JsonParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@
*/
public class JsonParseTest {

private JsonParse jsonParse;

@Before
public void setUp() {
jsonParse = new JsonParse();
}

@Test
public void shouldParseStrings() {
String test = "{\"foo\":\"bar\"}";
Map<String, Object> expected = MapBuilder.init()
.add("foo", "bar")
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -40,7 +33,7 @@ public void shouldParseSingleCharacterStrings() {
Map<String, Object> expected = MapBuilder.init()
.add("a", "b")
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -53,7 +46,7 @@ public void shouldParseNumbers() {
.add("zurb", Double.valueOf("1E-4"))
.add("boop", 6L)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -64,7 +57,7 @@ public void shouldExcludeCharactersAfterNumbers() {
.add("bar", 2L)
.add("baz", 3L)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -74,7 +67,7 @@ public void shouldParseBooleans() {
.add("foo", true)
.add("bar", false)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -83,7 +76,7 @@ public void shouldParseNulls() {
Map<String, Object> expected = MapBuilder.init()
.add("foo", null)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -92,7 +85,7 @@ public void shouldAllowSpacesAroundColons() {
Map<String, Object> expected = MapBuilder.init()
.add("foo", true)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -101,7 +94,7 @@ public void shouldAllowSpacesAndCommasInPropertyNames() {
Map<String, Object> expected = MapBuilder.init()
.add("special, foo", "isspecial")
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -110,7 +103,7 @@ public void shouldAllowSpacesInStringPropertyValues() {
Map<String, Object> expected = MapBuilder.init()
.add("specialfoo", "is, special")
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -119,7 +112,7 @@ public void shouldIgnoreSurroundWhitespace() {
Map<String, Object> expected = MapBuilder.init()
.add("foo", true)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -132,7 +125,7 @@ public void shouldAcceptABunchOfPropertiesSeperatedByWhitespace() {
.add("false", false)
.add("null", null)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -141,7 +134,7 @@ public void shouldParseNestedObject() {
Map<String, Object> expected = MapBuilder.init()
.add("obj", MapBuilder.init().add("swag", true).build())
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -150,14 +143,14 @@ public void shouldAllowSpecialCharactersInStrings() {
Map<String, Object> expected = MapBuilder.init()
.add("foo", "{}[]'1234")
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
public void shouldParseList() {
String test = "[1, \"foo\", true]";
List<Object> list = Arrays.<Object>asList(1L, "foo", true);
Assert.assertEquals(jsonParse.list(test), list);
Assert.assertEquals(JsonParse.list(test), list);
}

@Test
Expand All @@ -169,7 +162,7 @@ public void shouldParseNestedNests() {
.add("ayy", "lmao").build()
)
.build();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
Expand All @@ -179,17 +172,17 @@ public void shouldThrowExceptionIfNoObject() {
String noStart = " } ";

try {
jsonParse.map(noObject);
JsonParse.map(noObject);
Assert.fail("Did not not throw exception when there was no containing object");
} catch (Exception ignored) {}

try {
jsonParse.map(noEnd);
JsonParse.map(noEnd);
Assert.fail("Did not throw exception when object didn't end");
} catch (Exception ignored) {}

try {
jsonParse.map(noStart);
JsonParse.map(noStart);
Assert.fail("Did not throw exception when object didn't start");
} catch (Exception ignored) {}
}
Expand All @@ -198,16 +191,16 @@ public void shouldThrowExceptionIfNoObject() {
public void shouldAllowTabsOrNewlines() {
String test = "\t\n{\t}\n\t";
Map<String, Object> expected = new HashMap<>();
Assert.assertEquals(expected, jsonParse.map(test));
Assert.assertEquals(expected, JsonParse.map(test));
}

@Test
public void shouldOnlyThrowJsonParseExceptionOnTooManyClosingTags() {
try {
jsonParse.map("{}}");
JsonParse.map("{}}");
} catch (JsonParseException ignored) {}
try {
jsonParse.list("[]]");
JsonParse.list("[]]");
} catch (JsonParseException ignored) {}
}

Expand Down

0 comments on commit fed0374

Please sign in to comment.