I'm trying to map a JsonObject (System.Text.Json) with AutoMapper 12.0.0
Source:
public record Request
{
public Guid RequestId { get; set; }
public JsonObject AdditionalParameters { get; set; }
}
Destination:
public record ResultsEvent
{
public Guid RequestId { get; init; }
public JsonObject AdditionalParameters { get; init; }
}
The code which getting an error is the following:
var resultsEvent = _mapper.Map<ResultsEvent>(request);
The error I get:
---> System.InvalidOperationException: The node already has a parent.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NodeAlreadyHasParent()
at System.Text.Json.Nodes.JsonNode.AssignParent(JsonNode parent)
at lambda_method319(Closure , Object , ResultsEvent, ResolutionContext )
--- End of inner exception stack trace ---
I saw the following answer but I wonder if there is a more elegant way than setting it manually.
Just for clarity, with NewtonSoft.Json I don't get this error.
I tried to clone it in the AutoMapper profile and serializing/deserializing but nothing worked.