Page 1 of 1

Remove All Unnecessary Whitespaces from config String with regex

Posted: 11 Jan 2019, 10:26
by SL5
hwoto Remove All Unnecessary Whitespaces from JSON String with regex?

certainly very easy to do.

but I still ask maybe that's already exist here

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 11:07
by SL5
I think it's not so easy as I first thought:

discussion is started here: https://stackoverflow.com/questions/541 ... autohotkey

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:13
by SL5
best result for the moment. works for me (have no so difficlut values for the moment).

Code: Select all

configJSONContent =
(
	g_config := 
	{ 
        FuzzySearch:{
            enable: true ,
            keysMAXperEntry : 6 ,
            doValueCopy : false , 
		  halloWorld : "Hello World 4"
        }
    }
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include *i %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % g_config["FuzzySearch"]["halloWorld"]
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*" )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,50
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}


Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:33
by teadrinker
SL5 wrote:
11 Jan 2019, 12:13

Code: Select all

configJSONContent =
(
	g_config := 
	{ 
        FuzzySearch:{
            enable: true ,
            keysMAXperEntry : 6 ,
            doValueCopy : false , 
		  halloWorld : "Hello World 3"
        }
    }
)
This is not valid JSON. All the keys must be enclosed in quotes:

Code: Select all

json =
(
   { 
        "FuzzySearch":{
            "enable": true ,
            "keysMAXperEntry" : 6 ,
            "doValueCopy" : false , 
            "halloWorld" : "Hello World 3"
        }
    }
)
oDoc := ComObjCreate("htmlfile")
oDoc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">")
try oDoc.parentWindow.eval("var test = JSON.stringify(JSON.parse('" . RegExReplace(json, "\R") . "'));")
catch {
   MsgBox, Invalid json!
   Return
}
MsgBox, % oDoc.parentWindow.test

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:34
by MannyKSoSo
You are correct it is not a valid JSON, but you can see that he is making it an array that he can use inside of autohotkey

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:44
by teadrinker
MannyKSoSo wrote:
11 Jan 2019, 12:34
You are correct it is not a valid JSON, but you can see that he is making it an array that he can use inside of autohotkey
AutoHotkey array is not the same as JSON. The question is about JSON, isn't it? :)

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:56
by MannyKSoSo
Indeed it is different than JSON, but looking at the code he provided he is just using the style of the JSON (no quotes except in the data section), but uses it as a variable to call the information, which is why its not proper JSON format. So its kinda a mixture of two things.

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 12:58
by SL5
but for some reason that does not work (its empty):

