SerDes() - AHK object (de)serialization [v1.1 and v2.0-a049]

Post your working scripts, libraries and tools for AHK v1.1 and older
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

SerDes() - AHK object (de)serialization [v1.1 and v2.0-a049]

08 Aug 2014, 23:20

SerDes

Another Serialize / De-serialize function

Features:
  • Supports object references including circular refs. See remarks
  • Load input from string or file (de-serialize)
  • Dump object to string or file (serialize)
  • Uses AHK's escape sequence specifications (unlike JSON which uses \)
Syntax:
Serialize - See README -> Serilaize for parameter details OR check documentation within source code

Code: Select all

str           := SerDes( obj )
bytes_written := SerDes( obj [, outfile ] )
De-serialize - README -> De-serilaize for parameter details

Code: Select all

obj := SerDes( src )
obj := SerDes( file )
Example:

Code: Select all

#Include SerDes.ahk

ref := ["Hello"]
obj := [{"Hello":"Hello`nWorld"}, {(ref):"World"}, ref] ;// Object to serialize

;          1 2                         3 4
;// Shows '[ {"Hello":"Hello`nWorld"}, { ["Hello"]:"World"}, $4]'
MsgBox % out := SerDes(obj) ;// Serialize and display output

obj := SerDes(out)          ;// De-serialize
MsgBox % obj[1].Hello       ;// displays "Hello`nWorld" -> Escape sequence
MsgBox % obj[2][obj[3]]     ;// displays "World" -> Object reference at work
return
Source: GitHub Repo

Remarks:
v2 Support - Due to the changes in objects built-in functions, this wont work for A_AhkVersion < v2.0-a049
bobc119
Posts: 23
Joined: 10 Jan 2014, 17:02

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

03 Sep 2014, 21:11

Coco, thank you for your excellent work!

Since you created both this and a JSON module, and they both seem to share the same goal of saving and loading objects, can you explain why I might use one over the other?

the escape sequence part goes over my head, but this seems like SerDes is a better solution:
strings, numbers and objects are allowed as object/{} keys unlike JSON which restricts it to string data type only
I don't know if there are other reasons for using JSON, but I'm ultimately just looking for a simple way to save and load objects.

Edit: On testing it out, I love that serializing it converts it to ahk's object syntax, I hadn't understood that from the description some how.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

06 Sep 2014, 01:19

Thanks bobc119 :)
bobc119 wrote:can you explain why I might use one over the other?
JSON is for JSON formatted string/text so it is somehow written to conform to the JSON specifications despite some differences in some data types. While this one is written to conform to AHK itself. I would use JSON if you're expecting to share the data between different languages/apps/etc., otherwise if it's solely for AHK, then use this one.
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

23 Sep 2014, 10:12

Nice work!
If use ObjInsert(V1), or ObjRawSet(V2 a-0.54) to insert/add a normal key named base, serilaize and de-serilaize would work correctly.
Is it possible to support base(not as a normal key)? It might be used for inheritance which not have to implement __Get, just o.base:={..} a normal object.
我为人人,人人为己?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

24 Sep 2014, 09:18

RobertL wrote:Is it possible to support base ...
Yes it's possible, it's just a matter of changing a couple of lines, I believe so. The reason I went with ObjInsert/ObjRawSet is to avoid custom behavior from meta-functions ...
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

25 Sep 2014, 06:41

Yes.
I think could construct special key, or special object which describe object-base pair like $obj1:{..}, $obj2:$obj1..?
我为人人,人人为己?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

25 Sep 2014, 06:49

You mean a different notation for base when serializing? What's wrong with { "base": { "some_key": "some value" }? It's how it's written in AHK. I'm sorry but I'm not sure about what you really meant
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

25 Sep 2014, 06:57

Emm..
There are two type of 'base', a normal key, which's name is 'base', another one - real base, like class.
  • So, I think need to distinguish from them by notation?
  • Or, second way, use a special object, to record each object's base. Like
    I wrote:special object which describe object-base pair like SpecialObject : {$obj1:{obj1's base}, $obj2:$obj1}.
我为人人,人人为己?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a

25 Sep 2014, 07:13

Ok got it, object enumeration does not include the base, I'll work on this. Thanks. However, supported values will only be strings, numbers and objects(arrays, associative arrays).
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a049]

08 Nov 2015, 22:00

Thanks for some great libraries, Coco.

Is it possible to serialize to a variable in memory? I'm looking for a simple way to inspect objects in memory by serializing them and then displaying them via a msgbox. I've tried displaying a class variable via the JSON.dump(), but it doesn't work, so I thought I would try this.
Coco-guest

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a049]

09 Nov 2015, 01:22

This lib should do it, however I haven't really used it as of lately so I'm not sure if new bugs have been introduced. I do have another function that I'm using(for debugging) that should to do what you want, I can post it later if you want.
SpaghettiCoder
Posts: 6
Joined: 30 Sep 2015, 03:40

Re: SerDes() - AHK object (de)serialization [v1.1 and v2.0-a049]

15 Aug 2016, 03:04

There is a bug in deserialization: String literals ending in `` cause an exception ("Missing close quote(s).")

E.g. SerDes("[""a``""]") causes the script to abort with "Missing close quote(s)."

Is there a bugfix available?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 266 guests