<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK v2: JSON: кто как десериализует]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=18483&amp;type=atom" />
	<updated>2025-08-30T03:33:54Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=18483</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK v2: JSON: кто как десериализует]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=162649#p162649" />
			<content type="html"><![CDATA[<p>Спасибо.</p>]]></content>
			<author>
				<name><![CDATA[haridev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=45040</uri>
			</author>
			<updated>2025-08-30T03:33:54Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=162649#p162649</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK v2: JSON: кто как десериализует]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=162648#p162648" />
			<content type="html"><![CDATA[<p>Для v2:<br /></p><div class="codebox"><pre><code>#Requires AutoHotkey v2

json := &#039;{&quot;key&quot;: &quot;value&quot;}&#039;
obj := LightJson.Parse(json)
MsgBox obj[&#039;key&#039;]
MsgBox LightJson.Stringify(obj)
MsgBox LightJson.Beautify(json, &#039;    &#039;)

class LightJson
{
    static JS := LightJson.GetJS(), true := {}, false := {}, null := {}

    static Parse(json, objIsMap := true, _rec?) {
        if !IsSet(_rec)
            obj := this.Parse(this.JS.JSON.parse(json), objIsMap, true)
        else if !IsObject(json)
            obj := json
        else if this.JS.Object.prototype.toString.call(json) == &#039;[object Array]&#039; {
            obj := []
            Loop json.length
                obj.Push(this.Parse(json.%A_Index - 1%, objIsMap, true))
        }
        else {
            obj := objIsMap ? Map() : {}
            keys := this.JS.Object.keys(json)
            Loop keys.length {
                k := keys.%A_Index - 1%
                ( objIsMap &amp;&amp; obj[k]  := this.Parse(json.%k%, true , true))
                (!objIsMap &amp;&amp; obj.%k% := this.Parse(json.%k%, false, true))
            }
        }
        return obj
    }

    static Stringify(obj, indent := &#039;&#039;) {
        if indent = true {
            for k, v in [&#039;true&#039;, &#039;false&#039;, &#039;null&#039;]
                if (obj = this.%v%)
                    return v

            if IsObject(obj) {
                isArray := Type(obj) = &#039;Array&#039;
                enumerable := Type(obj) = &#039;Object&#039; ? obj.OwnProps() : obj
                str := &#039;&#039;
                for k, v in enumerable
                    str .= (A_Index = 1 ? &#039;&#039; : &#039;,&#039;) . (isArray ? &#039;&#039; : this.Stringify(k, true) . &#039;:&#039;) . this.Stringify(v, true)

                return isArray ? &#039;[&#039; str &#039;]&#039; : &#039;{&#039; str &#039;}&#039;
            }
            if IsNumber(obj) &amp;&amp; Type(obj) != &#039;String&#039;
                return obj

            for k, v in [[&#039;\&#039;, &#039;\\&#039;], [A_Tab, &#039;\t&#039;], [&#039;&quot;&#039;, &#039;\&quot;&#039;], [&#039;/&#039;, &#039;\/&#039;], [&#039;`n&#039;, &#039;\n&#039;], [&#039;`r&#039;, &#039;\r&#039;], [Chr(12), &#039;\f&#039;], [Chr(8), &#039;\b&#039;]]
                obj := StrReplace(obj, v[1], v[2])

            return &#039;&quot;&#039; obj &#039;&quot;&#039;
        }
        sObj := this.Stringify(obj, true)
        return this.JS.eval(&#039;JSON.stringify(&#039; . sObj . &#039;,&quot;&quot;,&quot;&#039; . indent . &#039;&quot;)&#039;)
    }

    static Beautify(json, indent) =&gt; this.JS.eval(&#039;JSON.stringify(&#039; . json . &#039;,&quot;&quot;,&quot;&#039; . indent . &#039;&quot;)&#039;)

    static GetJS() {
        static document := &#039;&#039;, JS
        if !document {
            document := ComObject(&#039;HTMLFILE&#039;)
            document.write(&#039;&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=9&quot;&gt;&#039;)
            JS := document.parentWindow
            (document.documentMode &lt; 9 &amp;&amp; JS.execScript())
        }
        return JS
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2025-08-29T21:19:21Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=162648#p162648</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK v2: JSON: кто как десериализует]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=162647#p162647" />
			<content type="html"><![CDATA[<p>В коллекции есть вариант для AHK v1, но я затруднился перевести его на v2.</p>]]></content>
			<author>
				<name><![CDATA[haridev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=45040</uri>
			</author>
			<updated>2025-08-29T17:36:04Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=162647#p162647</id>
		</entry>
</feed>
