Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
JSON... Issue with the url — Gideros Forum

JSON... Issue with the url

jimlevjimlev Member
edited January 8 in General questions
Hello everyone !
I try to implement a JSON loading of datas for the level's datas of my game.
But, I encounter a strange issue.

If I use the wiki example for JSON, it works great.
But when I've tried to modify the example for my own usage, I didn't achieve to make it works.
So, after a lot of try and fails, I just tried to make the wiki example work with a different value for the JSON url... without success.

Here is the wiki example :
https://wiki.giderosmobile.com/index.php/Json
require('json')
local loader = UrlLoader.new("<a href="http://ip-api.com/json" rel="nofollow">http://ip-api.com/json</a>")
loader:addEventListener(Event.COMPLETE, function(event)
local receivedTable=json.decode(event.data)
print(receivedTable.countryCode)
print(receivedTable.timezone)
end)
Here are the urls I tried :
http://ip-api.com/json (work)
https://jimlev.alwaysdata.net/test.json (error : Expected value but found T_END at character 1)
http://jsonkeeper.com/b/UIDW (error : Expected value but found invalid token at character 1)

Anything I miss ? :#
My meditation plan :
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔

Comments

  • SinisterSoftSinisterSoft Maintainer
    edited January 8
    Take a look at event.data, there must be something the json decoder doesn't like...

    eg, on the line just after loader, put this:
    print(event.data)
    if it's a table, just put this instead:
    dump(event)
    and use this code:
    function dump(tt,indent,done)
      done=done or {}
      indent=indent or 0
      if type(tt)=="table" then
        for key,value in pairs(tt) do
          if type(value)=="table" and not done[value] then
            done[value]=true
            print(rep(" ",indent)..(format("[%s] => table",tostring(key))))
            dump(value,indent+2,done)
          else
            print(rep(" ",indent)..(format("[%s] => %s",tostring(key),tostring(value))))
          end
        end
      else
        print(rep(" ",indent)..tt)
      end
    end
    I also have this at the top of my code:
    local sub,len,upper,lower,format,gsub,find,rep,ascii=string.sub,string.len,string.upper,string.lower,string.format,string.gsub,string.find,string.rep,string.byte

    Likes: MoKaLux, jimlev

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
  • jimlevjimlev Member
    edited January 8
    @SinisterSoft always here to help ! thanks a lot !

    I tried your additions in my code. That confirm what I was thinking : the code is ok, but the way / place the json is hosted seems to be the problem.

    I explain :

    http://ip-api.com/json >>> with that host, the dump gives me... the json content.

    https://jimlev.alwaysdata.net/test.json >>> on that url, nothing happen : no errors, no message, dump do nothing. If I change the url from https to http, I still have an error "Expected value but found T_END at character"

    http://jsonkeeper.com/b/UIDW >>> on that one, the dump gives me... an HTML header!!! how is it possible, I don't know but, in that case, now it's clear that the error ("invalid token at character 1") is given because of the < (first character of the HTML page).

    I'm going to investigate a bit more.


    SinisterSoft : could you, please, explain me briefly what is the use of that line ?

    local sub,len,upper,lower,format,gsub,find,...

    Likes: SinisterSoft

    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited January 8 Accepted Answer
    jimlev said:

    SinisterSoft : could you, please, explain me briefly what is the use of that line ?

    local sub,len,upper,lower,format,gsub,find,...
    I am not SinisterSoft but will try to answer :p
    This line simply simplifies the writing of the code, for example he assigns local upper to string.upper so this will shorten your code and your typing.

    EDIT: and as mentioned below will speed up your code :p

    Likes: SinisterSoft

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLux said:

    jimlev said:

    SinisterSoft : could you, please, explain me briefly what is the use of that line ?

    local sub,len,upper,lower,format,gsub,find,...
    I am not SinisterSoft but will try to answer :p
    This line simply simplifies the writing of the code, for example he assigns local upper to string.upper so this will shorten your code and your typing.
    No, its not :)
    http://lua-users.org/wiki/OptimisingUsingLocalVariables
    http://kiki.to/blog/2014/04/04/rule-3-allow-monkeypatching/

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • @MoKaLux , localizing functions also makes the code run faster but indeed it does not have an effect otherwise.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • btw i've found some things saying that in luau (which comes with next gideros version) this localizing is less useful:
    https://devforum.roblox.com/t/should-i-be-localizing-these-variablesfunctions/568955
    is it?

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • jimlevjimlev Member
    Ladies and Gentlemen, please, could we get back to MY JSON issue :D :relaxed:

    Likes: MoKaLux

    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    For the SSL case, if you are on the desktop player, check this: https://forum.gideros.rocks/discussion/comment/61529/#Comment_61529
  • SinisterSoftSinisterSoft Maintainer
    @jimlev It both makes the code easier to write (saves typing) and speeds up code on Lua (not yet sure about speed increases with Luau - but Luau overall gives a speed increase without modifying any code).

    @keszegh I'm not sure if getting the pointer to the function then calling that is still faster or maybe slower or the same speed in Luau. I can try do some tests though once @hgy29 does a new test build. :)

    Likes: keszegh

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    edited January 10
    @jimlev, not sure if you are still experimenting or what, but I tested your code here and got the same error, however here is what my browser says about that returned data:
    header("Content-type: application/json; charset=utf-8");
    <?php 
    header("Content-type: application/json; charset=utf-8");
    ?>
    {
    	"status": "success",
    	"country": "France",
    	"countryCode": "FR",
    	"region": "IDF",
    	"regionName": "Île-de-France",
    	"city": "Paris",
    	"zip": "75017",
    	"lat": 48.8323,
    	"lon": 2.4075,
    	"timezone": "Europe/Paris",
    	"isp": "France Telecom Orange",
    	"org": "",
    	"as": "AS3215 Orange S.A.",
    	"query": "90.127.14.20"
    }
    Notice the 'header' and PHP code in the returned data
  • jimlevjimlev Member
    edited January 10
    yes I'm still "experimenting" :tired_face:

    First of all, I didn't achieve to correct the httpS issue. I added the .dll files t my Gideros folder but with no result.

    When I try to load the JSON from non-https, the dump show that it's the redirect

    ex.:
    local loader = UrlLoader.new("http://jsonkeeper.com/b/UIDW")
    gives :
    "301 Moved Permanently..."


    I'm just surprised that the jsonkeeper service, which is made to deliver properly JSON files is not able to deliver through http and only https.
    I will also probably search for a SQL solution which can be a good alternative, may be more documented...
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • hgy29 said:

    @jimlev, not sure if you are still experimenting or what, but I tested your code here and got the same error, however here is what my browser says about that returned data:

    header("Content-type: application/json; charset=utf-8");
    <?php 
    header("Content-type: application/json; charset=utf-8");
    ?>
    {
    	"status": "success",
    	"country": "France",
    	"countryCode": "FR",
    	"region": "IDF",
    	"regionName": "Île-de-France",
    	"city": "Paris",
    	"zip": "75017",
    	"lat": 48.8323,
    	"lon": 2.4075,
    	"timezone": "Europe/Paris",
    	"isp": "France Telecom Orange",
    	"org": "",
    	"as": "AS3215 Orange S.A.",
    	"query": "90.127.14.20"
    }
    Notice the 'header' and PHP code in the returned data
    Note that I have modified 4 or 5 times that file this morning to try to solve the way the file is treated by the server and the header is "displayed"
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • Any other external data loading I can consider to load small 'structured' contents from outside Gideros as an alternative to JSON ?
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • jimlevjimlev Member
    edited January 15
    I finally achieve to make my JSON loading works.
    It still makes an error on gideros player on desktop but, when I try it on gideros player on my mobile, it works great.

    Now, I try to make the HTML5 export works but I have an error and I think it's still that damn JSON plugin.

    Here is the message I get :

    Oops, something went wrong...

    An unexpected error occured
    The details below may be helpful to you or the app developer... hopefully.
    exception thrown: TypeError: what.includes is not a function


    Error can be seen here : https://jimlev.alwaysdata.net/game/RapidoQuizz/

    It's the first time I use a plug-in with gideros. Do I need to add some files to the export folder ? I added the
    require("json")
    on first line of main.lua...



    Edit1: on export, I tried with Compression setting On and Off, PWA check or not, etc...
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • MoKaLuxMoKaLux Member
    edited January 15
    did you add json in plugins as well? (on the left in library)

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • In the gideros editor? Yes! And it works great when I test my app in mobile player...
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • hgy29hgy29 Maintainer
    Hey @jimlev, just before the exception in red, I see in the console 'Sorry: location xxx not allowed". This means that you explictly set a domain in the HTML5 export tab in the studio, and that domain doesn't match the one you are running from
    +1 -1 (+2 / -0 )Share on Facebook
  • jimlevjimlev Member
    edited January 16
    Thanks ! I tried a lot of parameters combinations anad that one was an attempt I forgot to clear.

    Now, I achieved to make something with my first JSON loading. It works and give me the params I need... But now, it's the second one who gives me an error... I'm going mad.


    edit : It's aliiiiivvve. hum hum... problem found. It works ;)
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
Sign In or Register to comment.