[v2] Yaml and JSON parser

Post your working scripts, libraries and tools.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

[v2] Yaml and JSON parser

Post by HotKeyIt » 27 Nov 2019, 19:40

Yaml on github

Parses Yaml and JSON documents from string or file. Requires AutoHotkey v2.106+
Most of Yaml 1.2 spec are implemented:
  • Mapping between Sequences is not supported, see Example 2.11.
  • Only tags !!str, !!int, !!float, !!binary, !!bool and !!null are supported. !!set results in error and all other tags are ignored.
  • Only Yaml documents with 1000 level deepness can be parsed (to increase simply increase Loop 1000 in Load(ByRef txt,Y:=0)
  • Other features might be not implemented.
  • Yaml uses objects for map and arrays for sequence, however Dump will work for Array, Map and Object.
How to use:
  • Create an Object from Text or File:
    YamlObj:=Yaml(TextOrFilePath)
  • Apply/combine another Yaml document into existing object:
    Yaml(TextOrFilePath, YamlObject)
  • Dump Yaml object back to text, use JSON from 5-th level only:
    Yaml(YamlObject, 5)
  • Dump Yaml object back to text, use JSON only and indent until 5-th level (v1.0.4++):
    Yaml(YamlObject, -5)
  • Yaml(TextOrFilePath) will result in Array of documents (objects), if you have only one document you can retrieve it directly:
    YamlObj:=Yaml(TextOrFilePath)[1]
Full length examples from yaml.org:

Code: Select all

text:="
(
--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments:
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.
---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
  This is an error message
  for the log file
---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
  A slightly different error
  message.
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
  Unknown variable "bar"
Stack:
  - file: TopClass.py
    line: 23
    code: |
      x = MoreObject("345\n")
  - file: MoreClass.py
    line: 58
    code: |-
      foo = bar

)"
y:=Yaml(text) ; Load Yaml documents into objects
MsgBox Yaml(y,5) ; Dump objects back into Yaml

User avatar
kczx3
Posts: 1644
Joined: 06 Oct 2015, 21:39

Re: [v2] Yaml parser

Post by kczx3 » 27 Nov 2019, 22:56

FYI there is a dedicated sub forum for v2 scripts and functions

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

Re: [v2] Yaml parser

Post by HotKeyIt » 28 Nov 2019, 09:00

Thanks, topic moved.

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

Re: [v2] Yaml parser

Post by HotKeyIt » 28 Nov 2019, 20:22

Fixed a bug and Alias for objects

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

Re: [v2] Yaml parser

Post by HotKeyIt » 01 Dec 2019, 09:09

V 1.0.2 wrote:
  • Intendation increased to 1000, can be easily increased if necessary.
  • Implemented multi-intend so examples from yaml.org work out of the box
  • Implemented multi document support, see example
  • Implemented plain scalar

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

Re: [v2] Yaml parser

Post by HotKeyIt » 02 Jan 2020, 12:56

V 1.0.3 wrote:
  • Fixed bug in Dump
  • Fixed bug in JSON parser

User avatar
kczx3
Posts: 1644
Joined: 06 Oct 2015, 21:39

Re: [v2] Yaml and JSON parser

Post by kczx3 » 02 Jan 2020, 13:43

The main title of this says "[v2] Yaml and JSON parser". What part of this parses JSON?

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: [v2] Yaml and JSON parser

Post by guest3456 » 02 Jan 2020, 14:14

kczx3 wrote:
02 Jan 2020, 13:43
The main title of this says "[v2] Yaml and JSON parser". What part of this parses JSON?
my guess from his yaml spec link:
https://yaml.org/spec/1.2/spec.html#id2759572 wrote: YAML can therefore be viewed as a natural superset of JSON, offering improved human readability and a more complete information model. This is also the case in practice; every JSON file is also a valid YAML file. This makes it easy to migrate from JSON to YAML if/when the additional features are required.

JSON's RFC4627 requires that mappings keys merely “SHOULD” be unique, while YAML insists they “MUST” be. Technically, YAML therefore complies with the JSON spec, choosing to treat duplicates as an error.


would be nice to see some unit testing supplied with libraries like this :angel:


User avatar
kczx3
Posts: 1644
Joined: 06 Oct 2015, 21:39

Re: [v2] Yaml and JSON parser

Post by kczx3 » 02 Jan 2020, 14:53

Interesting... So this could parse a JSON string just fine... I'm intrigued now. I think that the heading may throw people off because JSON is probably a more familiar term and format for people of this forum.

I assume that when you dump it though that it will be in yaml format and not the original JSON format.

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

Re: [v2] Yaml parser

Post by HotKeyIt » 02 Jan 2020, 15:45

V 1.0.4 wrote:
  • Implemented pure JSON parsing and proper dumping, to beautify (indent) JSON use negative second parameter
Yaml(YamlObject, -5)

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

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 02 Jan 2020, 15:46

kczx3 wrote:
02 Jan 2020, 14:53
Interesting... So this could parse a JSON string just fine... I'm intrigued now. I think that the heading may throw people off because JSON is probably a more familiar term and format for people of this forum.

I assume that when you dump it though that it will be in yaml format and not the original JSON format.
Now it should fully support in Yaml and JSON as well as in and out ;)

User avatar
kczx3
Posts: 1644
Joined: 06 Oct 2015, 21:39

Re: [v2] Yaml parser

Post by kczx3 » 02 Jan 2020, 21:02

HotKeyIt wrote:
02 Jan 2020, 15:45
V 1.0.4 wrote:
  • Implemented pure JSON parsing and proper dumping, to beautify (indent) JSON use negative second parameter
Yaml(YamlObject, -5)
Sorry but I’m not seeing that anywhere. I went to your GitHub repo for this however it doesn’t show as being updated for two years.

Never mind. Seems like you’re using the quote feature to post release notes. I wasn’t quite getting that. Apologies.

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

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 03 Jan 2020, 03:14

Sorry, I have updated GitHub as well ;)

coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: [v2] Yaml and JSON parser

Post by coffee » 09 Jan 2020, 20:42

return Load(FileRead(file),Yaml) ; load yaml from file This is referencing the wrong variable.

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

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 10 Jan 2020, 16:06

Thanks, this is fixed now.

User avatar
kczx3
Posts: 1644
Joined: 06 Oct 2015, 21:39

Re: [v2] Yaml and JSON parser

Post by kczx3 » 10 Jan 2020, 18:05

Coffee, was that issue what was causing the speed issues with this parser that you mentioned in another thread?

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

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 10 Jan 2020, 19:49

I had a look and AFAIK, the biggest speed issue is due to Yaml parsing char by char.
In Yaml notation it is allowed to use unquoted keys and values while JSON needs to be enclosed in quotes.

I will need to implement a separate function to parse pure JSON faster.

coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: [v2] Yaml and JSON parser

Post by coffee » 10 Jan 2020, 20:26

kczx3 wrote:
10 Jan 2020, 18:05
Coffee, was that issue what was causing the speed issues with this parser that you mentioned in another thread?
Nop.

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

Re: [v2] Yaml and JSON parser

Post by HotKeyIt » 21 Jan 2020, 17:03

v1.0.5 wrote: Pure JSON are now parsed separately very fast
Increased speed for Yaml and some bugfixes

v1.0.6
Fix double quotes


Post Reply

Return to “Scripts and Functions (v2)”