Jump to content

Recommended Posts

  • 4 weeks later...
Posted

Please provide changes  in JSON(2018.10.01b).zip

Member JSON.au3

Line 512  old:

Quote

Local $sObjPath = $ObjPath

Line 512 new

Quote

Local §Value, $sObjPath = $ObjPath

or

add "Local" to  Line 515

to avoid Message:

Quote

C:\Program Files (x86)\AutoIt3\Include_User\JSON.au3" (515) : ==> Variable used without being declared.:

========================================

Member Json_Test.au3 . to prettify

Lines 29 & 30 are part of "Test2", change "Test3" to "Test2"

thx

 

 

 

 

  • Developers
Posted (edited)
1 hour ago, DerPensionist said:

 

Quote

Local §Value, $sObjPath = $ObjPath

I guess you mean an $ sign there in stead of the  § ;)

Will add that for avoiding the au3check warning and update the zipfile.

Thanks,Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted (edited)

hi guys, :D

It's a been a long time since i use to pass json as parameter to my functions, really cool to add new parameter without changing code that much.

Each time i use the same variable $jsonfunc, so in many function i do:

If Json_IsObject($jsonfunc) Then        
        Json_ObjClear($jsonfunc)        
    Else
        $jsonfunc = Json_ObjCreate()
    EndIf

and next, i add parameter and all is ok, i call my functions with $jsonfunc as parameter

And this is ok.

 

Today i encounter a really simple problem, that you can reproduce with the code below:

I just pass a json (jsonfunc) as a parameter to myfunc(), in the function, the parameter is $jsonparam.

But if i do

Json_ObjClear($jsonfunc)

My $jsonparam is cleared.  why ?? :blink::blink:

#include <Json.au3>
local $jsonfunc


Func myfunc($jsonparam)
    ConsoleWrite("we're in myfunc" & @CRLF)
    If Not Json_IsObject($jsonparam) Then
        ConsoleWrite("$jsonparam is not an object, leaving." & @CRLF)
        Return
    Else
        ConsoleWrite("$jsonparam is an object OK." & @CRLF)
    EndIf

    If Json_IsObject($jsonfunc) Then
        ConsoleWrite("$jsonparam, before clear the $jsonfunc:" & @CRLF & Json_Encode($jsonparam, $Json_PRETTY_PRINT) & @CRLF & @CRLF)
        Json_ObjClear($jsonfunc)
        ConsoleWrite("$jsonparam, after clear the $jsonfunc:" & @CRLF & Json_Encode($jsonparam, $Json_PRETTY_PRINT) & @CRLF)
    Else
        $jsonfunc = Json_ObjCreate()
    EndIf

EndFunc


If Json_IsObject($jsonfunc) Then
    Json_ObjClear($jsonfunc)
Else
    $jsonfunc = Json_ObjCreate()
EndIf

Json_ObjPut($jsonfunc, "TRI", "1_Niveau_")
Json_ObjPut($jsonfunc, "JSON_ORIGINEL", 1)
ConsoleWrite("we call function with a json ($jsonfunc) as parameter" & @CRLF)
myfunc($jsonfunc)
Spoiler

Output:

we call function with a json ($jsonfunc) as parameter
we're in myfunc
$jsonparam is an object OK.
$jsonparam, before clear the $jsonfunc:
{
    "TRI": "1_Niveau_",
    "JSON_ORIGINEL": 1
}

$jsonparam, after clear the $jsonfunc:
{}


any idea ?

thanks.

Nicolas.
Edit: it is the same with last version of Json from 2018/10/28 and one from more than a year.

Edited by satanico64
Posted (edited)

 

@satanico64

You appear to have a problem with the scope of the $jsonfunc variable.  What happens when you run the snippet below?  It works for me.  All I did was change the "Local" to "Global"

 

#include <myincludes\json\Json.au3>
global $jsonfunc


Func myfunc($jsonparam)
    ConsoleWrite("we're in myfunc" & @CRLF)
    If Not Json_IsObject($jsonparam) Then
        ConsoleWrite("$jsonparam is not an object, leaving." & @CRLF)
        Return
    Else
        ConsoleWrite("$jsonparam is an object OK." & @CRLF)
    EndIf

    If Json_IsObject($jsonfunc) Then
        ConsoleWrite("$jsonparam, before clear the $jsonfunc:" & @CRLF & Json_Encode($jsonparam, $Json_PRETTY_PRINT) & @CRLF & @CRLF)
        Json_ObjClear($jsonfunc)
        ConsoleWrite("$jsonparam, after clear the $jsonfunc:" & @CRLF & Json_Encode($jsonparam, $Json_PRETTY_PRINT) & @CRLF)
    Else
        $jsonfunc = Json_ObjCreate()
    EndIf

EndFunc


If Json_IsObject($jsonfunc) Then
    Json_ObjClear($jsonfunc)
Else
    $jsonfunc = Json_ObjCreate()
EndIf

Json_ObjPut($jsonfunc, "TRI", "1_Niveau_")
Json_ObjPut($jsonfunc, "JSON_ORIGINEL", 1)
ConsoleWrite("we call function with a json ($jsonfunc) as parameter" & @CRLF)
myfunc($jsonfunc)

