How to implement with AutoBean a list of different base types like String, Integer, etc.?

StackOverflow https://stackoverflow.com/questions/13221018

  •  27-11-2021
  •  | 
  •  

سؤال

I want to create a request for JSON-RPC with three parameters - String, Integer and my own object. Request should look like this:

{"method":"MyMethod", "params":["text", 123, {"name": "any text", "num": 15}], "id":1}

Ideally, I would like to create an AutoBean like this (but it does not work):

interface JsonRpcRequest {  

    String getJsonrpc();
    void setJsonrpc(String value);

    String getMethod();
    void setMethod(String value);

    List<Object> getParams(); // ERROR: Type Object may not be used
    void setParams(List<Object> params); // ERROR: Type Object may not be used

} 

interface JsonRpcRequestFactory extends AutoBeanFactory {

    AutoBean<JsonRpcRequest> jsonRpcRequest();

}

The problem is that the AutoBean framework does not allows the use of List<Object> inside interface.

Is there another way to create a list/array of elements of different based and non-based types?

هل كانت مفيدة؟

المحلول

No, you simply can't. AutoBean requires everything to be statically typed: no polymorphism, and no mixed-typed lists of maps.

You might be interested by RequestFactory's built-in support for JSON-RPC though.

نصائح أخرى

Why do your params all need to be passed back in a list? Surely you're not going to do the same thing with a String, an Integer, and another Object! Just send them all back separately.

Further, you're not sending a custom Object over the JSON, you're sending the objid of that object... so just send the Integer id and let the server handle it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top