All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jayway.jsonpath.matchers.IsJson Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
package com.jayway.jsonpath.matchers;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.ReadContext;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.io.File;
import java.io.IOException;

public class IsJson extends TypeSafeMatcher {
    private final Matcher jsonMatcher;

    public IsJson(Matcher jsonMatcher) {
        this.jsonMatcher = jsonMatcher;
    }

    @Override
    protected boolean matchesSafely(T json) {
        try {
            ReadContext context = parse(json);
            return jsonMatcher.matches(context);
        } catch (JsonPathException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }

    public void describeTo(Description description) {
        description.appendText("is json ").appendDescriptionOf(jsonMatcher);
    }

    @Override
    protected void describeMismatchSafely(T json, Description mismatchDescription) {
        try {
            ReadContext context = parse(json);
            jsonMatcher.describeMismatch(context, mismatchDescription);
        } catch (JsonPathException e) {
            buildMismatchDescription(json, mismatchDescription, e);
        } catch (IOException e) {
            buildMismatchDescription(json, mismatchDescription, e);
        }
    }

    private static void buildMismatchDescription(Object json, Description mismatchDescription, Exception e) {
        mismatchDescription
                .appendText("was ")
                .appendValue(json)
                .appendText(" which failed with ")
                .appendValue(e.getMessage());
    }

    private static ReadContext parse(Object object) throws IOException {
        if (object instanceof String) {
            return JsonPath.parse((String) object);
        } else if (object instanceof File) {
            return JsonPath.parse((File) object);
        } else if (object instanceof ReadContext) {
            return (ReadContext) object;
        } else {
            return JsonPath.parse(object);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy