Sunday, March 8, 2015

Comparing JSON Strings with Jackson

Here's a quick way to compare two JSON String using Jackson. While we could always do a simple String compare, this method allows us to keep the expected JSON in a readable format, such as pretty-printed in a test resource:

import com.fasterxml.jackson.databind.ObjectMapper

class JsonAssert {

    static ObjectMapper mapper = new ObjectMapper()

    static void areEqual(String json1, String json2) {
        def tree1 = mapper.readTree json1
        def tree2 = mapper.readTree json2

        assert tree1.equals(tree2)
    }

}

The above can be called from an expect: or then: block in a Spock specification.

1 comment:

  1. The blog posted was very informative and useful thanks for sharing. Our on-line coaching supplier gained the high commonplace name through worldwide for its teaching.further information visit site.
    Oracle fusion financials training

    ReplyDelete