About JSON:
JSON (JavaScript Object Notation) is a lightweight data interchange format that is simple in syntax and supported by libraries or modules in various languages.
Because JSON is very small and very simple to parse, I often organize my configuration files into JSON format.
About Json::fast:
Introduction on http://modules.perl6.org/: A naive, but hopefully fast JSON parser; Drop-in replacement for Json::tiny
A simple, fast JSON parser; to replace Json::tiny
Originally wanted to do a perl6 FTP automatic upload applet, today spent half a day to write half, later found that Perl6 no ready-made FTP library, but in the process of writing to understand the Json::fast
Usage of the module: It's worth it.
Installation of Json::fast
PERL6 is the use of panda to manage modules, with the Panda installation module and Fedora installation software as simple as a single command can be resolved
1 Panda Install Json::fast
I do not know is the cause of panda, or my problem, panda reaction on my machine very slow, although my CPU is too rotten point.
Once installed, you can use the following command to test if the installation was successful
" Use json::fast "
If the Json::fast module does not exist, execution will error
Use of Json::fast
First, we have a simple JSON file, Sample.json, with the file content as follows
{ "user-list": [ "username",
"OtherUser" ], "username": { "IP": "192.168.0.100", "port": "+", "pass": "Password", " Dir ":". ", " ext ":" " },
"OtherUser": {...//omit some configuration}}
The JSON file is simple and intended to be used as a configuration file for FTP upload applet
First, create a new file called fast-json.pl, write a main function, Perl6 's main function is more powerful
1 SubMAIN (STR:C (:$config-file) ="", *@file) {2 if+@file==0 {3Say"No File Argument";4 return;5 }6}
Then chmod +x fast-json.pl, execute it./fast-json.pl--help
Usage: ./fast-json.pl [-c|--config-file=<str>] [<file> ...]
Like a command in the shell, there are usage hints.
Explain these lines of code,
First line
A sub is the beginning of a function, or a subroutine, and main is a function that has the same effect as the C language, but if the main function is defined, it executes the global statement and finally executes main;
STR is a type in perl6, other similar types are int, bool, and so on, to get the type of a variable, you can use the What method, for example
$var. what;
This sentence will print out the type of $var;
$config-file represents the first parameter of the main function, the path to the configuration file, and ":" in front of the variable to make it a mutable named variable, so that it can be used as a setting for the script, preceded by
: C represents the short name of this setting; * @file, @ represents the array, * represents all the remaining parameters as members of the @file array, because it is not related to JSON parsing.
So we can test it this way.
./fast-json.pl--config-file=users.cfg XXX
Users.cfg is a JSON-formatted config file, xxx represents the file to be uploaded
Then, read the contents of the JSON file that needs parsing
1 my $json-path = IO::P ath.new ($config-file);2 3 if(! ($json-PATH.E &&$json-path.r)) {4Say"Config File not exist path, $json-path";5 Exit6 }7 my $json-slurp =$json-path.slurp ();
IO::P Ath is a built-in class that has some file manipulation-related functions
. E. R is used to determine if the file exists and whether it can be read
. Slurp is used to read the entire contents of a file
After that is the JSON file parsing, Json::fast module parsing is very simple,
1 my $json;2 3 try {4 $json= From-json ($json-slurp);5 CATCH {6 Default {7 "User Config has error".say ();8 "Stack----->".say ();9...Ten } One } A}
try{catch{}} is an exception handling in perl6, want to know can go to see perl6 revelation, if JSON parse error, here will receive an exception, and then ... Indicates that the stack information for the crash is printed
If the resolution is successful, the $json will represent the JSON file we have completed, if you print $json with say. What, you will find is a hash.
And then it's the JSON element, the array
1 my $user-list;2 3 if $json{'user-list'}:exists {4 $user-list =$json{'user-list'};5 6Say$user-list.Elems;7Say$user-list[0];8Say$user-list[1];9}
The first sentence checks if there are element user-list in the hash, and if there is an element removed and printed out
Username OtherUser
Because it is an array, you can use the. Elems to get the number of elements in the array, using the subscript to get a separate item
Then the same can get the elements
1 if $json { $user -list[0 ]}: exists my
$info = $json { $user -list[0 3 4 say $ Info { " IP " }; 5 }
Then the element is stored or hashed, so the next step is simple, like taking the elements of $json $info elements can be. The example here will print out the value of the username IP
192.168.0.100
So, the json::fast of the analysis of the use of this, the specific use can also go to Json::fast's homepage to see
https://github.com/timo/json_fast/
Use of JSON parsing for perl6 json::fast modules