Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJSON parses JSON in wrong way #1946

Open
prnxdev opened this issue Dec 24, 2020 · 15 comments
Open

fromJSON parses JSON in wrong way #1946

prnxdev opened this issue Dec 24, 2020 · 15 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@prnxdev
Copy link

prnxdev commented Dec 24, 2020

Describe the bug
Today I noticed that fromJSON function doesn't work for JSON collections. It returns only first found object inside object or collection.

To reproduce
Two things:

Run inside MTA this:

outputDebugString(fromJSON('{"name": "Piorun", "position": { "x": 0, "y": 0, "z": 0 }}')) 
-- you will get { x: 0, y: 0, z: 0 } in debug console

and open web console (ie. Google Chrome DevTools) and use this code:

JSON.parse('{"name": "Piorun", "position": { "x": 0, "y": 0, "z": 0 }}');
// you will get object { name: 'Piorun', position: { x: 0, y: 0, z: 0 } }

Expected behaviour
Should return this:

{ name: 'Piorun', position: { x: 0, y: 0, z: 0 } }

Screenshots
none

Version
everything newest

Additional context
none

@prnxdev prnxdev added the bug Something isn't working label Dec 24, 2020
@Pirulax
Copy link
Contributor

Pirulax commented Dec 25, 2020

It's because MTA is retarded.
You need an extra array, that is:

outputDebugString(fromJSON('[{"name": "Piorun", "position": { "x": 0, "y": 0, "z": 0 }}]')) 

@prnxdev
Copy link
Author

prnxdev commented Dec 25, 2020

It's really misleading. Since collections are inside [...] brackets it shouldn't look like this because there still is problem with them. I have this JSON (it is MongoDB output) which I want to parse:

[
  {
    "_id":"5fe513b7132fc155b571d479",
    "modelId":567,
    "position":{
      "x":0,
      "y":0,
      "z":0
    },
    "rotation":{
      "x":0,
      "y":0,
      "z":0
    },
    "__v":0
  }
]

and it's not working properly (tbh it's not working at all)

@prnxdev
Copy link
Author

prnxdev commented Dec 25, 2020

Ok don't know why but it's working when I added [...] inside this collection. outputDebugString shown me something different that's why I was confused. IMHO adding [...] is stupid and should be changed but for now - it works and I cant move forward.

@Pirulax
Copy link
Contributor

Pirulax commented Dec 25, 2020

Not sure if changing it would break backwards compatibility. The issue has been discussed before, although not sure in which issue (that is which #). Perhaps we should automatically add the [...]. Somehow..

@Pirulax
Copy link
Contributor

Pirulax commented Dec 25, 2020

Ah, I see. Its done like this so that this works:

local name, weapon, ammo = fromJSON("[\"Desert Eagle\", 24, 147]")

@DREFTHUN
Copy link

DREFTHUN commented Dec 25, 2020

Don't change it, please. It is working perfectly fine and all of my resources use this method and I really don't feel like rewriting every one of my resources just because one user isn't satisfied with it.

@prnxdev
Copy link
Author

prnxdev commented Dec 25, 2020

I think it should be added to 1.6 so it doesn't have to be backward compatible with 1.5.x

@DREFTHUN
Copy link

DREFTHUN commented Dec 25, 2020

This function was working the same way as today since 2008. Do you really think backwards compatibility isn't important? Everyone will have to rewrite every one of their resources since 2008 that is using JSON, just because you don't like the way it is working. Don't be selfish.

@prnxdev
Copy link
Author

prnxdev commented Dec 25, 2020

I'm not selfish? I just know that if I have 1.x version of anything it doesn't have to be backward compatible with 1.y (where y < x). It's just natural way of creating programs or modules and having different version of it. I just want MTA team to change this behavior to be the same as everywhere in other languages. Someone who wants to use JSON and is not into "JSON's MTA world" for long time (like me, I never used JSON in MTA tbh) must know about few "things" and I think that many will say "damn, that's stupid, why I have to add special brackets for this?".

.. just because you don't like the way it is working.

For me - it's bug because It's not parsing JSON properly. JSON format with added square bracket's isn't proper JSON format

Lastly: good programmer would create some simple one function to add those brackets (like I do) so everything that should be change would be only one function.

@Pirulax
Copy link
Contributor

Pirulax commented Dec 25, 2020

MTA is very backwards compatible, like we still have MTA 1.0 functions "deprecated". Also, every value in the array will result in a separate variable, eg.:

local v1, v2 = fromJSON('["value1", "value2"]')
print(v1, v2) -- value1, value2

What we could do: Add an argument which indicates there's only one value (thus no extra array needed).

@DREFTHUN
Copy link

Also backwards compatible doesn't mean that something written in 1.6 should work in 1.5.8. It means that something written in 1.5.8 should work in 1.6, and in the case that you are saying, that wouldn't work. This function works, and the first thing you should've done was try toJSON with a table and see the outcome, then adapt to it. Not open a bug report.

@StrixG
Copy link
Contributor

StrixG commented Dec 26, 2020

@pawel-miczka, your example is incorrect. It works as intended:
image

@prnxdev
Copy link
Author

prnxdev commented Dec 26, 2020

There's weird thing that happens to outputDebuString. It sometimes shows things in incorrect way. I will try to test fromJSON more since I am working on authorization system using external service (API). I will paste my results.

@DREFTHUN
Copy link

Use iprint.

@Pirulax
Copy link
Contributor

Pirulax commented Apr 16, 2021

It isn't parsing it in the wrong way, this is how fromJSON works.
A Lua function can return multiple values, and that's why the top level array is unpacked.
I agree it's not ideal. It's pretty easy to fix tho, a simple bUnpackTopLevelArray or similar flag can be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants