PHPackages                             mtrbean/mongo-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mtrbean/mongo-json

ActiveLibrary[Database &amp; ORM](/categories/database)

mtrbean/mongo-json
==================

A helper library for serializing MongoDB types to JSON string

1.1.0(12y ago)51591MITPHPPHP &gt;=5.3.2

Since Jun 18Pushed 12y ago1 watchersCompare

[ Source](https://github.com/mtrbean/MongoJson)[ Packagist](https://packagist.org/packages/mtrbean/mongo-json)[ RSS](/packages/mtrbean-mongo-json/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

MongoJson
=========

[](#mongojson)

A small library that JSON encode an Mongo document that contains MongoDB-specific types (e.g. MongoDate).

If you just use `json_encode()`, you will get some PHP specific results that makes it very difficult to interface with other part of your program. This library attempts to implement the 'strict mode' and also the 'javascript (JSONP) mode' defined in

Installation using Composer
---------------------------

[](#installation-using-composer)

Create a `composer.json` file in your project root:

```
{
    "require": {
        "mtrbean/mongo-json": "1.*"
    }
}

```

Install via composer:

```
php composer.phar install

```

P.S. If you haven't already been using composer for managing your dependency, you can install it via:

```
curl -sS https://getcomposer.org/installer | php

```

Autoload
--------

[](#autoload)

You can have the class `MongoJson` autoloaded using the mechanism provided by composer:

```
require 'vendor/autoload.php';
```

Usage
-----

[](#usage)

### Strict Mode

[](#strict-mode)

```
$doc = array("dt" => new MongoDate);
echo MongoJson::strict($doc);
```

will print:

```
{"dt":{"$date":1371525158000}}

```

### Extended Mode

[](#extended-mode)

```
$doc = array("dt" => new MongoDate);
echo MongoJson::extended($doc);
```

will print:

```
{"dt":new Date(1371525158000)}

```

### Options

[](#options)

You can pass extra options that you can normally use with `json_encode()`

```
$doc = array("_id" => new MongoId, "regex" => new MongoRegex('/^acme.*corp/i'));
echo MongoJson::extended($doc, JSON_PRETTY_PRINT); // will prettify the JSON string
```

will print:

```
{
    "_id": {
        "$oid": "51bfcdd71ede01d61a000000"
    },
    "regex": /^acme.*corp/i
}

```

License
-------

[](#license)

MIT

Contribute
----------

[](#contribute)

Feel free to fork and submit pull requests!

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity60

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

3

Last Release

4707d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0b2c3ba23fe0af0e04847a4a62cfe94eab397145bf6dbe02079ce33683e3faa?d=identicon)[mtrbean](/maintainers/mtrbean)

---

Tags

jsondatabasemongodb

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtrbean-mongo-json/health.svg)

```
[![Health](https://phpackages.com/badges/mtrbean-mongo-json/health.svg)](https://phpackages.com/packages/mtrbean-mongo-json)
```

###  Alternatives

[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[moloquent/moloquent

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

120114.6k7](/packages/moloquent-moloquent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