Result:

we call function with a json ($jsonfunc) as parameter
we're in myfunc
$jsonparam is an object OK.
$jsonparam, before clear the $jsonfunc:
{
    "TRI": "1_Niveau_",
    "JSON_ORIGINEL": 1
}

$jsonparam, after clear the $jsonfunc:
{}

 

Edited by TheXman
Posted

I thought about the scope too, and tried that before, it does'not change anything.

Sames conclusions for you ?

 

Meanwhile, there is none reason to reset my function parameter, without calling it explicitely

 

Posted

I added my result to my post.  As I said, it worked for me.  Did you run the exact snippet or some other code?

Posted

What result are you expecting after the json_objclear()?

Posted

Oh, okay, I see what you are saying now.  Let me take a closer look.

Posted

I'm not exactly sure how AutoIt works in this regard without testing it, but in other languages, objects are passed ByRef.  Therefore, if the same holds true you aren't using a copy of the $jsonfunc object in myfunc(), you are using/manipulating the object itself.

Posted

i thought about that too, it would mean that a variable passed as a parameter would be a by ref ? no, i don't think so !

In a function, the scope of a parameter is local scope.

But i agree that the problem is something like that,

 

Pretty sure it's a PTR problem, but what should i do with that ? except using another name ...

 

 

Posted (edited)

Passing something ByRef means that you are passing a pointer.

Edited by TheXman
  • Developers
Posted (edited)

Again it is not about Byref but rather the $jsonfunc being a pointer already in the Main part of the script which is passed to the Func.
It is similar to what happens in this demo script:

Global $jsonfunc =  DllStructCreate("char txt[128]")
DllStructSetData($jsonfunc, "txt", "aaa")

Func myfunc($jsonparam)
    ConsoleWrite('+Before $jsonfunc  txt = ' & DllStructGetData($jsonfunc, 1) & @CRLF ) ;### Debug Console
    ConsoleWrite(' Before $jsonparam txt = ' & DllStructGetData($jsonparam, 1) & @CRLF ) ;### Debug Console
    DllStructSetData($jsonfunc, "txt", "bbb")
    ConsoleWrite('-After  $jsonfunc  txt = ' & DllStructGetData($jsonfunc, 1) & @CRLF ) ;### Debug Console
    ConsoleWrite(' After  $jsonparam txt = ' & DllStructGetData($jsonparam, 1) & @CRLF ) ;### Debug Console
EndFunc   ;==>myfunc

myfunc($jsonfunc)

Does that make sense?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted (edited)

thanks for your answer

in fact, not really clear for me, why my $jsonparam would be a pointer, function parameters must be in local scope,  they should'nt point to the variable used when we call a function.

In fact with string variables, scope is local, with json, we can't be in local scope and use json as parameter this is what i understand

Your script only with numbers do as i expect:

Global $jsonfunc =  DllStructCreate("char txt[128]")
DllStructSetData($jsonfunc, "txt", "aaa")

Func myfunc($jsonparam)
    ConsoleWrite('+Before $jsonfunc  txt = ' & DllStructGetData($jsonfunc, 1) & @CRLF ) ;### Debug Console
    ConsoleWrite(' Before $jsonparam txt = ' & DllStructGetData($jsonparam, 1) & @CRLF ) ;### Debug Console
    DllStructSetData($jsonfunc, "txt", "bbb")
    ConsoleWrite('-After  $jsonfunc  txt = ' & DllStructGetData($jsonfunc, 1) & @CRLF ) ;### Debug Console
    ConsoleWrite(' After  $jsonparam txt = ' & DllStructGetData($jsonparam, 1) & @CRLF ) ;### Debug Console
EndFunc   ;==>myfunc

myfunc($jsonfunc)

Global $varfunc =  10
Func myfunc2($varparam)
    ConsoleWrite('+Before $varfunc  txt = ' & $varfunc & @CRLF ) ;### Debug Console
    ConsoleWrite(' Before $varparam txt = ' & $varparam & @CRLF ) ;### Debug Console
    $varfunc = 5
    ConsoleWrite('+Before $varfunc  txt = ' & $varfunc & @CRLF ) ;### Debug Console
    ConsoleWrite(' Before $varparam txt = ' & $varparam & @CRLF ) ;### Debug Console
EndFunc   ;==>myfunc
myfunc2($varfunc)

Output:
+Before $jsonfunc  txt = aaa
 Before $jsonparam txt = aaa
-After  $jsonfunc  txt = bbb
 After  $jsonparam txt = bbb
+Before $jsonfunc  txt = 10
 Before $jsonparam txt = 10
+Before $jsonfunc  txt = 5
 Before $jsonparam txt = 10
[edit: variables names corrected in code]

Edited by satanico64
  • Developers
Posted (edited)

As I mentioned: 

35 minutes ago, Jos said:

it is not about Byref but rather the $jsonfunc being a pointer already

So $jsonfunc is a pointer to the memory location of the structure ( in your case the Object).
The pointer (hex value) is passed on to Myfunc() via the parameter, but is is still the same value for the pointer, so $jsonparam will then point to the exact same memory location.
That is why, when one changes the other changes as well since they both use the same memory location.

Making more sense now? :) 

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...