UPDATE: I found out thats is script dependent.
it works in a simple test script.
2019-01-11 19_17_59-global-IntelliSense-everywhere-Nightly-Build [G__fre_git_github_global-IntelliSe.png
2019-01-11 19_17_59-global-IntelliSense-everywhere-Nightly-Build [G__fre_git_github_global-IntelliSe.png (7.14 KiB) Viewed 3023 times

Code: Select all

configJSONContent =
(
test :=
 {
   enable: "true",
   MAXlines: "87654",
   keysMAXperEntry : "6",
   minKeysLen: "4",
   doValueCopy: "false"
 } ; difficult to implement symlink copy for not rr lines doValueCopy. todo: issue . doValueCopy : false  is not fully implemented
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % "minKeysLen=" test["FuzzySearch"]["minKeysLen"]
FileRead, configJSONContentminify, %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % configJSONContentminify
reload
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	; Changes always become active on the next next call. becouse include is a preparser command. 19-01-11_18-33
	; discussion here: https://stackoverflow.com/questions/54149980/remove-all-unnecessary-whitespaces-from-json-string-with-regex-in-autohotkey
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*" )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,40
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}


same problem here:

its producing:
test := { ddd : "4" }

but messagebox is empty

Code: Select all

test := {}
configJSONContent =
(
test :=
{
   ddd : "4"
}
)
JSONstr2minify_JSONfile(configJSONContent,"configJSON.minify.inc.ahk")
#Include %A_ScriptDir%\configJSON.minify.inc.ahk
FileRead, configJSONContentminify, %A_ScriptDir%\configJSON.minify.inc.ahk
MsgBox, % "test[ddd]=" test["ddd"] "`n`n" configJSONContentminify
; MsgBox, % configJSONContentminify
reload
JSONstr2minify_JSONfile(ByRef configJSONContent, configJSONminifyincahk := "configJSON.minify.inc.ahk"){
	; Changes always become active on the next next call. becouse include is a preparser command. 19-01-11_18-33
	; discussion here: https://stackoverflow.com/questions/54149980/remove-all-unnecessary-whitespaces-from-json-string-with-regex-in-autohotkey
	configJSONContentminify := RegExReplace( configJSONContent , "[\s\t ]*[\n\r]+[\s\t ]*", " " )
	tempFileAddress := A_ScriptDir "\" A_TickCount ".temp.txt"
	FileAppend, % configJSONContentminify, % tempFileAddress
	FileCopy,% tempFileAddress, %A_ScriptDir%\%configJSONminifyincahk%, 1
	Sleep,40
	FileDelete,% tempFileAddress
	Return configJSONminifyincahk
}

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 13:03
by safetycar
Personally I don't think it's a good Idea mixing things.
If you want to make a valid AHK config I'd go with an include on top of your script.
If you want to make a valid JSON, then don't use the ":=" operator or other syntax that isn't JS valid.
Maybe an <AHKON> could be possible but I see it as quite some effort...

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 13:08
by SL5
safetycar wrote:
11 Jan 2019, 13:03
Personally I don't think it's a good Idea mixing things.
If you want to make a valid AHK config I'd go with an include on top of your script.
If you want to make a valid JSON, then don't use the ":=" operator or other syntax that isn't JS valid.
Maybe an <AHKON> could be possible but I see it as quite some effort...
thanks
I know only this one to assign JSON to a variable in autohotkey. if i know it better i do it :)

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 13:16
by safetycar
To keep it simple JSON it's mostly about putting objects inside objects, you're not defining variables in JSON, you're just describing object keys and values.
You'll be mostly using colon ":" instead of assignments.
For example: { key1:val1, key2 : { key3:val3} }

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 11 Jan 2019, 13:59
by SL5
safetycar wrote:
11 Jan 2019, 13:16
To keep it simple JSON it's mostly about putting objects inside objects, you're not defining variables in JSON, you're just describing object keys and values.
You'll be mostly using colon ":" instead of assignments.
For example: { key1:val1, key2 : { key3:val3} }
yes. i will rename my vars an function.

i want a structured json like, nearly json like, config possibililty into a seperate file, with the possibilty to layout it with newlines and tabs.
probably the best from doku: https://www.autohotkey.com/docs/Objects ... ive_Arrays

updated:

Code: Select all

configStr2minify_configFile(configIncAhkAddress := "\config\config.inc.ahk", configMinifyIncAhkAddress := "\config.minify.inc.ahk" ){
..
now its permantly updated here: https://github.com/sl5net/global-Intell ... nc.ahk#L19

i like that :)
2019-01-11 21_05_19-Table awaiting Action.png
2019-01-11 21_05_19-Table awaiting Action.png (23.35 KiB) Viewed 2959 times
( https://github.com/sl5net/global-Intell ... ig.inc.ahk )
creates that:
2019-01-11 22_31_43-Table awaiting Action.png
2019-01-11 22_31_43-Table awaiting Action.png (11.06 KiB) Viewed 2956 times

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 12 Jan 2019, 02:33
by safetycar
I think what you're trying to do is ok from the point of view of functionality (I haven't seen where the g_config is set to {} but I guess it's somewhere or it won't work).

But don't get confused, what you're doing is not JSON. Because JSON is about an Object represented as a String. If you don't have an Stringifier and a Parser(Destringifier) you're just working with objects directly. Which is not bad, but it's good to know the difference.

Or maybe I got the wrong idea because I haven't seen any reference to those functions.

Re: Remove All Unnecessary Whitespaces from JSON String with regex

Posted: 12 Jan 2019, 06:05
by SL5
safetycar wrote:
12 Jan 2019, 02:33
I think what you're trying to do is ok from the point of view of functionality (I haven't seen where the g_config is set to {} but I guess it's somewhere or it won't work).

But don't get confused, what you're doing is not JSON. Because JSON is about an Object represented as a String. If you don't have an Stringifier and a Parser(Destringifier) you're just working with objects directly. Which is not bad, but it's good to know the difference.

Or maybe I got the wrong idea because I haven't seen any reference to those functions.
yes thank :thumbup: I have mentioned elsewhere down, that I was mistaken with the title. how can you change that(solved: i changed first posting title)? :problem:
this is an associative array

yes g_config is set to {} is done inside the script. no prob. Thank you, I want to mention that in the documentary:
[code]
useItLike =
(
SetTimer,check_configFile_Changed,2000
g_config := {}
configStr2minify_configFile()
# Include *i %A_ScriptDir%\config.minify.inc.ahk
)

[/code]