Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Ditto » [Ditto java client] how to convert java dto object to JsonValue
[Ditto java client] how to convert java dto object to JsonValue [message #1849594] Wed, 26 January 2022 13:17 Go to next message
Andrii Kratik is currently offline Andrii KratikFriend
Messages: 2
Registered: January 2022
Junior Member
Hello

In order to send ditto protocol message I execute:
dittoClient.sendDittoProtocol(Adaptable.newBuilder(TopicPath.newBuilder(ThingId.of(toThingId(device.getThingName())))
                            .twin()
                            .commands()
                            .merge() 
                            .build())
                            .withPayload(Payload.newBuilder(JsonPointer.of("/features"))
                                    .withValue(org.eclipse.ditto.json.JsonObject.newBuilder()
                                            .set("someKey", someJsonValue)
                                            .build())
                                    .build())
                            .build())


someJsonValue can be simple value or JsonObject/JsonField.
For simple value it can be created using builder, but what about complex dto objects conversion?
For example if I do:
    private JsonField getDesiredProperty(FeatureName featureName, Object valueDto) {
        return JsonField.newInstance(featureName.getFeatureName(),
                        org.eclipse.ditto.json.JsonObject.newBuilder()
                                .set("desiredProperty", JsonValue.of(gson.toJson(valueDto)))
                                .build());
    }


The issue with JsonValue.of(gson.toJson(valueDto)) is that valueDto is converted as plain String. No internal key/value fields are parsed.

Is there any convenient way to convert java DTO obcect to JsonField/JsonValue?
Re: [Ditto java client] how to convert java dto object to JsonValue [message #1849604 is a reply to message #1849594] Wed, 26 January 2022 16:21 Go to previous message
Andrii Kratik is currently offline Andrii KratikFriend
Messages: 2
Registered: January 2022
Junior Member
found solution:
JsonFactory.newObject(gson.toJson(valueDto))
Previous Topic:Receiving responses from Ditto twin commans over Hono MQTT adapter
Next Topic:Policy for public access to some attributes
Goto Forum:
  


Current Time: Sun Dec 04 05:49:08 GMT 2022

Powered by FUDForum. Page generated in 0.02439 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top