Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

JSON UDF Library (fully RFC4627-compliant)


  • Please log in to reply
15 replies to this topic

#1 Gabriel13

Gabriel13

    Seeker

  • Active Members
  • 9 posts

Posted 18 October 2009 - 06:50 AM

I started this JSON library back in late 2007, because I needed a way to pass structured data between Javascript HTAs and AutoIt3 scripts. I've recently worked to get it in good enough shape to share with the AutoIt community, and while it still needs a bit more polish, the core functionality should be solid.

Three files are included in the attached archive:

  • JSON.au3 – the UDF Library itself. It includes a (very large) comment section at the top, which should provide sufficient documentation on how the various data types are encoded and decoded, as well as the various Javascript-based decoding extensions allowed (single-quoted strings, comments, etc.).
  • JSON_Translate.au3 provides example functions for the powerful "translator" functionality. Translator functions can be used for both encoding and decoding. When encoding, it allows you to recursively convert your data from its normal format into the required data structures used by the JSON UDF library. When decoding, it allows you to convert the data in the opposite direction – from the JSON UDF library's data structures to the data structures your program normally uses. To use this functionality, you'll need to write your own translator functions, but this allows you to meet the custom needs of your own programs without having to hack JSON.au3. The examples provided in this file can probably be improved, but show one way of translating AutoIt-specific data types (such as binary and hwnd) back and forth.
  • testJSON.au3 provides some quick examples of regular and translator-enhanced usage, which also help verify that encoding and decoding are working properly. These tests may not be comprehensive.

The area needing the most work is error-handling. JSON data structures can be deeply nested, and it's tricky to make sure errors "bubble up" properly in AutoIt. (Plus, I need a flowchart to keep track of all my setError() numbers!)

Thanks to Douglas Crockford of JSON.org, whose 2005 Javascript code for JSON encoding and decoding provided an excellent starting point for this library.

Edit: updated to 0.9.1 on 2009-10-19
  • substantially updated opening comments
  • encoding: implemented logic to return "warning" @error flags for unsupported variable types & JSON objects with non-unique key strings
  • other minor fixes & tweaks

Attached Files

  • Attached File  JSON.7z   9.43KB   3929 downloads

Edited by Gabriel13, 20 October 2009 - 12:22 AM.

  • nobody0, Wiliat87 and TypeIt like this







#2 tomjoad

tomjoad

    Seeker

  • New Members
  • 1 posts

Posted 19 October 2009 - 06:16 PM

Thank You for sharing. Just what I was looking for.

;-)

/t

#3 Gabriel13

Gabriel13

    Seeker

  • Active Members
  • 9 posts

Posted 20 October 2009 - 03:17 AM

You're welcome! I'd appreciate any feedback you have on it.

In particular, the use of 2D arrays to represent JSON objects, how well does that work for everyone? I believe they should be compatible with the 2D array handling routines from the standard Array.au3 UDF library.

I'll be working on adding comments for each function next – hopefully, that'll help me get my error codes organized.

Slightly edited for clarity.

Edited by Gabriel13, 21 October 2009 - 05:24 AM.


#4 P388l3s

P388l3s

    Seeker

  • Active Members
  • 14 posts

Posted 22 December 2009 - 03:55 PM

You're welcome! I'd appreciate any feedback you have on it.

In particular, the use of 2D arrays to represent JSON objects, how well does that work for everyone? I believe they should be compatible with the 2D array handling routines from the standard Array.au3 UDF library.

I'll be working on adding comments for each function next – hopefully, that'll help me get my error codes organized.

Slightly edited for clarity.


Thanks, a project I'm working spits out JSON formatted files, hopefully I'll be able to use your UDf to make working with these files easier. I like easier!

I tip my hat to you. ;)

#5 LaiTTNet

LaiTTNet

    Seeker

  • New Members
  • 1 posts

Posted 13 January 2010 - 02:08 PM

