Hey 
Is there an easy way to create an IGroupInstance out of a JsonNode?
Or has someone implemented some code for that already that we could use?
The goal is to create a custom partial modify document operation that can set groups in a document.
Hi @gero-wide-spruce !
Do I understand correctly that you have a larger document, initially represented
by its root JsonNode (loaded from the serialized String representation of the
document). From this larger document, you are interested in just one subgroup
instance (including all sub-subgroups and fields inside that subgroup). You want
to convert just this subgroup, but not the whole document (for better
performance?), similar to what IDocumentSerializer.deserialize does for a whole
document. The result of this conversion would be a set of IGroupInstances and
IFieldInstances (in case of IDocument V1) or one GroupInstanceV2 (in case
of DocumentV2). After some modifications by the caller of your “modify document
operation”, the updated set of IGroupInstances and IFieldInstances (or updated
GroupInstanceV2) would be converted back to one JsonNode (analogous to
IDocumentSerializer.serialize), which would then be used to replace the
corresponding descendant JsonNode below the original root JsonNode of the
document.
If this is what you want to achieve, I suggest the following:
-
The current IDocumentSerializer does not directly support this, but the
following workaround may be viable:
-
- In a copy of your root JsonNode, remove all siblings of the group
instance of interest, and all siblings of its ancestors.
-
- Pass the resulting trimmed-down JSON to IDocumentSerializer.deserialize
or IDocumentV2Serializer.deserializeV2 (currently, this requires converting
the JsonNode back to a String, but the upcoming kernel version should
directly accept a JsonNode too, when A12K-3165 is done).
-
- The resulting document will contain only the EntityInstances from the
group of interest. If you want the group to be represented in isolation
(roughly as if it was itself the root of a document, not as a subgroup of
its ancestor groups): In the case of DocumentV2, you can obtain the group
like myDocument.group(“path/to[2]/subgroup/instance[3]/with/repetitions”);
in the case of IDocument (V1), you’d have to use custom code to remove the
prefixes of the paths and repetitions of the IGroupInstances and
IFieldInstances in the document.
-
- After the subgroup of interest has been updated, put it back into the
deserialized document, serialize, cut the subgroup from the serialized
document, and put it at the right place below the original root JsonNode.
This workaround should allow to update the subgroup in a way that avoids the
document-deserialization of unrelated parts of the document. Until A12K-3165 is
available, this approach does not yet avoid the JSON<->String (de)serialization of
the full document, but JSON<->String (de)serialization should be much more
performant than document-deserialization, so this limitation may be
unproblematic performance-wise.
-
Please note that the official document serializer should really be used -
while ad-hoc implementations may or may not work in cases where all fields are
of type String, there are a lot of inconsistencies and edge-cases that can
arise, especially for fields of other types.
-
Note that, as mentioned in the description of the workaround, the subgroup’s
JsonNode would not be represented by a single IGroupInstance, but by separate
IGroupInstances and IFieldInstances for the subgroup itself and all
sub-subgroups and fields within the subgroup, if IDocument (V1) is used.
However, since DocumentV2 is not natively supported by Data Services yet, it
may not yet be an option to use DocumentV2.
Hi @gero-wide-spruce,
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
Hey @denise-narrow-token 
My question has been answered by @sebastian-nimble-slate, yes. However, I decided to implement it like this:
- get the IDocument from the document service
- serialize the document i want to update to a JsonNode
- perform the update on the JsonNode
- deserialize the node to a IDocument again
- send it to the DefaultDocumentService to update the document