3

I have this kind of JSON that needs to be converted to POJO using JsInterop:

            "ACL": {
                "*": {
                    "read": true
                },
                "asdf123asdf": {
                    "read": true,
                    "write": true
                }
            }

Where the asdf123asdf can be any property name. Here's one I have some up (partially)

@JsType(isNative = true, namespace= JsPackage.GLOBAL, name="Object")
public class ACL {
    @JsProperty(name="*")
    String asterisk;
}

Is there a work around to implment the dynamic property name?

Jason Washo
  • 536
  • 6
  • 22
quarks
  • 33,478
  • 73
  • 290
  • 513
  • One option would be to entirely / dynamically generate your POJO based on the JSON content (annotations included), by using a code generation and manipulation library (e.g. Byte Buddy - http://bytebuddy.net/ - the most acclaimed nowadays). You could either generate Java code or directly bytecode, in a build step prior to regular compilation and you could also package your POJOs as a separate project module. That would be the type-safe option... – Octavian Theodor Apr 12 '17 at 11:10
  • You would probably need to take into account JSON property names that are not valid Java identifiers. Or, you could use a Map, right? That would be the second, type-unsafe option... – Octavian Theodor Apr 12 '17 at 11:31
  • 1
    @OctavianTheodor I don't think ByteBuddy can even work with GWT – quarks Apr 12 '17 at 13:15
  • I was actually thinking of generating the necessary POJOs not at runtime but in a build step, before actually compiling your app, which, in turn, depends on GWT. The generation part / code need not be included in the final application. It's true that this means to already know how the most comprehensive JSON will look like... – Octavian Theodor Apr 12 '17 at 14:10

0 Answers0