Sign In Register

How can we help you today?

Start a new topic
Answered

JSON string to ScriptData(GSData)

This is probably a weird question. 


I found that my more complex nested data from a Response works pretty well when using GSData object from a response. I get to do things such as ScriptData.GetObject("foo").GetObject("foo2").GetStringList("fooList"); and this actually seems to work. huzzah.


however there is nothing is nothing in the json libs that easliy let's me deserialize the the Scriptdata.json as an offline cache. So right now I need to breakdown to the ScriptData from a response and store them into simpler objects that can be serialized and loaded later.


What I would like to do is just do something like

LongTermStorageSave("file", ScriptData.JSON )

and then do something like

GSData data = new GSData( json );


and then get the original response.scriptdata object to navigate and work with.







Best Answer

Thanks David for you response. It's a massive improvement over what I had previously and it's something I can work with more than what I had. I will clarify the ideal goal and maybe some one will know how to achieve the ideal. 


I was unable to get those methods to do exactly what I was asking for. I did manage to get some form of what I was asking for working.  But here is what I have and was trying to do.

"scriptData":{"result":"success",
"doc":{
"_id":"576212f992c95a70de92500b",
"_dataVersion":"0.0",
"infoItem":{"createdAt":1.467036855389E12,
"name":"DefaultName",
"level":1.0,
"xp":0.0,
},
......

  What I'm trying to achieve is to take the response.GetData("doc").JSON and store this as a local cache of info. This is the easy part. The great part of response GSData is that I can do.

outputMethod  response.scriptData.GetGSData("doc").GetGSData("itemInfo").getInt("createdAt").

This is fantastic. it's pretty wordy to type. However this is just the simple part. I have more nested dictionaries, such as named collectable items, and they have individual values. So there is more than just itemInfo as part of the root doc. But this sample of how it works is great.


I want to be able to restore the GSData object. I would like to 

GSData doc = SomeMethodToGSData( json )


and then be able to repeat the same as being able to access the same way as response.scriptData.

outputMethod  doc.GetGSData("itemInfo").getInt("createdAt") 


The thing is. The GSJson.From/To did not do this. Instead I get a large set of nested Dictionary<string,object>. which is still improvement than Uniti's json utility which can't handle nested dictionaries :(


So to be able to access createdAt of infoItem I need to

Dictionary<string, object> doc = GSJson.From( json ) // level 1 basic and always expected when restoring the cache info

Dictionary<string, object> infoItem = doc["infoItem"]

Debug.log( infoItem["createdAt"] ) 


Not so bad if it's one level... however the system does go deeper, numerous nested times deeper. And to access that deep nested data requires

Dictionary<string, object> collectableCard = doc["cards"];

Dictionary<string,object> specificCard = cards[ cardID ];

Dictioanry<string,object> battleMetrics =  specificCard[ "battleMetrics" ]

Dictionary<string,object> powersUsedMetric = battleMetrics["powerMetric"] // fourth level of nested data


Using GSJson tool only returns a dictionary. Now I'm super grateful this actually works at all. Last week I didn't have this. I needed to break down the download on the server side. Into lists of strings, and rebuild the strings of everything. So this Dictionary casting at least is an improvement.


However if there is a way to get package into a GSData object so I can do

GSData doc = ToJSData( json ) // the original root

output doc.GetGSData("cards").getGSData( cardID ).getGSData( "battleMetritcs" ).getGSData( "powerMetrics").getGSData( powerID ).getInt("used")

^^^^^^ This is way better than the Dictionary<string,object> casting for every layer of nested data. If there is a way to get this. I will be giddy. If I need to use dictionary casting, I will be happy. Please oh please, let there be a way to be giddy :D











You have the GSJson.From() and GSJson.To() methods.

Answer

Thanks David for you response. It's a massive improvement over what I had previously and it's something I can work with more than what I had. I will clarify the ideal goal and maybe some one will know how to achieve the ideal. 


I was unable to get those methods to do exactly what I was asking for. I did manage to get some form of what I was asking for working.  But here is what I have and was trying to do.

"scriptData":{"result":"success",
"doc":{
"_id":"576212f992c95a70de92500b",
"_dataVersion":"0.0",
"infoItem":{"createdAt":1.467036855389E12,
"name":"DefaultName",
"level":1.0,
"xp":0.0,
},
......

  What I'm trying to achieve is to take the response.GetData("doc").JSON and store this as a local cache of info. This is the easy part. The great part of response GSData is that I can do.

outputMethod  response.scriptData.GetGSData("doc").GetGSData("itemInfo").getInt("createdAt").

This is fantastic. it's pretty wordy to type. However this is just the simple part. I have more nested dictionaries, such as named collectable items, and they have individual values. So there is more than just itemInfo as part of the root doc. But this sample of how it works is great.


I want to be able to restore the GSData object. I would like to 

GSData doc = SomeMethodToGSData( json )


and then be able to repeat the same as being able to access the same way as response.scriptData.

outputMethod  doc.GetGSData("itemInfo").getInt("createdAt") 


The thing is. The GSJson.From/To did not do this. Instead I get a large set of nested Dictionary<string,object>. which is still improvement than Uniti's json utility which can't handle nested dictionaries :(


So to be able to access createdAt of infoItem I need to

Dictionary<string, object> doc = GSJson.From( json ) // level 1 basic and always expected when restoring the cache info

Dictionary<string, object> infoItem = doc["infoItem"]

Debug.log( infoItem["createdAt"] ) 


Not so bad if it's one level... however the system does go deeper, numerous nested times deeper. And to access that deep nested data requires

Dictionary<string, object> collectableCard = doc["cards"];

Dictionary<string,object> specificCard = cards[ cardID ];

Dictioanry<string,object> battleMetrics =  specificCard[ "battleMetrics" ]

Dictionary<string,object> powersUsedMetric = battleMetrics["powerMetric"] // fourth level of nested data


Using GSJson tool only returns a dictionary. Now I'm super grateful this actually works at all. Last week I didn't have this. I needed to break down the download on the server side. Into lists of strings, and rebuild the strings of everything. So this Dictionary casting at least is an improvement.


However if there is a way to get package into a GSData object so I can do

GSData doc = ToJSData( json ) // the original root

output doc.GetGSData("cards").getGSData( cardID ).getGSData( "battleMetritcs" ).getGSData( "powerMetrics").getGSData( powerID ).getInt("used")

^^^^^^ This is way better than the Dictionary<string,object> casting for every layer of nested data. If there is a way to get this. I will be giddy. If I need to use dictionary casting, I will be happy. Please oh please, let there be a way to be giddy :D










You can create a GSData object with  

IDictionary<string, object> parsedJSON = (IDictionary<string, object>)GSJson.From(data);
GSData data = new GSData(parsedJSON );

 

Thank you very much. Your assistance has been extremely valuable :)

G(old).

Thanks, David.

Login to post a comment