1

Most of the json serializations work using object property accessors like getter and setter methods. I am trying to serialize third party object with no get/set methods(and I don't have control to modify source) to json and send over REST service.

But the final json produced doesn't have all the properties data as like in my object. This is obvious due to no accessor methods.

Is there any other way I can prepare JSON in this scenario? Otherwise, is there any other way I can send this 3rd party object over my rest service as is without compromising on it's properties values? (I considered like object serialization and send using streams, but that looks like unconventional).

San
  • 726
  • 2
  • 9
  • 18
  • Transform those third-party objects to objects that you control and are easy to serialize/deserialize (and vice-versa). Or write a specific JSON serializer/deserializer for the third-party class. – JB Nizet Apr 19 '19 at 19:54
  • @JBNizet as said, I don't have access to this library so I cannot modify. Due to set/get methods from the object, I cannot prepare or copy those values to my own object. – San Apr 19 '19 at 20:00
  • google to the rescue https://stackoverflow.com/questions/7105745/how-to-specify-jackson-to-only-use-fields-preferably-globally – Toerktumlare Apr 20 '19 at 00:50

1 Answers1

1

Maybe most. GSON on the other hand uses reflection to directly setup fields. You actually have to force it to not use reflection (see Gson avoid reflection).

So one solution would be to use that library. And just to be precise: gson uses reflection to identify the fields in your bean classes directly, without relying on getters/setters.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • do you mean gson works even though the class doesn't have getter and setter methods? – San Apr 19 '19 at 19:55
  • It works partly, but not the whole json representation of the object. Basically I am trying to print whole json on this below object. [https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/criterion/LogicalExpression.html] – San Apr 19 '19 at 20:52