JSON library, write by cpp

Post your working scripts, libraries and tools.
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

15 Nov 2022, 19:29

RaptorX wrote:
15 Nov 2022, 15:23
So The solution might be that the library always pass a pointer to a string to the dll function?
This makes me feel strange. Both string and string pointer are valid values. I need to verify them. Normally, the invalid json will indicate the character in the error.

I found the problem. Because of the recent update, class Var changed a little, which made it a little incompatible with the previous code.

The fixed version has been reuploaded.
User avatar
RaptorX
Posts: 395
Joined: 06 Dec 2014, 14:27
Contact:

Re: JSON library, write by cpp

15 Nov 2022, 21:11

If you update it please provide an update link :)
Projects:
AHK-ToolKit
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

15 Nov 2022, 21:21

In the first post. download/file.php?id=19769

The bug maybe existed when it was just released.
vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: JSON library, write by cpp

16 Nov 2022, 03:26

RaptorX wrote:
15 Nov 2022, 15:43
Maybe if we fix the pointer thing it would be a great library for a lot of people that deal with data from the internet.
In continuation of the discussion, with some digression from the original topic, I dare to offer a reason for understanding why using AutoHotkey to write JSON is not a good idea.

Original prettified JSON

A slightly modified, and augmented, example from @RaptorX:

Code: Select all

#Include Native.ahk2

js := Native.LoadModule('ahk-json\' (A_PtrSize * 8) 'bit\ahk-json.dll')['JSON']

If !SelectedFile := FileSelect(, A_WorkingDir, 'Open a file', 'Text/Manifest/JSON (*.txt; *.item; *.json)')
	ExitApp
_txt_in := FileRead(SelectedFile)
MsgBox('Symbols count: ' StrLen(_txt_in))

_obj1 := js.parse(StrPtr(_txt_in))

_txt_out1 := js.stringify(_obj1)
MsgBox(_txt_out1)

If FileExist('big_json_test_1.txt')
	FileDelete('big_json_test_1.txt')
FileAppend(_txt_out1, 'big_json_test_1.txt')

htmlfile := ComObject('htmlfile')
htmlfile.write('<meta http-equiv="X-UA-Compatible" content="IE=edge">')
_obj2 := htmlfile.parentWindow.JSON.parse(_txt_in)

_txt_out2 := htmlfile.parentWindow.JSON.stringify(_obj2)
MsgBox(_txt_out2)

If FileExist('big_json_test_2.txt')
	FileDelete('big_json_test_2.txt')
FileAppend(_txt_out2, 'big_json_test_2.txt')
Results:

big_json_test_1.txt
big_json_test_2.txt

As you can see, the original key sorting is completely lost, no matter how JSON is parsed using AutoHotkey. Accordingly, the subsequent conversion of the object to JSON will not restore the order of the keys.

PS. As they often say in my country: «It was smooth on paper, but they forgot about the ravines». (I am not a great expert in English, so I could not save the original rhyme :roll: )

PPS. I apologize for the offtopic :oops:
Please post your script code inside [code] ... [/code] block. Thank you.
vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: JSON library, write by cpp

16 Nov 2022, 05:01

@thqby
Can you explain why you use

Code: Select all

this.DefineProp('true', {value: 1})
this.DefineProp('false', {value: 0})
this.DefineProp('null', {value: ''})
instead of

Code: Select all

this.DefineProp('true', {value: ComValue(11, 65535)})
this.DefineProp('false', {value: ComValue(11, 0)})
this.DefineProp('null', {value: ComValue(1, 0)})
inside the Class definitions ?
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

16 Nov 2022, 06:30

Specific values can be freely defined without affecting usage. Values that are not objects are converted irreversibly.
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

16 Nov 2022, 06:34

vmech wrote:
16 Nov 2022, 03:26

As you can see, the original key sorting is completely lost, no matter how JSON is parsed using AutoHotkey. Accordingly, the subsequent conversion of the object to JSON will not restore the order of the keys.
JSON for ahk_h v2, The default is UMap, which preserves the order of the keys.
geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: JSON library, write by cpp

16 Nov 2022, 10:10

I've done a bit of research on the duplicate key situation, and as far as I can tell duplicate keys are considered valid (but strongly discouraged) in a JSON text. However, the recommended parser behavior when encountering a duplicate key is explicitly undefined, with the most recent JSON RFC merely documenting that rejecting and overwriting are both common rather than suggesting one over the other.

ECMAScript, the standard to which JavaScript conforms, specifies that JSON.parse will have the overwriting behavior. This is notable, given that JSON stands for "JavaScript Object Notation". But JavaScript's behavior in this case does not make thqby's rejecting implementation wrong, or the other JSON libraries right.

Personally, I would consider adding a boolean switch that lets the caller disable duplicate key rejection when needed, as well as an error message that more fully explains what happened.
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: JSON library, write by cpp

16 Nov 2022, 11:36

Nice research and a good suggestion :thumbsup:
vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: JSON library, write by cpp

16 Nov 2022, 11:38

@geek
«Tail wags the dog»

Image

JSON has absolutely nothing to do with it. Show at least one widely used programming language that allows duplication of object key names at same nesting level.
geek wrote:
16 Nov 2022, 10:10
Personally, I would consider adding a boolean switch that lets the caller disable duplicate key rejection when needed, as well as an error message that more fully explains what happened.
The precedent has already been set: AutoHotkey and C/C++ failed to process the JSON proposed by @RaptorX 8-)
Please post your script code inside [code] ... [/code] block. Thank you.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: JSON library, write by cpp

