Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Retreating custom data from Inventory Item.

Discussion in 'Economy' started by OddOwlI, Jan 22, 2023.

  1. OddOwlI


    Jun 5, 2022
    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.

    Code (JavaScript):
    1. {
    2.     "LEVELS": [{
    3.         "ATTACK": 4,
    4.         "CLASS": "GUNNER",
    5.         "DESC": "Your not getting away.",
    6.         "HEALTH": 5,
    7.         "MANA": 3,
    8.         "MIN_XP": 0,
    9.         "NAME": "Hunter",
    10.         "RACE": "HUMAN",
    11.         "RARITY": 1,
    12.         "SEX": "FEMALE",
    13.         "SKILL": ""
    14.     }],
    15.     "XP": 0
    16. }
    C# Object definition:

    Code (CSharp):
    1. public class LEVEL
    2. {
    3.     public int ATTACK { get; set; }
    4.     public string CLASS { get; set; }
    5.     public string DESC { get; set; }
    6.     public int HEALTH { get; set; }
    7.     public int MANA { get; set; }
    8.     public int MIN_XP { get; set; }
    9.     public string NAME { get; set; }
    10.     public string RACE { get; set; }
    11.     public int RARITY { get; set; }
    12.     public string SEX { get; set; }
    13.     public string SKILL { get; set; }
    14. }
    16. public class UnitData
    17. {
    18.     public List<LEVEL> LEVELS { get; set; }
    19.     public int XP { get; set; }
    20. }
    Code For Getting Item data:
    Code (CSharp):
    1. var inventoryItems = await EconomyService.Instance.Configuration.GetInventoryItemsAsync();
    2. var data = inventoryItems[0].CustomDataDeserializable.GetAs<UnitData>()
    Line 4 errors out with the following error:
    Code (csharp):
    1. DeserializationException: Unable to deserialize object.
    2. Unity.Services.Economy.Internal.Http.JsonObject.GetAs[T] (Unity.Services.Economy.Internal.Http.DeserializationSettings deserializationSettings) (at Library/PackageCache/
    Which is basically useless. Now if i remove the
    public int HEALTH { get; set; }
    from the LEVEL Class it will get the following error...

    Code (csharp):
    1. DeserializationException: Could not find member 'HEALTH' on object of type 'LEVEL'. Path 'LEVELS[0].HEALTH', line 1, position 81.
    2. Unity.Services.Economy.Internal.Http.JsonObject.GetAs[T] (Unity.Services.Economy.Internal.Http.DeserializationSettings deserializationSettings) (at Library/PackageCache/
    which is a very reasonable and us-full error.

    Doing some tracing in the GetAs methodon line 85 of Library/PackageCache/ 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.

  2. MiTschMR


    Aug 28, 2018
    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:
  3. OddOwlI


    Jun 5, 2022
    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.