{"record":{"id":"8fdaf801a187ad06","repo":"elsa-workflows/elsa-core","slug":"cannot-deserialize-value-gettype-to-typeof-t","errorCode":null,"errorMessage":"Cannot deserialize {value.GetType()} to {typeof(T)}.","messagePattern":"Cannot deserialize (.+?) to (.+?)\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/clients/Elsa.Api.Client/Extensions/JsonObjectExtensions.cs","lineNumber":90,"sourceCode":"        options ??= new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };\n\n        if (value is JsonObject jsonObject)\n            return JsonSerializer.Deserialize<T>(jsonObject, options)!;\n\n        if (value is JsonArray jsonArray)\n            return JsonSerializer.Deserialize<T>(jsonArray, options)!;\n\n        if (typeof(T).IsEnum || (Nullable.GetUnderlyingType(typeof(T))?.IsEnum ?? false))\n        {\n            if (value.GetValueKind() == JsonValueKind.Null)\n                return default!;\n            return (T)Enum.Parse(Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), value.ToString());\n        }\n\n        if (value is JsonValue jsonValue)\n            return jsonValue.GetValue<T>();\n\n        throw new NotSupportedException($\"Cannot deserialize {value.GetType()} to {typeof(T)}.\");\n    }\n\n    /// <summary>\n    /// Sets the property value of the specified model.\n    /// </summary>\n    /// <param name=\"model\">The model to set the property value on.</param>\n    /// <param name=\"value\">The value to set.</param>\n    /// <param name=\"path\">The path to the property.</param>\n    public static void SetProperty(this JsonObject model, JsonNode? value, params string[] path)\n    {\n        model = GetPropertyContainer(model, path);\n        model[path.Last()] = value?.SerializeToNode();\n    }\n\n    /// <summary>\n    /// Sets the property value of the specified model.\n    /// </summary>\n    /// <param name=\"model\">The model to set the property value on.</param>","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/clients/Elsa.Api.Client/Extensions/JsonObjectExtensions.cs#L72-L108","documentation":"JsonObjectExtensions.GetNullableValue/GetValue-like conversion helper in Elsa.Api.Client throws NotSupportedException when a JSON node value cannot be converted to the requested type T. The helper handles enums, JsonValue nodes, strings, and a few primitives; any other JsonNode subtype (e.g. JsonObject or JsonArray) falls through to this throw.","triggerScenarios":"Calling the extension with T that does not match the underlying JSON value, e.g. the property is a nested object or array (JsonObject/JsonArray) but T is a primitive like int or string.","commonSituations":"Reading custom properties from workflow definitions where a value is a dictionary or list but the client code tries to read it as a scalar; schema drift between Studio-created properties and client expectations.","solutions":["Check the JsonNode kind before calling (ValueKind / is JsonObject, is JsonArray) and use JsonSerializer.Deserialize<T> for object/array nodes","Change T to a matching type (e.g. Dictionary<string,object>, JsonElement)","Wrap in try-catch (NotSupportedException) and provide a default"],"exampleFix":"// before\nvar count = json.GetPropertyOrDefault<int>(\"customProps\");\n// after\nvar props = json.GetPropertyOrDefault<Dictionary<string, object?>>(\"customProps\");","handlingStrategy":"type-guard","validationCode":"var node = json[\"prop\"];\nif (node is not (JsonValue or null)) throw new InvalidOperationException(\"Expected a scalar JSON value\");","typeGuard":"static bool IsScalarJsonNode(JsonNode? n) => n is JsonValue or null;","tryCatchPattern":"try { value = json.GetPropertyOrDefault<T>(name); } catch (NotSupportedException) { value = default; }","preventionTips":["Check node kind (is JsonObject/is JsonArray) before scalar conversion","Match T to the actual JSON value shape","Keep client property types in sync with workflow definition schemas"],"tags":["dotnet","json","deserialization","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}