{"record":{"id":"1d6f83732cb800b4","repo":"elsa-workflows/elsa-core","slug":"failed-to-parse-jsondocument-activityjsonconverter","errorCode":null,"errorMessage":"Failed to parse JsonDocument","messagePattern":"Failed to parse JsonDocument","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Core/Serialization/Converters/ActivityJsonConverter.cs","lineNumber":28,"sourceCode":"using Microsoft.Extensions.Logging;\n\nnamespace Elsa.Workflows.Serialization.Converters;\n\n/// <summary>\n/// (De)serializes objects of type <see cref=\"IActivity\"/>.\n/// </summary>\npublic class ActivityJsonConverter(\n    IActivityRegistry activityRegistry,\n    IExpressionDescriptorRegistry expressionDescriptorRegistry,\n    ActivityWriter activityWriter,\n    IServiceProvider serviceProvider)\n    : JsonConverter<IActivity>\n{\n    /// <inheritdoc />\n    public override IActivity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        if (!JsonDocument.TryParseValue(ref reader, out var doc))\n            throw new JsonException(\"Failed to parse JsonDocument\");\n\n        var activityRoot = doc.RootElement;\n        var activityTypeName = GetActivityDetails(activityRoot, out var activityTypeVersion, out var activityDescriptor);\n        var notFoundActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<NotFoundActivity>();\n\n        // If the activity type is a NotFoundActivity, try to extract the original activity type name and version.\n        if (activityTypeName.Equals(notFoundActivityTypeName) && activityRoot.TryGetProperty(\"originalActivityJson\", out var originalActivityJson))\n        {\n            activityRoot = JsonDocument.Parse(originalActivityJson.GetString()!).RootElement;\n            activityTypeName = GetActivityDetails(activityRoot, out activityTypeVersion, out activityDescriptor);\n        }\n\n        var clonedOptions = GetClonedOptions(options);\n        // If the activity type is not found, create a NotFoundActivity instead.\n        if (activityDescriptor == null)\n        {\n            var notFoundActivityDescriptor = activityRegistry.Find<NotFoundActivity>()!;\n            var notFoundActivityResult = JsonActivityConstructorContextHelper.CreateActivity<NotFoundActivity>(notFoundActivityDescriptor, activityRoot, clonedOptions);","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Serialization/Converters/ActivityJsonConverter.cs#L10-L46","documentation":"ActivityJsonConverter.Read converts incoming JSON into an IActivity. It first parses the JSON token into a JsonDocument; if TryParseValue fails (malformed or unexpected JSON), it throws this JsonException. It is a low-level guard ensuring only valid JSON objects are processed for activity deserialization.","triggerScenarios":"Deserializing workflow/activity JSON where the Utf8JsonReader is not positioned on a valid JSON value (truncated JSON, invalid syntax, or reading a primitive where a document is required) at ActivityJsonConverter.Read (src/modules/Elsa.Workflows.Core/Serialization/Converters/ActivityJsonConverter.cs:28).","commonSituations":"Passing hand-edited workflow JSON from files or database blobs that got corrupted; copying JSON with trailing commas or comments; deserializing a partial HTTP response body.","solutions":["Validate/pretty-check the JSON source with a parser before handing it to Elsa's serializer.","Fix malformed JSON (missing braces/commas, truncation) at the source of the string.","Ensure you deserialize with System.Text.Json options compatible with the payload (e.g. no embedded comments unless configured).","If data comes from storage, re-export the workflow definition to regenerate clean JSON."],"exampleFix":"// before\nvar activity = JsonSerializer.Deserialize<IActivity>(truncatedJson);\n\n// after\nusing var doc = JsonDocument.Parse(json); // throws a precise line/position error first\nvar activity = JsonSerializer.Deserialize<IActivity>(json);","handlingStrategy":"validation","validationCode":"try { using var _ = JsonDocument.Parse(json); } catch (JsonException ex) { throw new InvalidOperationException(\"Workflow JSON is malformed\", ex); }\nvar activity = JsonSerializer.Deserialize<IActivity>(json, options);","typeGuard":"static bool IsValidJson(string s) { try { using var d = JsonDocument.Parse(s); return true; } catch (JsonException) { return false; } }","tryCatchPattern":"try { var activity = JsonSerializer.Deserialize<IActivity>(json); } catch (JsonException ex) when (ex.Message.Contains(\"Failed to parse JsonDocument\")) { log.LogError(ex, \"Malformed activity JSON\"); /* repair or re-export payload */ }","preventionTips":["Round-trip workflow JSON through Elsa export rather than hand-editing","Validate JSON files in CI before deploying workflow definitions","Check transport/storage for truncation (body size limits, blob column sizes)"],"tags":["csharp","json","serialization","system-text-json"],"backgroundTag":"json-parse-error","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"}