Parse a List of Storage Objects in Unity

I currently have a collection of “event” objects that I send to the storage engine. When I retrieve a list of all events for a user, they come back as a JSON string like this:
Cursor: , Objects: [

Collection: events,
CreateTime: 2020 - 05 - 09T02: 53:58Z,
Key: circa5 / 8 / 2020 10:54:06 PM,
PermissionRead: 1,
PermissionWrite: 1,
UpdateTime: 2020 - 05 - 09T02: 53:58Z,
UserId: 16cbab61 - 62b4 - 479e-a8d5 - 8cad0dfd444a,
Value: {
     "Stop": "5/8/2020 10:53:51",
    "Start": "5/8/2020 9:53:51", 
    "Category": "2"
    }, 
Version: 1d2c8e71a3d3fea64346ac9ed1bbb899,
   , 
Collection: events, 
CreateTime: 2020 - 05 - 09T11: 41:20Z, 
Key: circa5 / 9 / 2020 7:41:29 AM, 
PermissionRead: 1, 
PermissionWrite: 1, 
UpdateTime: 2020 - 05 - 09T11: 41:20Z, 
UserId: 16cbab61 - 62b4 - 479e-a8d5 - 8cad0dfd444a, 
Value: {
     "Stop": "5/9/2020 7:39:43", 
     "Start": "5/8/2020 11:00:43", 
     "Category": "1"
      }, 
Version: 3217ca7796d46ba3b1984daad2d3229b, 
],

When I make Serializable classes for Objects and Values to parse through the response, I keep getting errors to the point that I’m manually editing the chars of the JSON response in order to get objects from the string. Is there any straightforward way to serialize these responses?

@EarthAdam What does your entity class look like that you serialize to and from? What JSON library do you have in your project to use as the decoder?

@novabyte I’m using the JsonUtility from UnityEngine, and with that trying a mix of ways to match with either the “EventResponse” or “EventList” classes like so:
var result = await client.ListUsersStorageObjectsAsync(session4, "events", session4.UserId, limit, null);
EventResponse eventResponse = new EventResponse();
eventResponse = JsonUtility.FromJson<EventResponse>(result);

Here are the classes:

[System.Serializable]
public class EventObject
{
    public int start;
    public int stop;
    public int category;
}

[System.Serializable]
public class EventList
{
    public string Collection;
    public string CreateTime;
    public string Key;
    public string PermissionRead;
    public string PermissionWrite;
    public string UpdateTime;
    public string UserId;
    public EventObject Value;
    public string Version;
}
[System.Serializable]
public class EventResponse
{
    public string Cursor;
    public EventList Objects;
}

Turns out you don’t have to de-serialize it at all, aside from once for the Value. Here’s the fix:

public async void Load_Events()
    {
        var session4 = await client.AuthenticateEmailAsync(email, password, username);
        Debug.LogFormat("New event: {0}, {1}", session4.Created, session4);

        //Check for devices associated to account
        const int limit = 100; // default is 10.
        var result = await client.ListUsersStorageObjectsAsync(session4, "events", session4.UserId, limit, null);
        foreach (var apiStorageObject in result.Objects)
        {
            var collection = apiStorageObject.Collection;
            var creationTime = apiStorageObject.CreateTime;
            var key = apiStorageObject.Key;
            var permissionRead = apiStorageObject.PermissionRead;
            var permissionWrite = apiStorageObject.PermissionWrite;
            var updateTime = apiStorageObject.UpdateTime;
            var userId = apiStorageObject.UserId;
            var version = apiStorageObject.Version;
            EventObject value = JsonUtility.FromJson<EventObject>(apiStorageObject.Value);
            print("Start: "+value.start);
            print("Stop: "+ value.stop);
            print("Category: "+ value.category);
            print("THIS IS THE TEST: "+collection + " " + creationTime + " " + key + " " + permissionRead + " " + permissionWrite + " " + updateTime + " " + userId + " " + version + " " + value);
        }
}
[System.Serializable]
public class EventObject
{
    public int start;
    public int stop;
    public int category;
}

@EarthAdam I’m glad its sorted. I didn’t realize its not clear that the JSON value from a storage object in the Unity client comes back as a JSON string which you can then decode to your data model. We should add an example to the docs for it.

1 Like

@novabyte I am not the sharpest on immediately knowing what to do with JSON data, so left to my own devices I start splitting the string at brackets and commas. Depending on how many other people are like me, having that in the docs would help!