Jump to content

toJSON & fromJSON


Overkillz

Recommended Posts

Hello dear guys, Im trying to use the following functions which are toJSON and fromJSON

Well, I don't know when I use fromJSON I get some value changed. Why do I say this ? 

Well, in the table there are some booleans which will say if some objects will have "setElementCollisionsEnabled"  or not. When I try to update the table from the file updated using fromJSON, collision is always false. That is an example. Sometimes the error is the position or something similar.

I hope u can help me, thanks for reading.

This error only happens when I use toJSON and fromJSON. (I bet that the error is on string.gsub)

local tablaPrueba = {
	{"object","6959","3132.67969","-1632.39587","20.36549","0","90","0","0",true,"255","1"},
	{"object","6959","3132.67969","-1632.39551","61.70512","0","90","0","0",true,"255","1"},
	{"object","6959","3132.67969","-1592.40625","63.70512","0","90","0","0",true,"255","1"},
	{"object","6959","3132.67969","-1592.40625","69.70512","0","90","0","0",true,"255","1"}
}

--CREATING THE FILE
local fileHandle = fileCreate("tablaPrueba1.wha")
if fileHandle then
	local json = toJSON ( tablaPrueba ) 
	fileWrite(fileHandle,json)
	fileClose(fileHandle)
end

--LOADING THE FILE
file = fileOpen("tablaPrueba1.wha") 
if file then 
	local fileR = fileRead(file, fileGetSize(file))
	fileR = string.gsub(fileR, "{", "[{")
	fileR = string.gsub(fileR, "}", "}]")
	fileR = fromJSON(fileR)
	tablaPrueba = fileR
	fileClose(file)
end 

 

Edited by Overkillz
Link to comment
  • Moderators

You shouldn't be using string manipulate functions on JavaScript Object Notation, it might corrupt your file.

Make sure you check your string after you generate it, afaik it should be converted correctly both ways.

 


Debugging = understanding.

 

iprint(tablaPrueba)
local json = toJSON ( tablaPrueba ) 
iprint(json)

 

iprint(fileR)
fileR = fromJSON(fileR)
iprint(fileR)

 

  • Like 1
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...