Unity-Technologies/UnityCsReference · error · ArgumentException

Invalid JSON value: ${jsonValue}

Error message

Invalid JSON value: ${jsonValue}

What it means

Thrown by ModeService.DeepClone for any JSON value type it does not explicitly clone. The method only handles JSONObject (mapped to Dictionary<string,object>), bool, double, and string; any other runtime type — including null, JSON arrays (typically List<object>), nested numbers of other types, or custom objects — falls through to the ArgumentException.

Source

Thrown at Editor/Mono/EditorMode/ModeService.cs:922

                    var clonedList = new List<object>(listValue.Count);
                    foreach (var item in listValue)
                        clonedList.Add(DeepClone(item));
                    return clonedList;
                }

                var dictionaryValue = jsonValue as JSONObject;
                if (dictionaryValue != null)
                {
                    var clonedDict = new Dictionary<string, object>(dictionaryValue.Count);
                    foreach (DictionaryEntry kvp in dictionaryValue)
                        clonedDict.Add((string)kvp.Key, DeepClone(kvp.Value));
                    return clonedDict;
                }

                if (jsonValue is bool || jsonValue is double || jsonValue is string)
                    return jsonValue;

                throw new ArgumentException("Invalid JSON value: " + jsonValue);
            }
        }
    }
}

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure all JSON leaf values are bool, double, or string; convert integer leaves to double or string before insertion.
  2. Wrap arrays inside a JSONObject (object with keyed entries) or extend the clone routine if you control it.
  3. Validate the JSON tree shape before passing it to the mode service: reject null leaves and non-conformant types.
  4. Catch ArgumentException around mode-service calls and surface a clearer mode-descriptor error.

Example fix

// before
dict["counts"] = new List<object>{ 1, 2, 3 }; // array leaf -> throws

// after
dict["count"] = (double)1; // supported scalar leaf
Defensive patterns

Strategy: try-catch

Validate before calling

static bool IsCloneableJsonValue(object v) => v is bool || v is double || v is string || v is JSONObject || v == null ? (v != null ? true : false) : false; // reject null and unsupported

Type guard

static bool IsValidJsonLeaf(object v) => v is bool || v is double || v is string;

Try / catch

try { var clone = ModeService.DeepClone(value); } catch (ArgumentException ex) { Debug.LogError($"Unsupported JSON value: {ex.Message}"); }

Prevention

When it happens

Trigger: DeepClone receives a value that is null (none of the is-checks match), an array/List (not a JSONObject), a long/float boxed as something other than double, or an arbitrary object placed into the mode descriptor JSON tree.

Common situations: A mode descriptor JSON contains arrays or null values that the clone routine cannot represent; a JsonUtility/JSON parser that materializes integers as long instead of double; mode service extensions feeding hand-built dictionaries with unsupported leaf types.

Understand the failure class

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/15e1ad3058cbb058. Report an issue: GitHub.