The new DocumentV2 API makes dealing with documents a lot easier. I need to copy one document and make some modifications on the fly. Using DocumentV2.traverse() combined with getFieldValue() and the BatchUpdate makes this quite easy.
However the process of creating such a DocumentV2 instance seems a bit cumbersome:
- make the RPC call to load the source document (this gives me a JsonNode)
- convert the
JsonNode into a JSON String
- Create an
IDocumentSerializer, a DocumentDeserializationConfig and Consumer<RankedNotification>
- Convert the JSON String into an
IDocument using the IDocumentSerializer
- convert this
IDocument to a DocumentV2 using an IDocumentV1V2Converter
Is there a more straightforward way that requires less copying/converting of the document?
Hi @thomas-soft-grove!
Could you describe in some more detail why you want to copy the document, and in what context?
Some remarks based on my current incomplete understanding of your situation:
- a DocumentV2 by itself should not normally need to get copied: modifications happen with incremental copy-on-write, so the original document stays unchanged
- A json String can be deserialized directly to DocumentV2, see description in the following GetA12 section
- Regarding direct JsonNode → DocumentV2 deserialization: There is no kernel API for this direct conversion yet, but it will be added in 29.4.0 (A12K-3165).
- Is the RPC call you mention a call to Data Services?
I am cloning an existing document to a new document that contains most, but not all fields of the source document.
Regarding the deserialization of a Json String I still need a DocumentServiceFactory from which I get the IDocumentSerializer which seems to be only possible if the code runs inside a DataServices server as the DocumentServiceFactory requires an IDocumentModelResolver.
However, it seems that I can’t use the DocumentV2 API in my case. This is done in an application that is not a DataServicesApplication, so I don’t have access to a DocumentServiceFactory or rather the needed IDocumentModelResolver.
As discussed via Webex, it seems that documents can indeed not be used here because no corresponding document model is available. (an IDocumentModelResolver can be implemented by projects independent from DataServices, but it would nonetheless need access to the DMs)
Hi @thomas-soft-grove,
has your question been answered or do you need further support? If it is solved, please, use the checkbox to mark the solution to your problem so that other users also know what helped in your case.
Thanks in advance!
Denise from the Discourse team