Jump to content

A Non-Strict JSON UDF (JSMN)


Ward
 Share

Recommended Posts

  • Developers

@TheXman, I see that there are quit some differences between your  "Json.original.au3" file and the json.au3 in the current zip file in the first post. Did you use another version for starters?

I do see the issue you report and will look at the fix you propose for that.

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.
  :)

Link to comment
Share on other sites

  • Developers

Updated first post with merge of the lines needed to fix the reported issue. All other stuff skipped for now. 

Thanks,
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.
  :)

Link to comment
Share on other sites

@Jos Thanks for applying the fixes related to the json_dump() output.

 

3 hours ago, Jos said:

I see that there are quit some differences between your  "" file and the json.au3 in the current zip file in the first post.

The rest of the changes were not bug-related so they aren't as important.  One of the additional changes was to add a helper function, Json_ObjGetItems().  Json_ObjGetKeys() already existed so it seemed only logical to have a Json_ObjGetItems() too.  The other changes are just the declaration of variables before their use.  Unfortunately, I'm one of those old school programmers that strives to have a "clean" compile, meaning no warnings.  Since I always stipulate that all vars must exist before their use, when compiling scripts with the JSON UDF, it would throw multiple warnings about already declared vars and undeclared vars.  So I went through and made sure that all variables were declared only once prior to use.  Now the JSON UDF compiles "cleanly" with the strictest compilation directives, "#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d".

Again, your efforts towards maintaining the JSON UDF, and AutoIt in general, are greatly appreciated.

Edited by TheXman
Added that I went through and fixed all undeclared and already declared warnings from AU3Check
Link to comment
Share on other sites

  • Developers

Ah ok, well this is originally not my code and really only added the JSON_DUMP() function to it as I thought it would be very useful to others.

I am fine when we also clean up the code further and make an update for that. I honestly think these UDF's should be added to the standard AutoIt3 distribution but that means somebody needs to build the helpfilepages and examples. :)

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.
  :)

Link to comment
Share on other sites

  • Developers

Made another upload available adding Json_ObjGetItems(), Tidied source and fixed au3check warnings. Also updated json-test.au3 a bit.

Thanks,
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.
  :)

Link to comment
Share on other sites

  • 4 weeks later...

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

 

 

 

 

Link to comment
Share on other sites

  • Developers
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.
  :)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

@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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

What result are you expecting after the json_objclear()?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Developers

Pretty sure you are passing a PTR so both variables are using the same pointer hence use the same memory space.

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.
  :)

Link to comment
Share on other sites

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 ...

 

 

Link to comment
Share on other sites

Passing something ByRef means that you are passing a pointer.

Edited by TheXman
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...