<commit_before># Hibiscus
Hibiscus is JSON schema language defined by Java constructs.

## Why do we write JSON schema in Java?
Writing JSON schema in Java has following merits:

* Your favorite IDE compiles it and detects errors automatically.
* Code completion available.
* Omits parsing external schema resource at every startup.
* Avoids troubles in missing "$ref" links.
* Can extend validation logic with Java code.

## Schema example

As first example we assume that you have following JSON contents:

```json
{
    "firstName": "Jason",
    "lastName": "Bourne",
    "age": 46,
    "hobbies": ["shooting", "recollection"]
}
```
If you want to validate this JSON against adequate schema, we can write the schema in Java like following:

```java
ObjectType schema = object(
    required("firstName", string()),
    required("lastName", string()),
    optional("age", integer()),
    optional("hobbies", array(string()))
);
```

## How to write your own JSON validator

1. Create class extending `JsonValidator` class.

   ```java
   import com.github.i49.hibiscus.validation.JsonValidator;

   public class PersonValidator extends JsonValidator {
   }
   ```

2. Add `static import` that will help building schema.

   ```java
   import static com.github.i49.hibiscus.validation.SchemaComponents.*;
   ```

3. Define your schema as class variable of your validator.

   ```java
   import com.github.i49.hibiscus.validation.JsonValidator;
   import com.github.i49.hibiscus.validation.ObjectType;
   import static com.github.i49.hibiscus.validation.SchemaComponents.*;

   public class PersonValidator extends JsonValidator {
       private static final ObjectType schema = object(
           required("firstName", string()),
           required("lastName", string()),
           optional("age", integer()),
           optional("hobbies", array(string()))
       );
   }
   ```  
4. Pass the schema to constructor of super class and complete it.

   ```java
   import com.github.i49.hibiscus.validation.JsonValidator;
   import com.github.i49.hibiscus.validation.ObjectType;
   import static com.github.i49.hibiscus.validation.SchemaComponents.*;

   public class PersonValidator extends JsonValidator {
       private static final ObjectType schema = object(
           required("firstName", string()),
           required("lastName", string()),
           optional("age", integer()),
           optional("hobbies", array(string()))
       );

       public PersonValidator() {
           super(schema)
       }
   }
   ```

## How to validate JSON with your validator

1. Validate JSON with your validator.

   ```java
   PersonValidator validator = new PersonValidator();
   ValidationResult result = null;
   try (Reader reader = new FileReader("person.json")) {
       result = validator.validate(reader);
   }
   ```

2. Handle detected problems properly.

   ```java
   for (Problem p: result.getProblems()) {
       // handles each problem here.
       // We just print message of the problem.
       System.out.println(p.getMessage());
   }
   ```

3. Use parsed JSON value.

   ```java
   JsonValue root = result.getValue();
   ```
   
   Hibiscus does both reading and validating at the same time.
   It returns JSON primitives defined in [Java API for JSON Processing (JSR-353, JSON-P)](http://json-processing-spec.java.net/) even when schema validation found some problems.<commit_msg>Added Travis CI status in README.md<commit_after># Hibiscus

[![Build Status](https://travis-ci.org/i49/Hibiscus.svg?branch=master)](https://travis-ci.org/i49/Hibiscus)

Hibiscus is JSON schema language defined by Java constructs.

## Why do we write JSON schema in Java?
Writing JSON schema in Java has following merits:

* Your favorite IDE compiles it and detects errors automatically.
* Code completion available.
* Omits parsing external schema resource at every startup.
* Avoids troubles in missing "$ref" links.
* Can extend validation logic with Java code.

## Schema example

As first example we assume that you have following JSON contents:

```json
{
  "firstName": "Jason",
  "lastName": "Bourne",
  "age": 46,
  "hobbies": ["shooting", "recollection"]
}
```
If you want to validate this JSON against adequate schema, we can write the schema in Java like following:

```java
ObjectType schema = object(
  required("firstName", string()),
  required("lastName", string()),
  optional("age", integer()),
  optional("hobbies", array(string()))
);
```

## How to write your own JSON validator

1. Create class extending `JsonValidator` class.

  ```java
  import com.github.i49.hibiscus.validation.JsonValidator;

  public class PersonValidator extends JsonValidator {
  }
  ```

2. Add `static import` statement that will help building schema.

  ```java
  import static com.github.i49.hibiscus.validation.SchemaComponents.*;
  ```

3. Define your schema as class variable of your validator class.

  ```java
  import com.github.i49.hibiscus.validation.JsonValidator;
  import com.github.i49.hibiscus.validation.ObjectType;
  import static com.github.i49.hibiscus.validation.SchemaComponents.*;

  public class PersonValidator extends JsonValidator {
    private static final ObjectType schema = object(
      required("firstName", string()),
      required("lastName", string()),
      optional("age", integer()),
      optional("hobbies", array(string()))
    );
  }
  ```  
4. Pass the schema to constructor of super class and then your work is done.

  ```java
  import com.github.i49.hibiscus.validation.JsonValidator;
  import com.github.i49.hibiscus.validation.ObjectType;
  import static com.github.i49.hibiscus.validation.SchemaComponents.*;

  public class PersonValidator extends JsonValidator {
    private static final ObjectType schema = object(
      required("firstName", string()),
      required("lastName", string()),
      optional("age", integer()),
      optional("hobbies", array(string()))
    );

    public PersonValidator() {
      super(schema)
    }
  }
  ```

## How to validate JSON with your validator

1. Validate JSON with your validator.

  ```java
  PersonValidator validator = new PersonValidator();
  ValidationResult result = null;
  try (Reader reader = new FileReader("person.json")) {
    result = validator.validate(reader);
  }
  ```

2. Handle detected problems properly.

  ```java
  for (Problem p: result.getProblems()) {
    // handles each problem here.
    // We just print text representing of the problem.
    System.out.println(p.getMessage());
  }
  ```

3. Use parsed JSON value.

  ```java
  JsonValue root = result.getValue();
  ```
   
   Hibiscus does both reading and validating JSON at the same time.
   It returns JSON primitives defined in [Java API for JSON Processing (JSR-353, JSON-P)](http://json-processing-spec.java.net/) even when schema validation found some problems.
{'fraction_non_alphanumeric': 0.11339754816112084, 'fraction_numerical': 0.008756567425569177, 'mean_word_length': 3.6148148148148147, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '693594', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1830, 'n_words': 645}