Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Func] Convert Objects to/from JSON-like Strings


  • Please log in to reply
3 replies to this topic
VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
Recently, a particular user searched in vain for a fast, simple way to convert JSON text into AHK objects on the fly. The idea piqued my interest, so I wrote this library (packaged as separate functions, so as not to interfere with any other "JSON" library you might have).

The linked zip file contains two .ahk files which you may copy to the Lib folder of your choice or directly into your scripts. Kindly observe the included license.txt.

                AHK-Lib-JSON.zip

I have not extensively tested these functions, so let me know if you find any bugs. The "stringify" function's output does NOT conform to the JSON specification (www.json.org), see readme.txt. Also, that function may rearrange the order of a non-array object's members.

Enjoy!

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

Thanks for this VxE.

It will also help me port it to other languages.


Posted Image

don't duplicate, iterate!


nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
See also LSON

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
TLM, you're welcome! Just out of curiosity, which other languages don't already have JSON deserializing (either built-in or as a public library)?


My deserializer differs from similar functions in that it does not invoke javascript, it does not use regex, it does not use recursion, and it does not call other user-defined functions.


These differences translate to speed. My tests show that JSON_ToObj (my function) is more than twice as fast as LSON_Deserialize (link in nimda's post above).