question

plourdegui avatar image
plourdegui asked

JSON Serialization and Deserialization optimizations

Context:

I have a successful implementation of CloudScript server round-trip involving serialization/deserialization of a complex nested serializable C# object (named PlayerDataModel or pdm).

Problem:

I feel it is not optimized because I find myself in need of many inefficient steps.

My Implementation:

1. Serialize the pdm from its object form into a string then send to Cloudscript via ExecuteCloudScript

PlayFabClientAPI.ExecuteCloudScript(
    new ExecuteCloudScriptRequest()
    {
        FunctionName = "SavePlayerDataModel",
        FunctionParameter = new
        {
            pdm = JsonUtility.ToJson(pdm)
        },
    }, 

...

2. CloudScript deserializes the pdm string to put it back into a javascript object form in order to be able to work with it.

handlers.SavePlayerDataModel = function(args, context) {
    let pdm = JSON.parse(args.pdm);
...
// work with pdm, reading/writing from/to its fields.

3. Convert the pdm javascript object back into a string to be able to store it in the the Player Data (Title)

let data = { };
data.PlayerDataModel = JSON.stringify(pdm);
server.UpdateUserData({
    "PlayFabId": currentPlayerId,
    "Data": data
});

4. Return the javascript object "pdm" to the client because it will have been modified by the server and the client needs to update to its latest state.

return {
    "pdm": pdm
};

5. The client deserializes the coming json stream to transform it back into a C# object

resultCallback: result =>
{
    JsonObject jsonResult = (JsonObject) result.FunctionResult;
    PlayerDataModel pdmFromServer = JsonUtility.FromJson<PlayerDataModel>(jsonResult["pdm"].ToString());


...

My Concern/Question:

I feel like there's probably lots of inefficiencies in this flow.

Could you please suggest a better way of achieving this?

Thanks and regards

apisCloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Andy avatar image
Andy answered

Marshalling a complex object via JSON is inherently inefficient. Fortunately, the code doesn't end up being terribly complex thanks to everything being abstracted away by well-understood JSON libraries. You could probably avoid being explicit about the serialization and deserialization on both ends of the initial call. I'm thinking something like this:

PlayFabClientAPI.ExecuteCloudScript(
  new ExecuteCloudScriptRequest()  
  {
    FunctionName = "SavePlayerDataModel",
    FunctionParameter = pdm // You might need the explicit ToJson call here, I'm not 100% sure
  },
},

and

handlers.SavePlayerDataModel = function(args, context) {
  let pdm = args;
...
// work with pdm, reading/writing from/to its fields.

The stringify into Player Data is necessary and then returning the updated data to the client is better than making the client call again to get the updated data.

Overall, I'm not seeing many opportunities for improvement.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.