Retreating custom data from Inventory Item.

Trying to get the custom data from an item in the Economy Service. As outlined towards the bottom the json is being deserialized by the Newtonsoft JsonConvert.DeserializeObject call in the GetAs method however its failing somewhere in the ValidateObject method with no meaningful error.

JSON:

{
    "LEVELS": [{
        "ATTACK": 4,
        "CLASS": "GUNNER",
        "DESC": "Your not getting away.",
        "HEALTH": 5,
        "MANA": 3,
        "MIN_XP": 0,
        "NAME": "Hunter",
        "RACE": "HUMAN",
        "RARITY": 1,
        "SEX": "FEMALE",
        "SKILL": ""
    }],
    "XP": 0
}

C# Object definition:

public class LEVEL
{
    public int ATTACK { get; set; }
    public string CLASS { get; set; }
    public string DESC { get; set; }
    public int HEALTH { get; set; }
    public int MANA { get; set; }
    public int MIN_XP { get; set; }
    public string NAME { get; set; }
    public string RACE { get; set; }
    public int RARITY { get; set; }
    public string SEX { get; set; }
    public string SKILL { get; set; }
}

public class UnitData
{
    public List<LEVEL> LEVELS { get; set; }
    public int XP { get; set; }
}

Code For Getting Item data:

var inventoryItems = await EconomyService.Instance.Configuration.GetInventoryItemsAsync();
var data = inventoryItems[0].CustomDataDeserializable.GetAs<UnitData>()

Line 4 errors out with the following error:

DeserializationException: Unable to deserialize object.
Unity.Services.Economy.Internal.Http.JsonObject.GetAs[T] (Unity.Services.Economy.Internal.Http.DeserializationSettings deserializationSettings) (at Library/PackageCache/com.unity.services.economy@3.0.0/Runtime/Generated/Runtime/Http/JsonObject.cs:104)

Which is basically useless. Now if i remove the public int HEALTH { get; set; } from the LEVEL Class it will get the following error…

DeserializationException: Could not find member 'HEALTH' on object of type 'LEVEL'. Path 'LEVELS[0].HEALTH', line 1, position 81.
Unity.Services.Economy.Internal.Http.JsonObject.GetAs[T] (Unity.Services.Economy.Internal.Http.DeserializationSettings deserializationSettings) (at Library/PackageCache/com.unity.services.economy@3.0.0/Runtime/Generated/Runtime/Http/JsonObject.cs:100)

which is a very reasonable and us-full error.

Doing some tracing in the GetAs methodon line 85 of Library/PackageCache/com.unity.services.economy@3.0.0/Runtime/Generated/Runtime/Http/JsonObject.cs The Newtonsoft JsonConvert.DeserializeObject returns successfully with the parsed json as a C# object(as seen in the image). The next line where the ValidateObject call is made is where the exception is thrown, well somewhere under it. However like I said before the error is net very helpful.

I am only posting this here because it get passed the Netwonsoft deserializeation call, I was going to post elsewhere(not the unity forum) until i noticed that.

Thanks
~Chris

1 Like

Make sure that your classes use the System.Serializable attribute, this may already be the mistake.

Another way you could do that is by using Newtonsoft.Json directly: How to parse this json result without making class in C# - Stack Overflow

Thanks for the suggestion unfortunately that did not help. I thought about using Newtonsoft directly and will likely do that in the interim to get moving again. Though I would prefer to use the Economy SDK in case they do or start to do any UGS spastic things.

1 Like