Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved JsonUtility.FromJson throwing errors

Discussion in 'Scripting' started by andyz, Jul 11, 2023.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,128
    I've marked this as a bug as should JsonUtility really be able to throw an error?

    Code (CSharp):
    1. ArgumentException: JSON must represent an object type.
    2. UnityEngine.JsonUtility.FromJson (System.String json, System.Type type)
    But JsonUtility.FromJson can throw an error if the json is bad, is there no alternative to make it return null for a class say?
    It is hard to use such an API if it can be broken by bad JSON rather than failing to return anything useful. Can't it be made safe so it never throws an error?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    JsonUtility isn't a general-purpose Json serializer. As you an read in the docs for JsonUtility.FromJson, there are plenty of limitations. There needs to be a way for JsonUtility to notify its users if an incompatible Json input is used and to provide information about why it's not compatible.

    Exceptions are the standard way of handling these kinds of errors in C#. E.g. Json.Net will also throw if you give it Json it cannot properly parse (and you don't explicitly "handle" the error through their own mechanism).

    And there's also a standard way to work with exceptions in C#: try/catch. It's not that the API is broken or unsafe, the exception is part of the API and you need to catch it if you can expect an exception to be thrown during normal operation of your code.

    Unity's documentation is definitely lacking in telling you which methods can throw which exceptions, though.
     
    andyz and Bunny83 like this.
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,128


    I know it's limited but kind of useful for simple deserialization. But it should indeed say something and the docs mention no exceptions on this.
    HOWEVER - it appears perfectly good at raising an exception I see now, so time for a manual error to be reported.
    Unity MUST document when exceptions are raised!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You must be thinking that when Unity throws an error it is Unity's fault.

    That's not a thing. Errors are for you.

    Errors are to communicate to you, "You have done something wrong or something bad has happened."

    It's not like some engineer had an idea and rubbed his hands together gleefully and said "I'm gonna wait for andyz and give him an ArgumentException... watch this!"

    New to software development? :)

    Almost every piece of software stands between many other pieces of software, and often an exception is generated somewhere else and passed back through.

    That's... the point.

    If you want it all "made safe" you can always
     try/catch
    Pokemon (or diaper-) style, but that NEVER ends well, and that's how software becomes glitchy and unreliable because failures go undetected and manifest as weird bizarre other failures and glitches and nobody has any idea what happened because oh look the database has been throwing some arcane "Out of index keys exception" for the last 7 months and all of our customer data is now corrupted. Thanks Pokemon, your diaper is full!!

    https://wiki.c2.com/?PokemonExceptionHandling
     
    Bunny83 likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    That statement is kinda confusing. What do you consider "safe"? Throwing an exception IS a safety measure and indicates broken input. Just ignoring the error and return null doesn't make it any safer, just more unpredictable. That would be much harder to debug since you get zero information back. Exceptions should not be used as normal control flow. However they are specifically meant for "exeptional" situations, especially when you're parsing user data.

    Exceptions are your friend in such cases. Would you mind showing what text / string you actually pass to the method?
     
    Kurt-Dekker likes this.
  6. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,128
    No - never had problems with exception handling except when Unity decides to not mention when it can happen!
     
  7. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,128
    Yes exceptions are fine so that is fine - but the documents did not say anything about throwing exceptions - I do not expect to need try catch around every function call - never been a big issue before as most documentation clearly states what exceptions will be thrown if any.
     
  8. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,128
    Kurt I appreciate your help generally - but if my code (many years of coding) throws an error then it means I did not follow safe coding practices in most cases.
    The problem here is just bad documentation - I do not want to guess when exceptions can be thrown so I did not try catch.