Skip to main content



      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 08:17 Go to next message
Eclipse UserFriend
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 11:21 Go to previous message
Eclipse UserFriend
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: Tue Jul 08 17:22:38 EDT 2025

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

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

Back to the top