Great job :D

#6 James

James

    Universalist

  • MVPs
  • 10,353 posts

Posted 13 January 2010 - 09:25 PM

Hmm, this would be useful to use to attempt to implement oAuth!

#7 schnibble

schnibble

    Seeker

  • Active Members
  • 26 posts

Posted 21 May 2011 - 02:33 PM

is it possible to make this http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/serialization/json/?r=83 with outoit?
specially the JSONDecoder.as

thanks and best regards

#8 1RV34

1RV34

    Seeker

  • Active Members
  • 46 posts

Posted 17 February 2012 - 08:41 AM

It seems like this is the only good JSON AutoIt UDF I can find, but how do I cycle through the values?

Edited by 1RV34, 17 February 2012 - 09:03 AM.

MsgBox(0x40040, "", "Hello Forum!")

#9 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 17,557 posts

Posted 02 March 2012 - 01:33 PM

This version fixes problems with undeclared variables and variables not being used amongst other issues encountered e.g. encoding of the au3 files.

Note: I haven't tested, just fixed the errors.
Attached File  JSON.zip   10.63KB   1012 downloads

Edited by guinness, 09 June 2012 - 09:28 AM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()LockFile()Mapping CtrlIDsParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...
Updated: 26/09/2014


#10 squadjot

squadjot

    Seeker

  • Active Members
  • 31 posts

Posted 05 July 2012 - 03:50 PM

Thanks a bunch

#11 nobody0

nobody0

    Seeker

  • Normal Members
  • 1 posts

Posted 11 November 2012 - 12:18 AM

Thanks for this great work i have been looking for a proper JSON library for 2 days before i found this one!

To show my thanks heres a little helper function addition, its purpose is to enable to query the json object in an easier way like "key1.key2", i hope this helps someone =)

AutoIt         
#include <Array.au3> #include "JSON.au3" #include "JSON_Translate.au3" ; examples of translator functions, includes JSON_pack and JSON_unpack Func _JSONGet($json, $path, $seperator = ".") Local $seperatorPos,$current,$next,$l $seperatorPos = StringInStr($path, $seperator) If $seperatorPos > 0 Then $current = StringLeft($path, $seperatorPos - 1) $next = StringTrimLeft($path, $seperatorPos + StringLen($seperator) - 1) Else $current = $path $next = "" EndIf If _JSONIsObject($json) Then $l = UBound($json, 1) For $i = 0 To $l - 1 If $json[$i][0] == $current Then If $next == "" Then return $json[$i][1] Else return _JSONGet($json[$i][1], $next, $seperator) EndIf EndIf Next ElseIf IsArray($json) And UBound($json, 0) == 1 And UBound($json, 1) > $current Then If $next == "" Then return $json[$current] Else return _JSONGet($json[$current], $next, $seperator) EndIf EndIf return $_JSONNull EndFunc ;create an json object to test Local $json = _JSONDecode('{"test":{"x":[11,22,{"y":55}]}}') ;query this object Local $result = _JSONGet($json, "test.x.2") ;query the previous result $result = _JSONGet($result, "y") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF) _ArrayDisplay($result) ;query this object using another seperator $result = _JSONGet($json, "test->x->2->y", "->") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF) ;query this object $result = _JSONGet($json, "test.x.2") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF & @CRLF) _ArrayDisplay($result) ;query this object $result = _JSONGet($json, "test.non.existent.key") ;output the result ConsoleWrite(_JSONIsNull($result) & @CRLF) ConsoleWrite($result & @CRLF & @CRLF)

Edited by nobody0, 11 November 2012 - 12:20 AM.


#12 yahaosoft

yahaosoft

    Seeker

  • Active Members
  • 18 posts

Posted 16 November 2012 - 10:19 AM

great job! i have used in my project,thank you for sharing it! :ILA2:
Thanksgiving...

#13 Danp2

Danp2

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 930 posts

