The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WJSON - Write JSON with simplicities

VERSION

Version 0.08

SYNOPSIS

    use WJSON;
    
    my $json = new WJSON;
    $json->Object(
        key_1 => 'value_1',
        key_2 => 'value_2',
        key_3 => 'value_3',
    );
    print $json->Print;
    

ATTRIBUTES

encoding

    set encoding, default utf-8
    

variable

    set variable and return var variable = {}
    

ordering

    set ordering with 1 or true and return json order by position
    

METHODS

Open

    Open object or array
    

Close

    Close object or array, set 1 to separates of reference, set 2 to add value within of array,
    set 3 is 1 and 2 together

Object

    Create object with prototyped set of key/value (properties)

HashObject

    Create hash object with prototyped set of {key/value} (properties)

Array

    Create array, set of value
    Return "application/json"
    

HeaderJS

    Return "application/javascript"
    

HeaderCGI

    Return "Content-type: application/json\n\n";
    

HeaderJSCGI

    Return "Content-type: application/javascript\n\n";

Print

    Print JSON

EXAMPLES

Example 1

    my $json = new WJSON(encoding => 'iso-8859-1');
    $json->Array('value_1', 'value_2', 'value_3');
    print $json->Print;
    

Result JSON

    ["value_1", "value_2", "value_3"]
    

Example 2

    my $json = new WJSON;
    $json->encoding('iso-8859-1');
    $json->Object(
        key_1 => 'value_1',
        key_2 => 'value_2',
        key_3 => 'value_3',
    );
    print $json->Print;
    

Result JSON

    {
        "key_3": "value_3",
        "key_1": "value_1",
        "key_2": "value_2"
    }
    

Example 3

    my $json = new WJSON;
    $json->HashObject(
        {
            key_1 => 'value_1',
            key_2 => 'value_2',
            key_3 => 'value_3',
        }
    );
    print $json->Print;
    

Result JSON

    {
        "key_3": "value_3",
        "key_1": "value_1",
        "key_2": "value_2"
    }
    

Example 4

    my $json = new WJSON;
    $json->Open('Data');
        $json->Object(
            key_1 => 'value_1',
            key_2 => 'value_2',
            key_3 => 'value_3',
        );
    $json->Close;
    print $json->Print;
    

Result JSON

    {
        "Data": {
            "key_3": "value_3",
            "key_1": "value_1",
            "key_2": "value_2"
        }
    }
    

Example 5

    my $json = new WJSON;
    $json->Open('Data');
        $json->Object(
            key_1 => 'value_1',
            key_2 => 'value_2',
            key_3 => 'value_3',
        );
        $json->Object(
            key_1 => 'value_1',
            key_2 => 'value_2',
            key_3 => 'value_3',
        );
    $json->Close;
    print $json->Print;
    

Result JSON

    {
        "Data": [{
            "key_3": "value_3",
            "key_1": "value_1",
            "key_2": "value_2"
        }, {
            "key_3": "value_3",
            "key_1": "value_1",
  ut          "key_2": "value_2"
        }]
    }
    

Example 6

    my $json = new WJSON;
    $json->Open('Data');
        $json->Array('value_1', 'value_2', 'value_3');
    $json->Close;
    print $json->Print;
    

Result JSON

    {
        "Data": ["value_1", "value_2", "value_3"]
    }
    

Example 7

    my $json = new WJSON;
    $json->Object(
        key_1 => 'value_1',
        key_2 => 'value_2',
        key_3 => 'value_3',
    );
    $json->Object(
        key_1 => 'value_1',
        key_2 => 'value_2',
        key_3 => 'value_3',
    );
    $json->Open('Data');
        $json->Open('SubData');
            $json->Object(
                key_1 => 'value_1',
                key_2 => 'value_2',
                key_3 => 'value_3',
            );
            $json->Object(
                key_1 => 'value_1',
                key_2 => 'value_2',
                key_3 => 'value_3',
            );
        $json->Close;
        $json->Array(['value_1', 'value_2', 'value_3'], ['value_4', 'value_5']);
        $json->Array(['value_6', 'value_7']);
    $json->Close;
    $json->Array(['value_1', 'value_2', 'value_3'], ['value_4', 'value_5']);
    print $json->Print;
    