16 Nov 2022, 13:26

vmech wrote:The precedent has already been set: AutoHotkey and C/C++ failed to process the JSON proposed by @RaptorX
I also think that some configuration would make sense because there's a different point of view if you think about how autohotkey behaves about duplication in other cases:

Code: Select all

m := map("a",1, "a",2)
o := {a:1, a:2}
msgbox m["a"] " " o.a ; outputs "2 2"
User avatar
RaptorX
Posts: 395
Joined: 06 Dec 2014, 14:27
Contact:

Re: JSON library, write by cpp

16 Nov 2022, 14:19

vmech wrote:
16 Nov 2022, 11:38
The precedent has already been set: AutoHotkey and C/C++ failed to process the JSON proposed by @RaptorX 8-)
My understanding here is that the library creator decided it was an invalid JSON while other libraries dont.

I agree with the previous commenter that it should be an option because as the JSON definition doesnt explicitly define what should happen with duplicate keys, it is then determined either by the library creator or the end user.

Having the end user decide seems like be best approach.
Projects:
AHK-ToolKit
sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Re: JSON library, write by cpp

01 Apr 2023, 12:42

for a layman,

Im having a tough time parsing (pardon pun) folder structure.

myscript.ahk
/ lib

/ lib / native.ahk?

/ lib / 64-bit / ahk2.dll?
/ lib / 64-bit / ahk-json.dll?

I dont know where to put stuff
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
User avatar
RaptorX
Posts: 395
Joined: 06 Dec 2014, 14:27
Contact:

Re: JSON library, write by cpp

01 Apr 2023, 17:17

sashaatx wrote:
01 Apr 2023, 12:42
I dont know where to put stuff
image.png
image.png (6.03 KiB) Viewed 2217 times
And this is my lib folder:
image.png
image.png (9.22 KiB) Viewed 2214 times
This is my folder structure and then I added:

image.png
image.png (9.92 KiB) Viewed 2217 times
make sure you use .\lib notation to make sure the library will be loaded correctly even if you include JSON.ahk on another script.
Projects:
AHK-ToolKit
sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Re: JSON library, write by cpp

03 Apr 2023, 08:52

RaptorX wrote:
01 Apr 2023, 17:17
sashaatx wrote:
01 Apr 2023, 12:42
I dont know where to put stuff

image.png

And this is my lib folder:
image.png

This is my folder structure and then I added:


image.png

make sure you use .\lib notation to make sure the library will be loaded correctly even if you include JSON.ahk on another script.
thank you for the detailed answer!!
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: JSON library, write by cpp

12 Aug 2023, 09:54

Very nice. Thank you for the code.

Any plan to add an option for pretty printing?
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

12 Aug 2023, 20:57

20170201225639 wrote:
12 Aug 2023, 09:54
Any plan to add an option for pretty printing?
Parameter 2 specifies the characters to indent.
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: JSON library, write by cpp

12 Aug 2023, 21:35

Ah, it's right there in the examples. Thanks!

Would you consider adding an option make stringify output JSON strings that are not indented but that have a space after the colon separating key and value? I.e.:
`{"name": "Jack"}`
rather than
`{"name":"Jack"}`

The reason I ask for this is because sometimes I need to diff JSON strings that consists of objects with a small number of key value pairs, so each string representing an object is usually place on a single line. For whatever reason, some serializers default on leaving a space after the colon and have no option to omit the space. So to do the proper diff sometimes I need to handwrite a string operation on top of ahk-json.
User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON library, write by cpp

13 Aug 2023, 21:53

There is no such style, but you can specify param 2 as an empty string and replace the newline.
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: JSON library, write by cpp

13 Aug 2023, 22:41

Thanks thqby. I also found in the cpp source code which probably controls this behavior

Code: Select all

 objcolon = indent ? _T("\": ") : _T("\":"); 
I imagine changing it to something like

Code: Select all

 objcolon = 1 ? _T("\": ") : _T("\":"); 
should probably result in defaulting on the one extra space output, in case I'll need to do this.

Thanks for the great tool!

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: TAC109 and 51 guests