Posted 16 November 2012 - 11:20 PM

I agree, and I recently used this in a project as well. nobody0's helper function was also a lifesaver and should be added to the main .au3 file.

One question:

I noticed that @error was 2 after a call to _JSONDecode(). This seems to be coming from the final line of __JSONSkipWhitespace(). Can someone explain the reasoning behind @error being set in this routine?

Thx!

#14 MrTWist

MrTWist

    Seeker

  • Active Members
  • 10 posts

Posted 21 November 2012 - 07:47 AM

Thanks for the effort, Gabriel!

I have found that the JSON UDF appears to choke on the following JSON returned from Google Books API, whereas other JSON parsers don't have issues.
Specifically, the JSON array for the "items" key is not parsed.


Plain Text         
{ "kind": "books#volumes", "totalItems": 1, "items": [ { "kind": "books#volume", "id": "1sCDyGWDgQsC", "etag": "ND3n2hIHVlg", "selfLink": "<a href='https://www.googleapis.com/books/v1/volumes/1sCDyGWDgQsC' class='bbc_url' title='External link' rel='nofollow external'>https://www.googleapis.com/books/v1/volumes/1sCDyGWDgQsC"</a>, "volumeInfo": { "title": "Excel 2007 Advanced Report Development", "authors": [      "Timothy Zapawa" ], "publisher": "Wiley", "publishedDate": "2007-03-26", "description": "Create powerful, innovative reporting solutions with Excel 2007! With this new book, which is significantly updated from the bestselling Excel 2003 version, author Timothy Zapawa provides you with in-depth coverage of Excel 2007’s enhanced reporting capabilities. Discover what you can do with PivotTable and Spreadsheet reports, enhanced conditional formatting and filtering functions, improved data access and management tools, and expanded OLAP features. With notes, tips, warnings, real-world examples, and how-to videos in every chapter, you'll soon be an expert on Excel report development!", "industryIdentifiers": [      {      "type": "ISBN_10",      "identifier": "0470046449"      },      {      "type": "ISBN_13",      "identifier": "9780470046449"      } ], "pageCount": 576, "printType": "BOOK", "categories": [      "Computers" ], "averageRating": 4.0, "ratingsCount": 1, "contentVersion": "preview-1.0.0", "imageLinks": {      "smallThumbnail": "<a href='http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=5&source=gbs_api' class='bbc_url' title='External link' rel='nofollow external'>http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=5&source=gbs_api"</a>,      "thumbnail": "<a href='http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=1&source=gbs_api' class='bbc_url' title='External link' rel='nofollow external'>http://bks4.books.google.com/books?id=1sCDyGWDgQsC&printsec=frontcover&img=1&zoom=1&source=gbs_api"</a> }, "language": "en", "previewLink": "<a href='http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&cd=1&source=gbs_api' class='bbc_url' title='External link' rel='nofollow external'>http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&cd=1&source=gbs_api"</a>, "infoLink": "<a href='http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&source=gbs_api' class='bbc_url' title='External link' rel='nofollow external'>http://books.google.com/books?id=1sCDyGWDgQsC&dq=isbn:9780470046449&hl=&source=gbs_api"</a>, "canonicalVolumeLink": "<a href='http://books.google.com/books/about/Excel_2007_Advanced_Report_Development.html?hl=&id=1sCDyGWDgQsC' class='bbc_url' title='External link' rel='nofollow external'>http://books.google.com/books/about/Excel_2007_Advanced_Report_Development.html?hl=&id=1sCDyGWDgQsC"</a> }, "saleInfo": { "country": "US", "saleability": "NOT_FOR_SALE", "isEbook": false }, "accessInfo": { "country": "US", "viewability": "NO_PAGES", "embeddable": false, "publicDomain": false, "textToSpeechPermission": "ALLOWED_FOR_ACCESSIBILITY", "epub": {      "isAvailable": false }, "pdf": {      "isAvailable": false }, "webReaderLink": "<a href='http://books.google.com/books/reader?id=1sCDyGWDgQsC&hl=&printsec=frontcover&output=reader&source=gbs_api' class='bbc_url' title='External link' rel='nofollow external'>http://books.google.com/books/reader?id=1sCDyGWDgQsC&hl=&printsec=frontcover&output=reader&source=gbs_api"</a>, "accessViewStatus": "NONE" }, "searchInfo": { "textSnippet": "In this book, Timothy Zapawa has provided you with in-depth coverage of Excel 2007&#39;s enhanced reporting capabilities." } } ] }

