-1

I am working on an Unity Project which I use new CloudSave system. On my game normally I use serialization/ deserialization and write the game data to a file. I serialize/deserialize the custom data type: "GameData".

However, now I need to use both systems. writing/ reading on file and cloud as well. and this is first time for me. (since Unity CloudSave is new system, there is almost no source)

I am trying to convert a dictionary to JSON string where dictionary has one key and some of values. I want to have a JSON with only values of the dictionary.

to load the data from Unity Cloud I call LoadHandler() method in CouldHandler.cs from an other trigger script which returns me GameData type result.

CouldHandler.cs:

using System.Collections;
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.CloudSave;
using Unity.Services.Core;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
using System.Xml;
using UnityEditor.Experimental.RestService;
using System;
using Unity.VisualScripting;
using System.Text.Json;
using Newtonsoft.Json.Linq;

public class CloudHandler : MonoBehaviour
{

    public static string PLAYER_CLOUD_KEY = "PLAYER_DATA";
    public static int counter = 0;
    public static GameData gameData;
    public static Dictionary<string, string> data;
  
    public static string myJson;

    public static GameData LoadHandler()
    {
        LoadData();
        return gameData;
    }

    public static async void LoadData()
    {
        if (counter == 0)
        {
            await UnityServices.InitializeAsync();
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            counter++;
        }


        data = await CloudSaveService.Instance.Data.LoadAsync(new HashSet<string> { PLAYER_CLOUD_KEY});
       
        
       //string INeedAValueHere =  // I NEED HERE JSON FROM DICTIONARY [data]
       //GameData INeedAnotherValueHere =  // I NEED HERE GAMEDATA FORMAT FROM JSON 

        string myString = data[PLAYER_CLOUD_KEY];
        char[] MyChar = { '{', '}'};
        myString = myString.TrimStart(MyChar);
        myString = myString.TrimEnd(MyChar);

        myJson= JsonConvert.SerializeObject(myString);
        gameData = JsonUtility.FromJson<GameData>(myJson);

        Debug.Log($"myStore:  {myJson}"); // out come: "Panda":false,"Peacock":false,"RedPanda ":false 


    }
}

GameData.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Unity.VisualScripting;
using UnityEngine;

[System.Serializable]
public class GameData
{

    public bool Panda, Peacock, RedPanda, Sloth, Tapir;
   
    public GameData()
    {


        this.Panda = false; this.Peacock = false; this.RedPanda = false; this.Sloth = false; this.Tapir = false;
 
    }
}

I need a variable in Json string format as well as I need a variable in GameData format

I canNOT claim that my c# is so sharp!! but anyway at max I could get this outcome.

"Panda":false,"Peacock":false,"RedPanda ":false 

above outcome may look like a JSON string. But unity is not writing this values to the file. It is just deleting all earlier values.

and when I want to print the values from my file Unity prints it as below format. Therefore I thought I do something wrong and Unity is not recognizing myJson variable as Json string.

{
   "Alligator" : false,
   "Alpaca" : false,
   "Antelope" :false
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Sirhot Bay
  • 63
  • 6
  • 3
    "I could not convert this to proper JSON string" but what is the difference? Pls explain – Serge Jul 01 '23 at 21:41
  • do you want convert string to json the same this like "https://stackoverflow.com/a/22870647/10193401" – abolfazl sadeghi Jul 01 '23 at 21:43
  • Honestly it's hard to understand your problem as currently written. Might you please [edit] your question to share a [mcve] -- i.e. code + JSON sufficient to reproduce the problem by copying into a console app? You don't need to include all the hundreds of values, just two or three is sufficient. – dbc Jul 01 '23 at 22:28
  • 1
    That being said, your two JSON samples are **semantically identical**. `{"Alligator":false,"Alpaca":false,"Antelope":false}` is unformatted while the "proper JSON string" is formatted, but according to the [JSON standard](https://www.json.org/), whitespace formatting is ignored. If you really need formatting for some reason see [How do I get formatted JSON in .NET using C#?](https://stackoverflow.com/q/2661063), specifically [this answer](https://stackoverflow.com/a/2661117) for Json.NET, or [this answer](https://stackoverflow.com/a/74572568/3744182) for System.Text.Json. – dbc Jul 01 '23 at 22:33
  • Thank you all. I tried to explain better. here is my problem maybe not knowing what kind of syntax should I use to convert/ cast data between. I will appreciate if you can have look again. – Sirhot Bay Jul 01 '23 at 23:37
  • Again, we almost certainly need to see a [mcve] to help you -- code that can be copied **into a console app** and debugged that demonstrates the problem. There's no way we can debug your current code because 1) we don't have access your to `CloudSaveService`. 2) We can't log into your `AuthenticationService`. 3) We may not even have Unity licenses so we can't use `MonoBehaviour`. – dbc Jul 02 '23 at 17:41
  • You should be able to make a standalone console app that hardcodes the contents of your `Dictionary data` field with the contents that would have been downloaded from your `CloudSaveService`, then proceeds to demonstrate the problem. If you can do that, we can help. – dbc Jul 02 '23 at 17:43

1 Answers1

1

Assuming that your data is stored as JSON on the dictionary you retrieve from the cloud, all you have to do is directly convert the string you get instead of removing '{' and '}' from it.

myJson = data[PLAYER_CLOUD_KEY];
gameData = JsonUtility.FromJson<GameData>(myJson);
Houtamelo
  • 271
  • 1
  • 6
  • That's not exactly what I was looking for but, thank you so much, you light an idea. So I kept converting the data Json to Object and Object to Json and then Json to Object! So then it became readable to Unity!! it works now! – Sirhot Bay Jul 04 '23 at 09:40