"First, you must create the universe"Strictly speaking, without a working universe, writing a JSON parser would be difficult. But beyond having a universe, it might also be helpful to know what JSON actually is.
--Carl Sagan
This is a JSON object:
{
"name":"Coding As A Life Hobby",
"count":3,
"titles":[
"Concept",
"Setup",
"Requirements"
]
}
JSON is a lightweight, human-readable format for encoding data, used by basically any web service that wants to be taken seriously. A JSON object contains a bunch of key/value pairs, where the key is some string, and the value can be a JSON property of any type. The types are:
- String - Any sequence of unicode characters
- Number - Can be an integer or floating point, and must be expressible using "e" notation (like 6.23e23)
- Boolean - true or false
- Array - An ordered list, delimited by square brackets '[]', of JSON properties, each of which can be any of these six types
- Object - An unordered set of key/value pairs, like I mentioned above
- Null - Just null
Ideally, I would like my JSON parsing library to map this JSON to a Java object that might be defined like:
package com.themutatedshrimp.json.example; import java.util.Arrays; public class ExampleObject { private final String name; private final int count; private final String[] titles; public ExampleObject(final String name, final String... titles) { this.name = name; this.count = titles.length; this.titles = titles; } public String getName() { return name; } public int getCount() { return count; } public String getTitle(final int index) { return titles[index]; } }
Where the values as defined in the JSON string get mapped into the instance data of this object.
Since I will be using this object in my unit tests, I will introduce it into the code base as commit REQS-1. While I'm at it, I'll also go ahead and remove Maven's gifts - App and AppTest, since all they're doing is adding clutter.
My end goal is to pass a JSON string, like the one I mentioned above, into some JSON processor object, and have it spit out an ExampleObject with the fields populated.
Or in other words, this unit test should pass:
@Test public void testExampleDeserialization() { final String json = "{" + "\"name\":\"Coding As A Life Hobby\"," + "\"count\":3," + "\"titles\":[\"Concept\",\"Setup\",\"Requirements\"]" + "}"; final JsonProcessor processor = new JsonProcessor(); final ExampleObject example = (ExampleObject)processor.deserialize(json); assertEquals("Coding As A Life Hobby", example.getName()); assertEquals(3, example.getCount()); assertEquals("Concept", example.getTitle(0)); assertEquals("Setup", example.getTitle(1)); assertEquals("Requirements", example.getTitle(2)); }
The commit REQS-2 has this unit test added, and just enough code to make the code compile, namely a skeleton JsonProcessor class, which I will probably want to rename at some point.
If you check out REQS-2 (by running "git checkout REQS-2"), and try to build (by running "mvn package"), you will notice that the unit test fails, blocking the build. That makes sense, because this test asserts a very specific behavior that doesn't work yet. But now isn't the time to make this test pass either, because I honestly have no idea how to do that.
What I've done so far isn't TDD, by the way; this test is far too complicated. Before this is over, I'll show you a positive example of TDD so that you can see the difference.
All I did here was establish one of my requirements, in a form that happens to be a test. It's okay that I did this, but I'm not going to make the test pass right now. Instead, I will add the "@Ignore" annotation, so that JUnit doesn't try to run this test. Once I think my code is ready for this test, I'll drop "@Ignore", but that will be a while.
Check out REQS-3 and run "mvn package", and you'll see the output
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1Sanity is restored, but the existence of skipped tests will serve as a warning that we still have work to do...