#Please share exactly how you call it
1 messages · Page 1 of 1 (latest)
An example
class RBS_CargoCb : RestCallback
{
array <string> GetCargoFromJSON(string valueMain)
{
SCR_JsonLoadContext reader();
reader.LoadFromFile("$profile:/Relibus/cargo.json");
ref CargoFromServer cargo = NewFromReader(reader, valueMain);
PrintFormat("name: %1 container: %2",mc.CargoId, cargo.Container);
return cargo.Container;
}
}
ref CargoFromServer NewFromReader(SCR_JsonLoadContext loadctx, string valueMain)
{
ref CargoFromServer cargo = new CargoFromServer();
loadctx.ReadValue(valueMain, cargo );
loadctx.ReadValue("CargoId", cargo.CargoId);
loadctx.ReadValue("Container", cargo.Container);
return cargo;
}
class CargoFromServer
{
string CargoId;
array<string> Container;
}
in $profile:/Relibus/cargo.json
{"cargo1":{"CargoId":"cargo1","Container":["aaa", "sss"]},"cargo2":["aaa", "sss"]}
"cargo1": {
"CargoId": "cargo1",
"Container": [
"aaa",
"sss"
]
},
"cargo2": [
"aaa",
"sss"
]
}
you are mixing upper and lower case
cargo2 is incorrect but Im trying to retrieve cargo1 anyway
write the json file from script once. Then you see how it should look like
i have
{
FileHandle cargoFile = FileIO.OpenFile("$profile:/Relibus/cargo.json", FileMode.WRITE);
if(cargoFile)
{
cargoFile.Write(data);
cargoFile.Close();
} else{
PrintFormat("failed to open and write %1", cargoFile);
}
```
and remove theses
json save context has their own write to file
use that as making it a string in script first and then writing is vastly more expensive
Thanks! I will look closer. I thought the given datatype (array<string>) isn't supported in serialisation
they are, including map<string, string> etc
it's super wierd. types like string, int are serialised well but with array<string> im keep getting storage is: 0x0000000000000000 {} 



but now im reading not from the json file.
array<string> GetFromJSON(string data) {
SCR_JsonLoadContext reader();
reader.ImportFromString(data);
InventoryServerModel mc = NewFromReader(reader);
return mc.Storage;
}
it's from GET request
{"username":"","storage":["(6BCAA9E5BC957693)Prefabs/Characters/HeadGear/Hat_ZmijovkaCap_01/Hat_ZmijovkaCap_01_black.et","(50F8B1F928F93ABD)Prefabs/Characters/Uniforms/Shirt_Turtleneck_01/Shirt_Turtleneck_01_beige.et","(5A8C9A020BD7
ABA6)Prefabs/Characters/Uniforms/Pants_Trousers_01/Pants_Trousers_01_DarkGrey.et"]}```
it's working with string, int, float types. But there's no chance to get it working with array<string>
start simple to find out where your code fails. the load context supports it
Also fun fact while we are at it, serialize ResourceName directly, we have a nice internal handling for it so you get the readable paths in diag env but in production it will just the guids, saving on data size
Because in this case your InventoryServerModel object will be deleted at the end of the scope, so at the end of the method. And probably because you keep the ref to the array only in this object, it will become null
wow! thanks! it works!
ref array<string> GetFromJSON(string data) {
SCR_JsonLoadContext reader();
reader.ImportFromString(data);
InventoryServerModel mc = NewFromReader(reader);
return mc.Storage;
}
class InventoryServerModel
{
string Username;
ref a
rray<string> Storage;
}
works!