$json_utf8 1.0.2

By ProIcons on Jun 22, 2013

$json_utf8 by Nikolas (ProIcons) -> Remake of $json by timi.
Date: 22/6/2013

So one day i was trying to parse some data with $json from Timi. That data were in different languages UTF8 Encoded.
So I was getting a result something like:
çâÂÂçàçâ¢çççÃÂçâ¢çÃÂ
ÃÂÃÂÃÂàÃÂâ¢ÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂ÷ÃÂâÂÂÃÂíÃÂâÂÂ
And ETC.
So I look into this problem since my mIRC was accepting utf-8 charsets. I Found out that: Scripting.FileSystemObject that timi
used with comobjects through JScript language, DOES NOT support UTF8. SO i started to thing a better solution into this. I Found

ADODB.Stream, and i tried to replace Timi's Objects to This... though it failed.. I Read some articles in stackoverflow, and other sites
that JScript is forbidden to open ADODB.Stream, because ADODB.Stream can alter files, and JScript is not permitted to change any
computer file. So studying how MSScriptControl.ScriptControl works, i found another support scripting language. VBScript.
So i rebuild whole $json using vbscript. Now it finaly Supports UTF8 and in the place of: ΞΞΉ Εκδικητές We have: Οι Εκδικητές
and in the place of: Χ”Χ Χ•Χ§ΧΧ™Χ we have: הנוקמים

So i was about to post it here, but then i though... Wait dude. I always needed some tweaks to this.
So i added 2 more features.
.nocache(): When you are getting a file over internet by default VBScript with Microsoft.XmlHttp, caching the file as a result when the file change, you are not able to see the differences. with .nocache() property you are allowed to bypass Microsoft.XmlHttp's cache by having always a different query.

.savetofile(): is another feature that it just gets the data from the remote file, and saves it to a local file in your hard drive disk. If file exists it will be get replaced and overwrited. With This Property the second parameter of the alias $json_utf8, must be local file. If not exists it will created , if it is i said, Overwrite and so on.

and i left the .count(). as it was...

Now what the difference with the old version on the type..?

The concept of the command is pretty much the same. But i added a small feature to alias' properties. you can call mass properties just by typing this:

$json_utf8(http://remote.server/remote.file,local.file).savetofile().nocache()

That wasn't really so hard, numtok, gettok, a loop and you are ready :D.

Anyway pretty much this is the concept of $json_utf8 is pretty much the same with the old one, except it supports utf8... and you have the ability for nocache, and for savetofile...

If you find any bug i'm here, feel free to tell me.

Versions:

1.0.0: Initial Version
1.0.1: Fixed bug on savetofile() causing bad format files.
1.0.2: Fixed bug on nocache() causing bad urls.

alias json_utf8 {
  if ($isid) {
    var %c = json_utf8,%x = 2,%str,%p,%v
    while (%x <= $0) {
      %p = $($+($,%x),2)
      if (%p == $null) { noop }
      elseif (%p isnum || $qt($noqt(%p)) == %p) { %str = $+(%str,[,%p,]) }
      else { %str = $+(%str,[',%p,']) }
      inc %x
    }
    if (!$com(%c)) {
      .comopen %c MSScriptControl.ScriptControl
      noop $com(%c,language,4,bstr*,VBScript)
      var %code Function filejson(json) $crlf Dim objStream, strData $crlf Set objStream = CreateObject("ADODB.Stream") $crlf $&
        objStream.CharSet = "utf-8" $crlf objStream.Open $crlf objStream.LoadFromFile(json) $crlf strData = objStream.ReadText() $crlf $&
        filejson = strData $crlf End Function $crlf Function http2json(url) $crlf Set http = CreateObject("Microsoft.XmlHttp") $crlf $&
        http.open "GET", url, FALSE $crlf http.send "" $crlf http2json=http.responseText $crlf End Function $crlf $&
        Function str2json(json,value) $crlf Set scriptControl = CreateObject("MSScriptControl.ScriptControl") $crlf $&
        scriptControl.Language = "JScript"  $crlf scriptControl.AddCode("x="& json & ";") $crlf str2json= scriptControl.Eval( "x"& value  ) $crlf  $&
        End Function $crlf Function get_json_from_file(json,value) $crlf get_json_from_file=str2json(filejson(json),value) $crlf End Function $crlf $&
        Function get_json_from_http(url,value) $crlf get_json_from_http=str2json(http2json(url),value) $crlf End Function $crlf $&
        Function save_json_from_http(url,loc) $crlf Set fso = CreateObject("Scripting.FileSystemObject") $crlf fullpath = fso.GetAbsolutePathName(loc) $crlf $&
        Dim objStream, strData $crlf Set objStream = CreateObject("ADODB.Stream") $crlf objStream.CharSet = "utf-8" $crlf objStream.Open $crlf $&
        objStream.WriteText http2json(url) $crlf objStream.SaveToFile fullpath, 2 $crlf save_json_from_http=fullpath $crlf End Function
      noop $com(%c,addcode,1,bstr*,%code)
    }
    var %i = 1
    var %save_to_file $false
    var %nocache $false
    while (%i <= $numtok($prop,46) ) {
      if ($gettok($prop,%i,46) == count()) { %str = %str $+ .length }
      if ($gettok($prop,%i,46) == nocache()) { var %nocache $true }
      if ($gettok($prop,%i,46) == savetofile()) { var %save_to_file $true }
      inc %i
    }

    if ($isfile($1)) {
      var %t $+(get_json_from_file,$chr(40),$qt($shortfn($replace($1,\,\\,;,\u003b))),$chr(44),$qt(%str),$chr(41)))
      if ($com(%c,eval,1,bstr*,%t))  return $com(%c).result 
    }
    elseif (http://* iswm $1) {
      if (%nocache) { var %url = $1 $+ $iif($chr(63) isin %url,&,?) $+ _ctime_json_= $+ $ctime $+ . $+ $ticks }
      else { var %url = $1 }
      if (!%save_to_file) { var %t $+(get_json_from_http,$chr(40),$qt($replace($qt(%url),;,\u003b)),$chr(44),$qt(%str),$chr(41))) }
      else { var %t $+(save_json_from_http,$chr(40),$qt($replace($qt(%url),;,\u003b)),$chr(44),$qt($2),$chr(41))) }
      if ($com(%c,eval,1,bstr*,%t))  return $com(%c).result (URL: %url )
    }
    else {

      var %t $+(str2json,$chr(40),$qt($replace($1,",')),$chr(44),$qt(%str),$chr(41))
      if ($com(%c,eval,1,bstr*,%t))  return $com(%c).result 
    }
  } 
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.