Edited by MrTWist, 21 November 2012 - 07:51 AM.


#15 robertcollier4

robertcollier4

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 28 March 2013 - 07:13 AM

To show my thanks heres a little helper function addition, its purpose is to enable to query the json object in an easier way like "key1.key2", i hope this helps someone =)

Thanks nobody0 - this helped me a lot. Here is a complementary _JSONSet() function I wrote to also set JSON entries in the dotsyntax.

AutoIt         
#include #include "JSON.au3" #include "JSON_Translate.au3" ; examples of translator functions, includes JSON_pack and JSON_unpack ;Expands upon _JSONGet from nobody0 ;<a href='http://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/#entry1030327' class='bbc_url' title=''>http://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/#entry1030327</a> Func _JSONSet($writeValue, ByRef $json, $path, $seperator = ".")     Local $seperatorPos, $current, $next, $l     $seperatorPos = StringInStr($path, $seperator)     If $seperatorPos > 0 Then         $current = StringLeft($path, $seperatorPos - 1)         $next = StringTrimLeft($path, $seperatorPos + StringLen($seperator) - 1)     Else         $current = $path         $next = ""     EndIf     If _JSONIsObject($json) Then         $l = UBound($json, 1)         Local $matchFound = False         For $i = 0 To $l - 1             If $json[$i][0] == $current Then                 $matchFound = True                 If $next == "" Then                     $json[$i][1] = $writeValue                     Return                 Else                     _JSONSet($writeValue, $json[$i][1], $next, $seperator)                     Return                 EndIf             EndIf         Next         If Not $matchFound Then             ReDim $json[UBound($json)+1][2]             If $next == "" Then                 $json[UBound($json)-1][0] = $current                 $json[UBound($json)-1][1] = $writeValue             Else                 $json[UBound($json)-1][0] = $current                 Local $newjsonobject[1][2]                 $newjsonobject[0][0] =""                 $newjsonobject[0][1] ='JSONObject'                 $json[UBound($json)-1][1] = $newjsonobject                 _JSONSet($writeValue, $json[UBound($json)-1][1], $next, $seperator)                 Return             EndIf         EndIf     ElseIf IsArray($json) And UBound($json, 0) == 1 Then         If UBound($json, 1) > $current Then             If $next == "" Then                 $json[$current]                 Return             Else                 _JSONSet($writeValue, $json[$current], $next, $seperator)                 Return             EndIf         Else             ReDim $json[$current+1]             Local $newjsonobject[1][2]             $newjsonobject[0][0] =""             $newjsonobject[0][1] ='JSONObject'             $json[$current] = $newjsonobject             If $next == "" Then                 $json[$current]                 Return             Else                 _JSONSet($writeValue, $json[$current], $next, $seperator)                 Return             EndIf         EndIf     Else         return $_JSONNull     EndIf EndFunc

Edited by robertcollier4, 28 March 2013 - 07:42 AM.


#16 vortexed

vortexed

    Seeker

  • Active Members
  • 27 posts

Posted 18 February 2014 - 03:28 AM

Thanks for sharing this! I will be using this to build a simple jquery grid to display on a website. This UDF will constantly run in a while loop with sleep timers comparing flat file modified date with current systems date and query a SQL server to build the Jason files if the date difference between file date and system date is 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users