Result JSON

    [{
            "key_3": "value_3",
            "key_1": "value_1",
            "key_2": "value_2"
        }, {
            "key_3": "value_3",
            "key_1": "value_1",
            "key_2": "value_2",
            "Data": [{
                    "SubData": [{
                        "key_3": "value_3",
                        "key_1": "value_1",
                        "key_2": "value_2"
                    }, {
                        "key_3": "value_3",
                        "key_1": "value_1",
                        "key_2": "value_2"
                    }]
                },
                ["value_1", "value_2", "value_3"],
                ["value_4", "value_5"], "value_6", "value_7"
            ]
        },
        ["value_1", "value_2", "value_3"],
        ["value_4", "value_5"]
    ]
    

Example 8

    my $json = new WJSON;
    $json->variable('json');
    $json->Object(
        key_1 => 'Formulário',
        key_2 => 'value_2',
        key_3 => 'value_3',
    );
    print $json->Print;
    

Result JSON

    var json = {
        "key_3": "value_3",
        "key_1": "Formulário",
        "key_2": "value_2"
    };

Example 9

    my $json = new WJSON;
    $json->Open('glossary');
        $json->Object(
            title => 'example glossary'
        );
        $json->Open('GlossDiv');
            $json->Object(
                title => 'S'
            );
            $json->Open('GlossList');
                $json->Object(
                    GlossSee => 'markup'
                );
                $json->Open('GlossEntry');
                    $json->Object(
                        ID => 'SGML',
                        SortAs => 'SGML',
                        GlossTerm => 'Standard Generalized Markup Language',
                        Acronym => 'SGML',
                        Abbrev => 'ISO 8879:1986',
                    );
                    $json->Open('GlossDef');
                        $json->Object(
                            para => 'A meta-markup language, used to create markup languages such as DocBook.'
                        );
                        $json->Open('GlossSeeAlso');
                            $json->Array('GML', 'XML');
                        $json->Close;
                    $json->Close;
                $json->Close;
            $json->Close;
        $json->Close;
    $json->Close;
    print $json->HeaderCGI;
    print $json->Print;
    

Result JSON

    {
        "glossary": {
            "GlossDiv": {
                "GlossList": {
                    "GlossEntry": {
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossTerm": "Standard Generalized Markup Language",
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "Acronym": "SGML",
                        "Abbrev": "ISO 8879:1986"
                    },
                    "GlossSee": "markup"
                },
                "title": "S"
            },
            "title": "example glossary"
        }
    }
    

Example 10

    my $json = new WJSON;
    $json->Open('menu');
        $json->Object(
            id => 'file',
            value => 'File',
        );
        $json->Open('popup');
            $json->Open('menuitem');
                $json->Object(
                    {
                        value => 'New',
                        onclick => 'CreateNewDoc()'
                    },
                    {
                        value => 'Open',
                        onclick => 'OpenDoc()'
                    },
                    {
                        value => 'Close',
                        onclick => 'CloseDoc()'
                    }
                );
            $json->Close;
        $json->Close;
    $json->Close;
    print $json->HeaderCGI;
    print $json->Print;
    

Result JSON

    {
        "menu": {
            "value": "File",
            "popup": {
                "menuitem": [{
                    "value": "New",
                    "onclick": "CreateNewDoc()"
                }, {
                    "value": "Open",
                    "onclick": "OpenDoc()"
                }, {
                    "value": "Close",
                    "onclick": "CloseDoc()"
                }]
            },
            "id": "file"
        }
    }

AUTHOR

Lucas Tiago de Moraes, <lucastiagodemoraes@gmail.com>

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc WJSON

You can also look for information at:

Github

https://github.com/lucas1/WJSON

LICENSE AND COPYRIGHT

Copyright 2014 Lucas Tiago de Moraes.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.