<commit_before>minimal-json
============

[![License](https://img.shields.io/github/license/ralfstx/minimal-json.svg)](https://github.com/ralfstx/minimal-json/blob/master/LICENSE)
[![Maven Central](https://img.shields.io/maven-central/v/com.eclipsesource.minimal-json/minimal-json.svg)](http://search.maven.org/#search|ga|1|g%3A%22com.eclipsesource.minimal-json%22%20a%3A%22minimal-json%22)
[![Build Status](https://img.shields.io/travis/ralfstx/minimal-json.svg)](http://travis-ci.org/ralfstx/minimal-json)

A fast and minimal JSON parser and writer for Java.
It's not an object mapper, but a bare-bones library that aims at being

* **minimal**: no dependencies, single package with just a few classes, small download size (< 25kB)
* **fast**: high performance comparable with other state-of-the-art parsers (see below)
* **lightweight**: object representation with minimal memory footprint (e.g. no HashMaps)
* **simple**: reading, writing and modifying JSON with minimal code (short names, fluent style)

Minimal-json is fully covered by unit tests, and field-tested by the [Eclipse RAP project](http://eclipse.org/rap) and others (see below). The JAR contains a **valid OSGi** bundle manifest and can be used in OSGi environments without modifications.

Code Examples
-------------

### Read JSON from a String or a Reader

Reading is buffered already, so you *don't* need to wrap your reader in a BufferedReader.

```java
JsonObject jsonObject = JsonObject.readFrom( string );
JsonArray jsonArray = JsonArray.readFrom( reader );
```

### Access the contents of a JSON object

The `get` method returns a `JsonValue` if the element exists. Depending on the type, you can get corresponding Java values using the methods `asString`, `asBoolean`, etc.

```java
String name = jsonObject.get( "name" ).asString();
int age = jsonObject.get( "age" ).asInt(); // asLong(), asFloat(), asDouble(), ...
```

you can use a default value if the element does not exist:

```java
String name = jsonObject.getString( "name", "unknown" );
int age = jsonObject.getInt( "age", -1 );
```

you can also iterate over the members:

```java
for( Member member : jsonObject ) {
  String name = member.getName();
  JsonValue value = member.getValue();
  // ...
}
```

### Access the contents of a JSON array

```java
String name = jsonArray.get( 0 ).asString();
int age = jsonArray.get( 1 ).asInt(); // asLong(), asFloat(), asDouble(), ...

// or iterate over the values:
for( JsonValue value : jsonArray ) {
  // ...
}
```

### Access nested contents

```java
// Example: { "friends": [ { "name": "John", "age": 23 }, ... ], ... }
JsonArray friends = jsonObject.get( "friends" ).asArray();
String name = friends.get( 0 ).asObject().get( "name" ).asString();
int age = friends.get( 0 ).asObject().get( "age" ).asInt();
```

### Create JSON objects and arrays

```java
JsonObject jsonObject = new JsonObject().add( "name", "John" ).add( "age", 23 );
// -> { "name": "John", "age", 23 }

JsonArray jsonArray = new JsonArray().add( "John" ).add( 23 );
// -> [ "John", 23 ]
```

### Modify JSON objects and arrays

```java
jsonObject.set( "age", 24 );
jsonArray.set( 1, 24 ); // access element by index

jsonObject.remove( "age" );
jsonArray.remove( 1 );
```

### Write JSON to a Writer or a String

These methods can be used on all JsonValues:

```java
jsonValue.writeTo( writer );
String json = jsonValue.toString();
```

Enable formatted output:

```java
jsonValue.writeTo( writer, WriterConfig.PRETTY_PRINT );
String json = jsonValue.toString( WriterConfig.PRETTY_PRINT );
```<commit_msg>Fix typo and adjust library size in README<commit_after>minimal-json
============

[![License](https://img.shields.io/github/license/ralfstx/minimal-json.svg)](https://github.com/ralfstx/minimal-json/blob/master/LICENSE)
[![Maven Central](https://img.shields.io/maven-central/v/com.eclipsesource.minimal-json/minimal-json.svg)](http://search.maven.org/#search|ga|1|g%3A%22com.eclipsesource.minimal-json%22%20a%3A%22minimal-json%22)
[![Build Status](https://img.shields.io/travis/ralfstx/minimal-json.svg)](http://travis-ci.org/ralfstx/minimal-json)

A fast and minimal JSON parser and writer for Java.
It's not an object mapper, but a bare-bones library that aims at being

* **minimal**: no dependencies, single package with just a few classes, small download size (< 35kB)
* **fast**: high performance comparable with other state-of-the-art parsers (see below)
* **lightweight**: object representation with minimal memory footprint (e.g. no HashMaps)
* **simple**: reading, writing and modifying JSON with minimal code (short names, fluent style)

Minimal-json is fully covered by unit tests, and field-tested by the [Eclipse RAP project](http://eclipse.org/rap) and others (see below). The JAR contains a **valid OSGi** bundle manifest and can be used in OSGi environments without modifications.

Code Examples
-------------

### Read JSON from a String or a Reader

Reading is buffered already, so you *don't* need to wrap your reader in a BufferedReader.

```java
JsonObject jsonObject = JsonObject.readFrom( string );
JsonArray jsonArray = JsonArray.readFrom( reader );
```

### Access the contents of a JSON object

The `get` method returns a `JsonValue` if the element exists. Depending on the type, you can get corresponding Java values using the methods `asString`, `asBoolean`, etc.

```java
String name = jsonObject.get( "name" ).asString();
int age = jsonObject.get( "age" ).asInt(); // asLong(), asFloat(), asDouble(), ...
```

you can use a default value if the element does not exist:

```java
String name = jsonObject.getString( "name", "unknown" );
int age = jsonObject.getInt( "age", -1 );
```

you can also iterate over the members:

```java
for( Member member : jsonObject ) {
  String name = member.getName();
  JsonValue value = member.getValue();
  // ...
}
```

### Access the contents of a JSON array

```java
String name = jsonArray.get( 0 ).asString();
int age = jsonArray.get( 1 ).asInt(); // asLong(), asFloat(), asDouble(), ...

// or iterate over the values:
for( JsonValue value : jsonArray ) {
  // ...
}
```

### Access nested contents

```java
// Example: { "friends": [ { "name": "John", "age": 23 }, ... ], ... }
JsonArray friends = jsonObject.get( "friends" ).asArray();
String name = friends.get( 0 ).asObject().get( "name" ).asString();
int age = friends.get( 0 ).asObject().get( "age" ).asInt();
```

### Create JSON objects and arrays

```java
JsonObject jsonObject = new JsonObject().add( "name", "John" ).add( "age", 23 );
// -> { "name": "John", "age": 23 }

JsonArray jsonArray = new JsonArray().add( "John" ).add( 23 );
// -> [ "John", 23 ]
```

### Modify JSON objects and arrays

```java
jsonObject.set( "age", 24 );
jsonArray.set( 1, 24 ); // access element by index

jsonObject.remove( "age" );
jsonArray.remove( 1 );
```

### Write JSON to a Writer or a String

These methods can be used on all JsonValues:

```java
jsonValue.writeTo( writer );
String json = jsonValue.toString();
```

Enable formatted output:

```java
jsonValue.writeTo( writer, WriterConfig.PRETTY_PRINT );
String json = jsonValue.toString( WriterConfig.PRETTY_PRINT );
```
{'fraction_non_alphanumeric': 0.16645719871526324, 'fraction_numerical': 0.010054461667364893, 'mean_word_length': 4.336810730253354, 'pattern_counts': {'":': 9, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1705588', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2175, 'n_words': 753}