[v2] Yaml and JSON parser

Post your working scripts, libraries and tools.
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: [v2] Yaml and JSON parser

Post by guest3456 » 28 Mar 2020, 09:06

HotKeyIt wrote:
28 Mar 2020, 08:56
call to nonexistent function UMap()..

is this for reg v2 or _h?


HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 28 Mar 2020, 09:14

Sorry, forgot to change back to Map, UMap (Unsorted Map) can be used in _H only.
Should be fine now.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: [v2] Yaml and JSON parser

Post by TheArkive » 17 Apr 2020, 01:42

Thank you very much for this! Better than XML when comparing overhead.

Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: [v2] Yaml and JSON parser

Post by Larkal » 01 Aug 2020, 02:40

Any chance of an update? I would love to use YAML as a configuration format for a project I am working on.

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 28 Dec 2020, 05:23

Updated for v2.122.

quaritexa
Posts: 32
Joined: 09 Nov 2017, 21:03

Re: [v2] Yaml and JSON parser

Post by quaritexa » 14 Jul 2021, 11:03

HotKeyIt wrote:
28 Dec 2020, 05:23
Updated for v2.122.
It would be nice to upgrade to v2. 138

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Yaml and JSON parser

Post by RaptorX » 20 Apr 2022, 07:22

Im trying to find a JSON lib for v2.0-b that allows me to load a string into an object.
This function seems to load the json into a map and then I have to do this for accessing the data:

Code: Select all

Y:=Yaml(text)
Y["users"][1]["_id"] ; btw this breaks using the example text string
instead of the expected:

Code: Select all

Y:=Yaml(text)
Y.users[1]._id
this is the only library that I have found to work on v2 but then it is very cumbersome to use because if I need to access a property that is a few levels deep I end up with something like this:

Code: Select all

json["included"][1]["progressiveStreams"][1]["bitRate"]
The issue is the extra [" and "] on many keys that could be replaced by a single .... any pointers on this?
Projects:
AHK-ToolKit

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 21 Apr 2022, 07:29

In v2 accessing properties and Items is separated and Map + Array have Items and Properties at the same time, also in obj.1 the key 1 is as a string and in obj[1] the key 1 is a number!
You can try somthing like this but will be only for reading and rather slow:

Code: Select all

m:=Map(1,2,3,4,5,6,7,8)
j:=EASYJSON(m)
MsgBox j.1
Class EASYJSON {
	__New(json){
		this.%""%:=json
	}
	__Get(k,v){
		return IsObject(val:=this.%""%[IsNumber(k)?k+0:k]) ? EASYJSON(val) : val
	}
}

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Yaml and JSON parser

Post by RaptorX » 21 Apr 2022, 17:58

HotKeyIt wrote:
21 Apr 2022, 07:29
In v2 accessing properties and Items is separated and Map + Array have Items and Properties at the same time, also in obj.1 the key 1 is as a string and in obj[1] the key 1 is a number!
You can try somthing like this but will be only for reading and rather slow:

Code: Select all

m:=Map(1,2,3,4,5,6,7,8)
j:=EASYJSON(m)
MsgBox j.1
Class EASYJSON {
	__New(json){
		this.%""%:=json
	}
	__Get(k,v){
		return IsObject(val:=this.%""%[IsNumber(k)?k+0:k]) ? EASYJSON(val) : val
	}
}
I am aware of those yeah.
So. Maps can access properties and keys separately. but this implementation only uses keys. why not use properties instead? I would only use keys if the property name is not a valid variable name no?
Projects:
AHK-ToolKit

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 21 Apr 2022, 19:21

There are default properties like length, __Item, Capacity, Count, CaseSense, Default, base... that would cause problems if used in yaml or json.

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2] Yaml and JSON parser

Post by RaptorX » 21 Apr 2022, 21:33

HotKeyIt wrote:
21 Apr 2022, 19:21
There are default properties like length, __Item, Capacity, Count, CaseSense, Default, base... that would cause problems if used in yaml or json.
I see how those could be a problem but I personally would program around those and map them to keys instead and make a note of that. I think the pros of having dot notation outweigh the few keywords that should be mapped to keys instead.

right now im doing this:

Code: Select all

case "video":
	RegExMatch post["content"]["Metadata"], "Asset:(?<id>.*)", &media:=""
	vidurl := mediaLib[media.id].url
	imgurl := mediaLib[media.id].thumb
case "article":
	vidurl := "none"
	imgurl := post["content"]["lImage"]["attributes"][1]["vector"]["Url"]
	.  post["content"]["lImage"]["attributes"][1]["vector"]["art"][2]["Url"]
Actual code by the way... really unsightly and truly unwieldy at times :D

I think one of the things that make it more annoying is the fact that the notation for the array item and property are basically the same when using key-value pairs.

consider:

Code: Select all

post["content"]["lImage"]["attributes"][1]["vector"]["Url"]
vs

Code: Select all

post.content.lImage.attributes[1].vector.Url
Notice how attributes[1] makes a bit of a context of what Im accesing there: a nameless array of attributes.

I do understand your decision but I have been looking for a "normal" JSON class for v2 and havent seen any functioning ones... all other versions are for older builds of v2 :P
Projects:
AHK-ToolKit

MrDoge
Posts: 159
Joined: 27 Apr 2020, 21:29

Re: [v2] Yaml and JSON parser

Post by MrDoge » 04 May 2022, 19:07

@RaptorX

Code: Select all

obj_.DefineProp("CaseSense", {Value: value_})
MsgBox obj_.%"CaseSense"%
this overrides CaseSense
we can forget it's a Map() with CaseSense

___

if you are only going to parse JSON:
viewtopic.php?f=83&t=103652

Code: Select all

MsgBox JSON_parse(FileRead("AutoHotkey_L releases.json"))[1]["assets"][1]["browser_download_url"]
MsgBox JSON_parse(FileRead("AutoHotkey_L releases.json"))[1].assets[1].browser_download_url
both work

jly
Posts: 89
Joined: 30 Sep 2020, 06:06

Re: [v2] Yaml and JSON parser

Post by jly » 07 Feb 2023, 23:15

@HotKeyIt

Run the following code and report an error.
Remove a "-" (Use text2) can execute successfully.
What is the reason for this?

Code: Select all


#Requires AutoHotkey v2.0
#SingleInstance Force
#Include Yaml.ahk

text:="
(
chlds:
  -
    a1title: 0301
  -
    a1title: 0301
  -
    a1title: 0301
)"

text2:="
(
chlds:
  -
    a1title: 0301
  -
    a1title: 0301
)"


y := Yaml(text)    ; Load Yaml documents into objects

MsgBox Yaml(y,5)   ; Dump objects back into Yaml


esc::ExitApp

/*
Error: Indentation problem.

Specifically:   -

	---- D:\work\14_scripts\AK2\Libs_v2_2\Yaml-ahkV2\Yaml-ahkV2\Yaml.ahk
	226: If LL<_LL
	227: If !I[LL]
▶	228: Throw ValueError("Indentation problem.", 0, LF)
	229: Else
	229: L := I[LL]

The current thread will exit.

Show call stack »

*/



Post Reply

Return to “Scripts and Functions (v2)”