Package com.crawljax.matchers

Source Code of com.crawljax.matchers.IsValidJson

package com.crawljax.matchers;

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

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IsValidJson {

  /**
   * @return a {@link Matcher} that checks if the given {@link File} contains a valid JSON object.
   */
  @Factory
  public static <T> Matcher<File> isValidJson() {
    return new TypeSafeMatcher<File>() {

      @Override
      public void describeTo(Description description) {
        description.appendText("Valid JSON String");
      }

      @Override
      protected boolean matchesSafely(File item) {
        boolean valid = false;
        try {
          JsonParser parser = new ObjectMapper().getFactory().createParser(item);
          while (parser.nextToken() != null) {
          }
          valid = true;
        } catch (IOException e) {
          throw new AssertionError(e);
        }

        return valid;
      }
    };
  }
}
TOP

Related Classes of com.crawljax.matchers.IsValidJson

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.