Snippets Project Page
Author: Dazuz
Added: 2y
Updated: Never
mIRC: 7.53
Hits: 514
Downloads: 8
Review: westor
Size: 6.86KB
2 0
Login to vote.
json2hash
v1.0
A simple pure mSL script to convert JSON data into a mIRC hash table.
To use the script, store your JSON data in a &bvar, and call "/json2hash &bvar hashtable" alias.
You will manually have to /hfree the hash table if and when you want/need.
Example:
//bset -t & 1 [{"array"1,2,3],"key":"string"},{"boolean":true,"number":123,"object":{"array"true,false,null]}}] | json2hash & json
This will create a hash table called "json," which will contain the data:
//noop $hfind(json,*,0,w,echo -a $1 = $hget(json,$1))
Download
JSON
▲ Review
▼ Source
/* json2hash for mIRC v7.53+ Version: 1.0 Coded by: Dazuz @ QuakeNet Usage: /json2hash <bvar> <hash table> OR $json2hash(<bvar>,<hash table>) bvar contains the JSON data, and hash table is the name of the hash table where the data will be output. $json2hash will return 1 on success, and 0 on failure. Example: //bset -t & 1 [{"array":[1,2,3],"key":"string"},{"boolean":true,"number":123,"object":{"array":[true,false,null]}}] | json2hash & json This will create a hash table called "json," which will contain the data: //noop $hfind(json,*,0,w,echo -a $1 = $hget(json,$1)) */ on *:load: { if ($version < 7.53) { echo $color(info) -at * Unsupported mIRC version, please update to $v2 or newer. unload -rs $qt($script) } } on *:unload:unset %json.path %json.cache alias json2hash { if (!$2) || (&* !iswm $1) return 0 set -l %bvar $1 set -l %hash $2 if ($hget(%hash)) hfree %hash hmake %hash set -l %pos 1 set -l %index set -l %start set -l %temp set -l %key set -l %m $maxlenl set -l %32 $chr(32) set -l %1 $chr(1) %json.cache = while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 91) { %index = 0 %json.path = $instok(a,i 0,1,1) goto arraystart } if ($v1 == 123) { %json.path = o goto object } goto end :key inc %pos %start = %pos while ($bfind(%bvar,%pos,34)) { %pos = $v1 - 1 %temp = %pos while ($bvar(%bvar,%temp) == 92) dec %temp %temp = %pos - %temp if (!%temp) || (2 // %temp) break inc %pos 2 } %key = $left($bvar(%bvar,%start - %pos).text,%m) inc %pos 2 while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 34) goto string if ($v1 == 123) { if (o* iswm %json.path) %json.path = $instok(%json.path,o %key,1,1) else %json.path = $instok($puttok(%json.path,i %index,1,1),o %key,1,1) goto object $json.cache } if ($v1 isnum 48-57) || ($v1 == 45) goto number if ($v1 == 91) { if (o* iswm %json.path) %json.path = $+(i 0,%1,a %key,%1,%json.path) else %json.path = $+(i 0,%1,a %key,%1,$puttok(%json.path,i %index,1,1)) %index = 0 goto arraystart $json.arraycache } goto $v1 :string inc %pos %start = %pos while ($bfind(%bvar,%pos,34)) { %pos = $v1 - 1 %temp = %pos while ($bvar(%bvar,%temp) == 92) dec %temp %temp = %pos - %temp if (!%temp) || (2 // %temp) break inc %pos 2 } hadd %hash $left($replacex(%json.cache %key,%32,_) $bvar(%bvar,%start - %pos).text,%m) inc %pos :next inc %pos while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 44) { inc %pos while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 34) goto key goto end } if ($v1 == 125) { %json.path = $gettok(%json.path,2-,1) if (o* iswm %json.path) goto next $json.cache %index = $gettok($gettok(%json.path,1,1),2,32) goto arraynext $json.arraycache } goto end :arraystart inc %pos while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 34) goto arraykey if ($v1 == 123) { inc %index %json.path = $instok($puttok(%json.path,i %index,1,1),o,1,1) goto object $json.cache } if ($v1 == 93) { %json.path = $gettok(%json.path,3-,1) if (o* iswm %json.path) goto next $json.cache %index = $gettok($gettok(%json.path,1,1),2,32) goto arraynext $json.arraycache } if ($v1 isnum 48-57) || ($v1 == 45) goto arraynumber if ($v1 == 91) { %json.path = $+(i 0,%1,a,%1,$puttok(%json.path,i %index,1,1)) %index = 0 goto arraystart $json.arraycache } goto $chr($v1) :number %start = %pos while ($bvar(%bvar,%pos) isnum 45-57) inc %pos dec %pos if ($bvar(%bvar,%start - %pos).text isnum) { hadd %hash $left($replacex(%json.cache %key,%32,_) $v1,%m) goto next } goto end :arraykey inc %pos %start = %pos while ($bfind(%bvar,%pos,34)) { %pos = $v1 - 1 %temp = %pos while ($bvar(%bvar,%temp) == 92) dec %temp %temp = %pos - %temp if (!%temp) || (2 // %temp) break inc %pos 2 } inc %index hadd %hash $left($replacex(%json.cache %index,%32,_) $bvar(%bvar,%start - %pos).text,%m) inc %pos :arraynext inc %pos while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 44) goto arraystart if ($v1 == 93) { %json.path = $gettok(%json.path,3-,1) if (o* iswm %json.path) goto next $json.cache %index = $gettok($gettok(%json.path,1,1),2,32) goto arraynext $json.arraycache } goto end :object inc %pos while ($istok(32 10 13 9 58,$bvar(%bvar,%pos),32)) inc %pos if ($bvar(%bvar,%pos) == 34) goto key if ($v1 == 125) { %json.path = $gettok(%json.path,2-,1) if (o* iswm %json.path) goto next $json.cache %index = $gettok($gettok(%json.path,1,1),2,32) goto arraynext $json.arraycache } goto end :102 inc %pos 4 hadd %hash $replacex(%json.cache %key,%32,_) false goto next :110 inc %pos 3 hadd %hash $replacex(%json.cache %key,%32,_) null goto next :116 inc %pos 3 hadd %hash $replacex(%json.cache %key,%32,_) true goto next :arraynumber %start = %pos while ($bvar(%bvar,%pos) isnum 45-57) inc %pos dec %pos if ($bvar(%bvar,%start - %pos).text isnum) { inc %index hadd %hash $left($replacex(%json.cache %index,%32,_) $v1,%m) goto arraynext } goto end :f inc %pos 4 inc %index hadd %hash $replacex(%json.cache %index,%32,_) false goto arraynext :n inc %pos 3 inc %index hadd %hash $replacex(%json.cache %index,%32,_) null goto arraynext :t inc %pos 3 inc %index hadd %hash $replacex(%json.cache %index,%32,_) true goto arraynext :error reseterror unset %json.path %json.cache return 0 :end unset %json.path %json.cache return 1 } alias -l json.cache { %json.cache = tokenize 1 %json.path json.build $* } alias -l json.arraycache { %json.cache = tokenize 1 $gettok(%json.path,2-,1) json.build $* } alias -l json.build %json.cache = $2- %json.cache
Changelog:
0
0