package com.yammer.dropwizard.testing;

import com.yammer.dropwizard.json.Json;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;

import static com.yammer.dropwizard.testing.FixtureHelpers.fixture;

/**
 * A set of helper methods for testing the serialization and deserialization of classes to and from
 * JSON.
 * <p>For example, a test for reading and writing a {@code Person} object as JSON:</p>
 * <pre><code>
 * assertThat("writing a person as JSON produces the appropriate JSON object",
 *            asJson(person),
 *            is(jsonFixture("fixtures/person.json"));
 *
 * assertThat("reading a JSON object as a person produces the appropriate person",
 *            fromJson(jsonFixture("fixtures/person.json"), Person.class),
 *            is(person));
 * </code></pre>
 */
public class JsonHelpers {
    private static final Json JSON = new Json();

    private JsonHelpers() { /* singleton */ }

    /**
     * Converts the given object into a canonical JSON string.
     *
     * @param object    an object
     * @return {@code object} as a JSON string
     * @throws IOException if there is an error writing {@code object} as JSON
     */
    public static String asJson(Object object) throws IOException {
        return JSON.writeValueAsString(object);
    }

    /**
     * Converts the given JSON string into an object of the given type.
     *
     * @param json     a JSON string
     * @param klass    the class of the type that {@code json} should be converted to
     * @param <T>      the type that {@code json} should be converted to
     * @return {@code json} as an instance of {@code T}
     * @throws IOException if there is an error reading {@code json} as an instance of {@code T}
     */
    public static <T> T fromJson(String json, Class<T> klass) throws IOException {
        return JSON.readValue(json, klass);
    }

    /**
     * Converts the given JSON string into an object of the given type.
     *
     * @param json         a JSON string
     * @param reference    a reference of the type that {@code json} should be converted to
     * @param <T>          the type that {@code json} should be converted to
     * @return {@code json} as an instance of {@code T}
     * @throws IOException if there is an error reading {@code json} as an instance of {@code T}
     */
    public static <T> T fromJson(String json, TypeReference<T> reference) throws IOException {
        return JSON.readValue(json, reference);
    }

    public static String jsonFixture(String filename) throws IOException {
        return JSON.writeValueAsString(JSON.readValue(fixture(filename), JsonNode.class));
    }
}

@@ -1137,33 +1137,46 @@
      * @throws I
-O
+llegalArgument
 Exception if the
@@ -1190,20 +1190,21 @@
 n error 
-writ
+encod
 ing %7B@co
@@ -1213,24 +1213,16 @@
  object%7D
- as JSON
 %0A     */
@@ -1269,33 +1269,46 @@
 object) throws I
-O
+llegalArgument
 Exception %7B%0A    

Fix JsonHelpers#asJson throws clause.