Kernel::System::JSON

NAME

Kernel::System::JSON – the JSON wrapper lib

DESCRIPTION

Functions for encoding perl data structures to JSON.

PUBLIC INTERFACE

$Kernel::System::JSON::SortKeys

Implicitly turns on key sorting for Encode() method. Useful when the actual call of the method cannot be influenced, but predictable result is still expected (i.e. in the unit tests).

    $Kernel::System::JSON::SortKeys = 1;

$Kernel::System::JSON::Pretty

Implicitly turns on pretty print for the Encode() method. Useful when the actual call of the method cannot be influenced, but predictable result is still expected (i.e. in the unit tests).

    $Kernel::System::JSON::Pretty = 1;

new()

create a JSON object. Do not use it directly, instead use:

    my $JSONObject = $Kernel::OM->Get('Kernel::System::JSON');

Encode()

Encode a perl data structure to a JSON string.

    my $JSONString = $JSONObject->Encode(
        Data     => $Data,
        SortKeys => 1,          # (optional) (0|1) default 0, to sort the keys of the json data
        Pretty   => 1,          # (optional) (0|1) default 0, to pretty print
        NoQuotes => 1,          # (optional) (0|1) default 0, to remove trailing and trailing double quotes
    );

Decode()

Decode a JSON string to a perl data structure.

    my $PerlStructureScalar = $JSONObject->Decode(
        Data => $JSONString,
    );

IsValid()

Checks if the given string is a valid JSON.

    my $IsValid = $JSONObject->IsValid(
        Data => $JSONString,
    );

Returns

    1 - valid json string
    0 - invalid json string

True()

returns a constant that can be mapped to a boolean true value in JSON rather than a string with "true".

    my $TrueConstant = $JSONObject->True();

    my $TrueJS = $JSONObject->Encode(
        Data => $TrueConstant,
    );

This will return the string 'true'. If you pass the perl string 'true' to JSON, it will return '"true"' as a JavaScript string instead.

False()

like True(), but for a false boolean value.

Scroll to Top