Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/sync-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Sync Docs

on:
push:
branches: [main]
paths:
- 'docs/**'

jobs:
notify-docs-repo:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.DOCS_TOKEN }}
repository: cortexphp/docs
event-type: docs-updated
89 changes: 3 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Repair invalid JSON strings by automatically fixing common syntax errors like si
## Requirements

- PHP 8.3+
- JSON PHP extension

## Installation

Expand Down Expand Up @@ -41,93 +42,9 @@ $data = (new JsonRepairer($broken))->decode();
$data = json_repair_decode($broken);
```

## Configuration Options
## Documentation

### Omit Empty Values

When repairing JSON from streaming sources (e.g., LLM responses), you may want to remove keys with missing values instead of adding empty strings:

```php
// Missing value - defaults to adding empty string
$broken = '{"name": "John", "age": }';
$repaired = json_repair($broken);
// {"name": "John", "age": ""}

// Remove keys with missing values
$repaired = json_repair($broken, omitEmptyValues: true);
// {"name": "John"}
```

### Omit Incomplete Strings

Similarly, you can remove keys with incomplete string values instead of closing them:

```php
// Incomplete string - defaults to closing the string
$broken = '{"name": "John", "bio": "A developer who';
$repaired = json_repair($broken);
// {"name": "John", "bio": "A developer who"}

// Remove keys with incomplete strings
$repaired = json_repair($broken, omitIncompleteStrings: true);
// {"name": "John"}
```

### Using Both Options Together

Both options can be used together, which is especially useful for streaming JSON where deltas are concatenated:

```php
$broken = '{"name": "John", "age": , "bio": "A developer who';
$repaired = json_repair($broken, omitEmptyValues: true, omitIncompleteStrings: true);
// {"name": "John"}
```

### Using with JsonRepairer Class

You can also pass these options to the `JsonRepairer` constructor:

```php
$repairer = new JsonRepairer(
$broken,
ensureAscii: true,
omitEmptyValues: true,
omitIncompleteStrings: true
);
$repaired = $repairer->repair();
```

Or with `json_repair_decode`:

```php
$data = json_repair_decode(
$broken,
omitEmptyValues: true,
omitIncompleteStrings: true
);
```

## Logging

The library supports PSR-3 logging for debugging repair operations. Pass any PSR-3 compatible logger to see what repairs are being made:

```php
use Psr\Log\LoggerInterface;

// Using the helper function
$repaired = json_repair($broken, logger: $logger);

// Using the class (implements LoggerAwareInterface)
$repairer = new JsonRepairer($broken);
$repairer->setLogger($logger);
$repaired = $repairer->repair();
```

Log messages include the position in the JSON string and a context snippet showing where the repair occurred. This is useful for:

- Debugging why certain repairs are being made
- Understanding how malformed JSON is being interpreted
- Tracking repair operations in production environments
📚 **[View Full Documentation →](https://docs.cortexphp.com/json-repair)**

## Credits

Expand Down
13 changes: 13 additions & 0 deletions docs/assets/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/assets/logo-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading