Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Kanti/LJSON

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Packagist Packagist Travis Code Climate Code Climate

LJSON

LJSON is a drop-in replacement for JSON which also allows you to parse and stringify pure functions and their contents. There are good security reasons for functions to be out of the JSON specs, but most of those are only significant when you allow arbitrary, side-effective programs. With pure functions, one is able to interchange code while still being as safe as with regular JSON.

note:
this is a port of LJSON for JavaScript originaly from MaiaVictor

<?php
require_once "vendor/autoload.php";

// A random object with a pure function inside.
$person = [
    "name" => "John",
    "mail" => function ($msg) {
        return [
            "author" => "John",
            "message" => $msg,
        ];
    },
];

$personStr    = \LJSON\LJSON::stringify($person);
$personVal    = \LJSON\LJSON::parse($personStr);
$mailFunction = $personVal->mail;
$mail         = $mailFunction("hello");// would crash with json_encode

echo $personStr . "\n";
echo \LJSON\LJSON::stringify($mail) . "\n";

output:

{"name":"John","mail":(v0) => ({"author":"John","message":v0})}
{"author":"John","message":"hello"}

More info:

Installing

composer require ljson/ljson