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

Vector2/3/4 cannot be converted into JSON #1931

Open
prnxdev opened this issue Dec 20, 2020 · 7 comments
Open

Vector2/3/4 cannot be converted into JSON #1931

prnxdev opened this issue Dec 20, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@prnxdev
Copy link

prnxdev commented Dec 20, 2020

Describe the bug
I decided do create some simple system where there are some positions saved into DB. Ok so at the 'save this thing into database' line of code I tried to create JSON from Vector3 and get an error that it cannot be done because it's userdata.

To reproduce

outputDebugString(toJSON(Vector3(0, 0, 0)))

Expected behaviour
There should be JSON from Vector3

{"x": 0, "y": 0, "z": 0}

Version

  • MTA 1.5.8
  • Newest Windows 10
  • MTA server ran on WSL

Additional context
Meybe matrix should be JSON formattable also?

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

to keep backward compatibility, this kind of change require toJSON and fromJSON accept another argument ( which in general extend what can be converted ), i'm sure some servers already storing some kind of coords in json like above.

@prnxdev
Copy link
Author

prnxdev commented Dec 20, 2020

Another argument? I think everything needs to be done is to change a little toJSON function. Like adding another if (or changing existing one) and adding some toString rules.

@CrosRoad95
Copy link
Contributor

or, use metatables for this purpose, __tojson and __fromjson keys would be responsible for conversion

@sbx320
Copy link
Member

sbx320 commented Dec 22, 2020

Just changing toJSON doesn't really solve this, as you cannot use fromJSON to retrieve the original vector, since we have no way to determine whether something is a vector or a table in fromJSON. Ideally x == fromJSON(toJSON(x)) should hold for any x, so I'd be a bit hesitant to just add it to toJSON.

or, use metatables for this purpose, __tojson and __fromjson keys would be responsible for conversion

__tojson would be an option for userdata/tables. However how would __fromjson work? We can't select the metatable if we don't know the content of a json string. We need type information from somewhere before we can figure out how to decode the string.

@DREFTHUN
Copy link

Just use toJSON({0,0,0}). And with this you can even unpack it and set the position of an element with setElementPosition(element, unpack(fromJSON(pos))). I don't think it's necessary to use Vector3 in this case.

@prnxdev
Copy link
Author

prnxdev commented Dec 23, 2020

I think that only toJSON function should be changed because in future if something will be needed to parsing JSON or stringifying JSON we will be able to modify one function and not every toJSON method inside every object/class.

Btw. we can use this:

Vector3(fromJSON(something))

and it works. Btw. it's not even mentioned on wiki that developers can use table as parametr for Vector3.create method (aka constructor).

@prnxdev
Copy link
Author

prnxdev commented Dec 23, 2020

I don't know how toJSON is implemented but for this case Factory Design Pattern would be cool solution.

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

No branches or pull requests

4 participants