Recommended way to read Entity data? (Unity, C#)

  • Hi, I'm experimenting with brainCloud in Unity (C#) and I'm trying to read a Global Entity. I can get the JSON of the Entity object but then what's the best way to access the "data" key inside? I can do it in a convoluted way but I'm hoping there's a friendlier way using the API.
    Here's some sample code with questions in the comments:

    string entityId = "whatever";
    SuccessCallback successCallback = (jsonResponse, cbObject) =>
    	// Unity's JsonUtility doesn't support Dictionaries so I guess I should use brainCloud's internal JsonReader?
    	// And is there a more specific class I can Deserialize to, or should I just use generic Dictionaries?
    	var responseDict = JsonReader.Deserialize<Dictionary<string, object>>(jsonResponse);
    	var entityDict = responseDict["data"] as Dictionary<string, object>;
    	var myDataDict = entityDict["data"] as Dictionary<string, object>;
    	// Now I have my data as a Dictionary, but ideally I want to deserialize it into my own custom class type...
    	// So should I *re-serialize* it back to JSON so I can read it? This seems unnecessary.
    	var myDataJson = JsonWriter.Serialize(myDataDict);
    	var myData = JsonReader.Deserialize<MyCustomClass>(myDataJson);
    	// Now I have my data in the correct form!
    FailureCallback failureCallback = (status, reasonCode, jsonError, cbObject) => {};
    _bc.GlobalEntityService.ReadEntity(entityId, successCallback, failureCallback);

    Seems like there's probably an easier way to do it, just wondering what you recommend?

  • Hi Henry,

    Yes the data received is in JSON. And brainCloud expects a JSON when you update the entity. What you can do is create a serialize and a deserialize method inside your custom type.

Log in to reply