Creating a DocumentV2 from "plain" Java values

We need to create DocumentV2 instances based on data retrieved from a database table.

The DocumentV2 API offers a convenient way to create an instance. However, it seems I can only pass Strings to to putFieldValue() or FieldInstanceV2.ofValue()

So the following will fail with an IllegalArgumentException

    List<UpdateAction> updates = List.of(
        UpdateAction.putFieldValue("/Root/Name", "Arthur"),
        UpdateAction.putFieldValue("/Root/Amount", 42)
    );

Based on some threads here in the forum, it seems I have to use an instance of IDmAwareDocService to create values other than strings - however the input is always as string. So I retrieve an Integer from the database, then I need to convert it to a String, which is then converted back to a FieldInstanceV2.

This seems quite an overhead given that IDocumentV2Serializer will happily serialize a JSON string that contains an integer.

To make my live easier, I am considering creating a valid JSON string, then use IDocumentV2Serializer to create a DocumentV2. But that means I first create a JsonNode object (using Jackson) serialize that to a String then convert that to a DocumentV2 which seems a lot of intermediate steps.

My question basically boils down to: is there an easier (=more efficient) way to directly create a Document where I know that the values are all correct (=instances of the correct class)?

Hi @thomas-soft-grove,

you can pass a value with type Object to the methods UpdateAction.putFieldValue() and FieldInstanceV2.ofValue(), not only String. The concrete types which you should pass to the methods are documented in the enum BasicTypeV2.

That’s true, but you don’t get an Integer-instance out of it, but an instance of BigDecimal.

The safest way to set values coming from a source other than another DocumentV2-instance in a document is using the method IDmAwareDocService.convertToJavaTypeV2(...) (as you mentioned), especially for all date related values. If you know that the value for a number field is correct, and you know that you are getting an Integer, you can try creating an instance of BigDecimal with that value and setting it with the methods mentioned above.

Thanks, using the types defined in the (internal) BasicTypeV2 enum works.

Using DocumentV2.withBatchUpdates() is substantially faster than using a JsonNode!