JSON Formater

JSON Formater

Allow a pretty-print of JSON data in a human-readable format

::encode($value) : [string]

 Renders Json represented Data
Paramters Type Required Descritpion
$value Mixed true The value being encoded. Note: all String must be UTF-8 encoded.
default: void

$json = array(
    'first_name' => 'Foo',
    'last_name'  => 'Bar',
    'personal'   => array(
        'emails' => array(
            'email_1' => 'foo@Bar.com',
            'email_2' => 'Bar@foo.com'
        ),
        'links' => array(
            'json' => array(
                'json_1' => 'http://json.foobar.com/foobar1',
                'json_2' => 'http://json.foobar.com/foobar2'
            ),
            'url' => 'http://foobar.com'
        )
    )
);


 echo Json::encode($json)

The above code Outputs:


{
"first_name": "Foo",
"last_name": "Bar",
"personal": {
"emails": {
"email_1": "foo@Bar.com",
"email_2": "Bar@foo.com"
},
"links": {
"json": {
"json_1": "http://json.foobar.com/foobar1",
"json_2": "http://json.foobar.com/foobar2"
},
"url": "http://foobar.com"
}
}
}

::setBreak($break_string) : [void]

 Replaces the default line breaker with the sepecified replacement string.
Paramters Type Required Descritpion
$break_string string true The replacement value that replaces default string break or line break.
default: <br />
Json::setBreak("\n");

//--OR


Json::setBreak('PHP_EOL');

::setIndent($open_replacement, $close_replacement) : [void]

 Replaces the default json output wrapper with the sepecified replacement tag.
Paramters Type Required Descritpion
$open_replacement string true The replacement value for the default string at which all line contents are wrapped into.
default: <div style="margin-left:30px;">
$close_replacement string false The replacement value for the default closing tag.
default: </div ">
Json::setIndent('<span style="margin-left:30px;display:block;">', '</span>');

// Or you can use the auto tag close


Json::setBreak('<span style="margin-left:30px;">', '..');

::allowWrapper($option, $font_name) : [void]

 Wraps generated json data into a HTML div tag.
Paramters Type Required Descritpion
$option bool true If FALSE, prevents the generated code from being wrapped into a HTML div tag
default: true
$font_name string false This parameter works with the $option and can only be evaluated if the $option parameter is TRUE Replaces custom HTML div font
default: Courier New, Courier, monospace
//prevent json contend wrapping

Json::allowWrapper(false);

//wraps json content into a HTML div with font-family of proxima_nova_rgregular 

Json::allowWrapper(true, 'proxima_nova_rgregular');

Fair Usage


typical example of rendering data into a file
<?php  

 Json::setBreak("\n"); //or Json::setBreak('PHP_EOL');

 Json::setIndent("\t");
 Json::allowWrapper(false);
 
 $json = array(
    'first_name' => 'Foo',
    'last_name'  => 'Bar',
 );
 
 file_put_contents('filenName.ext', Json::encode($json)); 

Subscribe to our Newsletter for latest news.

© ghostff 2014 - All rights reserved.