Any straight forward way to convert javax.json.JsonObject to jakarta.json.JsonObject?

Viewed 247

When starting the new Jakarta (9) project, I need to handle old (legacy libraries and modules) using the "javax" JSON-P API. Usually we end with this error:

java: incompatible types: jakarta.json.JsonObject cannot be converted to javax.json.JsonObject

Unfortunately, the old code uses various javax.json.* classes, especially the JsonObjects, that are not usable in the new module's methods (as they are using jakarta.json.JsonObject). We worked this around with producing the string out of the first JsonObject. Then we deserialized it into the other class instance, but is there any other more straightforward way to "retype" or convert the old javax JsonObject instances to new jakarta instances? And vice versa?

1 Answers

You are asking for a straightforward way - I think there is a common data format your code and the one you want to call understand: JSON So the straightforward way would be to serialize your data, then parse it with the other parser. Short in lines of code, it may not be the fastest way to get the result.

Another option could be to walk the object class structure (it is a hierarchy), and with every step visited you construct a parallel object hierarchy in the other model. Thy may be more lines of code but also has a chance to perform better.

Related