{
    "mode": "info",
    "parameter": "Cpanel::JSON::XS::Type",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/Cpanel%3A%3AJSON%3A%3AXS%3A%3AType/json",
    "generated": "2026-07-05T13:50:50Z",
    "synopsis": "use Cpanel::JSON::XS;\nuse Cpanel::JSON::XS::Type;\nencodejson([10, \"10\", 10.25], [JSONTYPEINT, JSONTYPEINT, JSONTYPESTRING]);\n# '[10,10,\"10.25\"]'\nencodejson([10, \"10\", 10.25], jsontypearrayof(JSONTYPEINT));\n# '[10,10,10]'\nencodejson(1, JSONTYPEBOOL);\n# 'true'\nmy $perlstruct = { key1 => 1, key2 => \"2\", key3 => 1 };\nmy $typespec = { key1 => JSONTYPESTRING, key2 => JSONTYPEINT, key3 => JSONTYPEBOOL };\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":\"1\",\"key2\":2,\"key3\":true}'\nmy $perlstruct = { key1 => \"value1\", key2 => \"value2\", key3 => 0, key4 => 1, key5 => \"string\", key6 => \"string2\" };\nmy $typespec = jsontypehashof(JSONTYPESTRING);\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"0\",\"key4\":\"1\",\"key5\":\"string\",\"key6\":\"string2\"}'\nmy $perlstruct = { key1 => { key2 => [ 10, \"10\", 10.6 ] }, key3 => \"10.5\" };\nmy $typespec = { key1 => jsontypeanyof(JSONTYPEFLOAT, jsontypehashof(jsontypearrayof(JSONTYPEINT))), key3 => JSONTYPEFLOAT };\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":{\"key2\":[10,10,10]},\"key3\":10.5}'\nmy $value = decodejson('false', 1, my $type);\n# $value is 0 and $type is JSONTYPEBOOL\nmy $value = decodejson('0', 1, my $type);\n# $value is 0 and $type is JSONTYPEINT\nmy $value = decodejson('\"0\"', 1, my $type);\n# $value is 0 and $type is JSONTYPESTRING\nmy $jsonstring = '{\"key1\":{\"key2\":[10,\"10\",10.6]},\"key3\":\"10.5\"}';\nmy $perlstruct = decodejson($jsonstring, 0, my $typespec);\n# $perlstruct is { key1 => { key2 => [ 10, 10, 10.6 ] }, key3 => 10.5 }\n# $typespec is { key1 => { key2 => [ JSONTYPEINT, JSONTYPESTRING, JSONTYPEFLOAT ] }, key3 => JSONTYPESTRING }",
    "sections": {
        "NAME": {
            "content": "Cpanel::JSON::XS::Type - Type support for JSON encode\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Cpanel::JSON::XS;\nuse Cpanel::JSON::XS::Type;\n\nencodejson([10, \"10\", 10.25], [JSONTYPEINT, JSONTYPEINT, JSONTYPESTRING]);\n# '[10,10,\"10.25\"]'\n\nencodejson([10, \"10\", 10.25], jsontypearrayof(JSONTYPEINT));\n# '[10,10,10]'\n\nencodejson(1, JSONTYPEBOOL);\n# 'true'\n\nmy $perlstruct = { key1 => 1, key2 => \"2\", key3 => 1 };\nmy $typespec = { key1 => JSONTYPESTRING, key2 => JSONTYPEINT, key3 => JSONTYPEBOOL };\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":\"1\",\"key2\":2,\"key3\":true}'\n\nmy $perlstruct = { key1 => \"value1\", key2 => \"value2\", key3 => 0, key4 => 1, key5 => \"string\", key6 => \"string2\" };\nmy $typespec = jsontypehashof(JSONTYPESTRING);\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"0\",\"key4\":\"1\",\"key5\":\"string\",\"key6\":\"string2\"}'\n\nmy $perlstruct = { key1 => { key2 => [ 10, \"10\", 10.6 ] }, key3 => \"10.5\" };\nmy $typespec = { key1 => jsontypeanyof(JSONTYPEFLOAT, jsontypehashof(jsontypearrayof(JSONTYPEINT))), key3 => JSONTYPEFLOAT };\nmy $jsonstring = encodejson($perlstruct, $typespec);\n# '{\"key1\":{\"key2\":[10,10,10]},\"key3\":10.5}'\n\nmy $value = decodejson('false', 1, my $type);\n# $value is 0 and $type is JSONTYPEBOOL\n\nmy $value = decodejson('0', 1, my $type);\n# $value is 0 and $type is JSONTYPEINT\n\nmy $value = decodejson('\"0\"', 1, my $type);\n# $value is 0 and $type is JSONTYPESTRING\n\nmy $jsonstring = '{\"key1\":{\"key2\":[10,\"10\",10.6]},\"key3\":\"10.5\"}';\nmy $perlstruct = decodejson($jsonstring, 0, my $typespec);\n# $perlstruct is { key1 => { key2 => [ 10, 10, 10.6 ] }, key3 => 10.5 }\n# $typespec is { key1 => { key2 => [ JSONTYPEINT, JSONTYPESTRING, JSONTYPEFLOAT ] }, key3 => JSONTYPESTRING }\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides stable JSON type support for the Cpanel::JSON::XS\nencoder which doesn't depend on any internal perl scalar flags or\ncharacteristics. Also it provides real JSON types for Cpanel::JSON::XS\ndecoder.\n\nIn most cases perl structures passed to encodejson come from other\nfunctions or from other modules and caller of Cpanel::JSON::XS module\ndoes not have control of internals or they are subject of change. So it\nis not easy to support enforcing types as described in the simple\nscalars section.\n\nFor services based on JSON contents it is sometimes needed to correctly\nprocess and enforce JSON types.\n\nThe function decodejson takes optional third scalar parameter and\nfills it with specification of json types.\n\nThe function encodejson takes a perl structure as its input and\noptionally also a json type specification in the second parameter.\n\nIf the specification is not provided (or is undef) internal perl scalar\nflags are used for the resulting JSON type. The internal flags can be\nchanged by perl itself, but also by external modules. Which means that\ntypes in resulting JSON string aren't stable. Specially it does not\nwork reliable for dual vars and scalars which were used in both numeric\nand string operations. See simple scalars.\n\nTo enforce that specification is always provided use \"requiretypes\".\nIn this case when \"encode\" is called without second argument (or is\nundef) then it croaks. It applies recursively for all sub-structures.\n\nJSON type specification for scalars:\nJSONTYPEBOOL\nIt enforces JSON boolean in resulting JSON, i.e. either \"true\" or\n\"false\". For determining whether the scalar passed to the encoder\nis true, standard perl boolean logic is used.\n\nJSONTYPEINT\nIt enforces JSON number without fraction part in the resulting\nJSON.  Equivalent of perl function int is used for conversion.\n\nJSONTYPEFLOAT\nIt enforces JSON number with fraction part in the resulting JSON.\nEquivalent of perl operation +0 is used for conversion.\n\nJSONTYPESTRING\nIt enforces JSON string type in the resulting JSON.\n\nJSONTYPENULL\nIt represents JSON \"null\" value. Makes sense only when passing\nperl's \"undef\" value.\n\nFor each type, there also exists a type with the suffix \"ORNULL\"\nwhich encodes perl's \"undef\" into JSON \"null\". Without type with suffix\n\"ORNULL\" perl's \"undef\" is converted to specific type according to\nabove rules.\n\nJSON type specification for arrays:\n[...]\nThe array must contain the same number of elements as in the perl\narray passed for encoding. Each element of the array describes the\nJSON type which is enforced for the corresponding element of the\nperl array.\n\njsontypearrayof\nThis function takes a JSON type specification as its argument which\nis enforced for every element of the passed perl array.\n\nJSON type specification for hashes:\n{...}\nEach hash value for corresponding key describes the JSON type\nspecification for values of passed perl hash structure. Keys in\nhash which are not present in passed perl hash structure are simple\nignored and not used.\n\njsontypehashof\nThis function takes a JSON type specification as its argument which\nis enforced for every value of passed perl hash structure.\n\nJSON type specification for alternatives:\njsontypeanyof\nThis function takes a list of JSON type alternative specifications\n(maximally one scalar, one array, and one hash) as its input and\nthe JSON encoder chooses one that matches.\n\njsontypenulloranyof\nLike \"jsontypeanyof\", but scalar can be only perl's \"undef\".\n\nRecursive specifications\njsontypeweaken\nThis function can be used as an argument for \"jsontypearrayof\",\n\"jsontypehashof\" or \"jsontypeanyof\" functions to create weak\nreferences suitable for complicated recursive structures. It\ndepends on the weaken function from Scalar::Util module.  See\nfollowing example:\n\nmy $struct = {\ntype => JSONTYPESTRING,\narray => jsontypearrayof(JSONTYPEINT),\n};\n$struct->{recursive} = jsontypeanyof(\njsontypeweaken($struct),\njsontypearrayof(JSONTYPESTRING),\n);\n\nIf you want to encode all perl scalars to JSON string types despite\nhow complicated is input perl structure you can define JSON type\nspecification for alternatives recursively. It could be defined as:\n\nmy $type = jsontypeanyof();\n$type->[0] = JSONTYPESTRINGORNULL;\n$type->[1] = jsontypearrayof(jsontypeweaken($type));\n$type->[2] = jsontypehashof(jsontypeweaken($type));\n\nprint encodejson([ 10, \"10\", { key => 10 } ], $type);\n# [\"10\",\"10\",{\"key\":\"10\"}]\n\nAn alternative solution for encoding all scalars to JSON strings is\nto use \"typeallstring\" method of Cpanel::JSON::XS itself:\n\nmy $json = Cpanel::JSON::XS->new->typeallstring;\nprint $json->encode([ 10, \"10\", { key => 10 } ]);\n# [\"10\",\"10\",{\"key\":\"10\"}]\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Pali <pali@cpan.org>\n\nCOPYRIGHT & LICENSE\nCopyright (c) 2017, GoodData Corporation. All rights reserved.\n\nThis module is available under the same licences as perl, the Artistic\nlicense and the GPL.\n\nperl v5.34.0                      2025-09-12                     XS::Type(3pm)",
            "subsections": []
        }
    },
    "summary": "Cpanel::JSON::XS::Type - Type support for JSON encode",
    "flags": [],
    "examples": [],
    "see_also": []
}