question

Jean-Roch Roy avatar image
Jean-Roch Roy asked

JSON.parse/JSON.stringify corrupting int arrays

Hello,

I'm using Shared Group Data to store int arrays that change frequently. (I'm aware that SGD should be restricted to small groups of players, but as a temporary solution we use it for all our players, and we intend to find a better solution if/when our title scales up.)

Whenever a player completes an activity and gets a score in our game, it gets added to a list in SGD via a CloudScript function, reproduced here:

var MAX_SCORES = 200;


handlers.submitScore = function(args)
// Adds a new score to the specified activity
{
    var environment = args.environment; // string
    var activityId = args.activityId; // string
    var score = args.score; // int: the new score
    
    var sharedGroupId = "Scores_" + environment;
    var result = null;
    var scores = null;


    try
    {
        result = server.GetSharedGroupData({ SharedGroupId: sharedGroupId, Keys: [ activityId ] });
    }
    catch (e)
    {
        log.error("GetSharedGroupData: Error: " + e.message);
        return;
    }


    if (result == null)
    {
        log.info("GetSharedGroupData: No result");
        scores = [score];
    }
    else if (!result.Data.hasOwnProperty(activityId))
    {
        log.info("GetSharedGroupData: Activity '" + activityId + "' not found");
        scores = [score];
    }
    else
    {
        var scoresAsJson = result.Data[activityId].Value;
        scores = JSON.parse(scoresAsJson);

        scores.push(score);
        scores = scores.slice(-MAX_SCORES); // only keep the last MAX_SCORES values
    }
    
    var scoresAsJson = JSON.stringify(scores);
    log.info("scores: " + scoresAsJson + " (" + scores.length + " elements)");
    
    var data = {};
    data[activityId] = scoresAsJson;
    server.UpdateSharedGroupData({ SharedGroupId: sharedGroupId, Data: data });
}

There seems to be a recurring problem where sometimes a comma is missing between two values in these lists, eg it contains '[40,46,17,4533,99]' instead of '[40,46,17,45,33,99]' (notice the comma between 45 and 33). I suspect the problem is with JSON.parse()/JSON/stringify(), perhaps because the function is called too frequently, but I haven't been able to confirm.

Has anyone else encountered this problem? Thank you.

CloudScriptShared Group Data
2 comments
10 |1200

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

Hernando avatar image Hernando commented ·

Thank you for your feedback. How often does the problem occur? Would you tell us your Title ID so that we can look into it and help to troubleshoot ?

0 Likes 0 ·
Jean-Roch Roy avatar image Jean-Roch Roy Hernando commented ·

Thanks for the reply! The problem happens at least a few times a day. Title ID: 67F9.

0 Likes 0 ·

0 Answers

·

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.