It happens every time. You release a library and then go round looking for ways to promote it, only to discover something similar…

In this case, I knew I was replicating the feature set of JSONAssert, but hadn’t factored in the existence of the rather fully featured JsonUnit.

However, ModelAssert is live, and I’m still delighted with it.

DSL by Function Call not String

I’ve written quite a few libraries over the last few years where it’s necessary to express some sort of configuration or rule.

There are two basic approaches to this:

  • Have a “language” for the configuration, based on expressions, parsed from Strings
  • Use a fluent DSL to build the objects that represent the configuration, all as code

Let’s compare two examples. This is how to express an assertion in the rather neat JsonUnit library:


The special placeholder in the right hand side tells the assertion to ignore this field.

Here’s the equivalent in ModelAssert:


I’ve replicated the a field in the expected here, though that’s not necessary for the assertion to work.

Let’s compare another. Let’s look at a path wildcard in JsonUnit:

assertThatJson("[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]")
    .isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]");

In this instance, we’ve got a JSON path expression that matches any array index. In ModelAssert:

assertJson("[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]")
    .where().path(ANY, "b").isIgnored()
    .isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]");

In the ModelAssert version, rather than a cryptic language, expressing the wildcard rules, the match is more self explanatory.

DSLs Rock

The more that the syntax of the tool’s configuration can be turned into builder functions, the more expressive the tool is, and the more autocomplete help you get.

While there’s much to be learned from JSON Unit and JSON Assert, I’m relatively pleased with the alternate direction I’ve taken.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s