socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.34k stars 977 forks source link

Parse JSON into Java objects instead of JSONObject #235

Closed b95505017 closed 2 years ago

b95505017 commented 9 years ago

There are some of libraries e.g. Gson, Moshi, will help us parse json string into java object.

If public void call(Object... args) { could return the java object instead of JSONObject, I think it will be more convenient.

For example, in Retrofit, we could define the response java object, after response returned, it will call converters to help parsing the response string into java object, then we could use them directly instead of get the value one by one from JSONObject.

Encoder could also use Gson/Moshi to convert java object into json string automatically and send it.

nkzawa commented 9 years ago

Related to https://github.com/socketio/socket.io-client-java/issues/184, I have a thought to add an adapter which you can implement your own JSON parser, though it's still an idea and actually, it might be a bit difficult to support.

Alternatively, I think you would be able to use a mapper library like https://github.com/FasterXML/jackson-datatype-json-org for now.

Let me know if you have any ideas how we should support the feature.

emanuelet commented 8 years ago

+1. Do you think it will be possible (and feasible) to port what Retrofit2 is doing with adapters? As far as I saw, they declare an interface in the main package and then they declare the different adapters.

b95505017 commented 8 years ago

The easiest way is just output as String, then we could use any json convert library to transform our model, or passing into JsonObject for who still want to use old way.

emanuelet commented 8 years ago

For that matter I already doing that:

 private Emitter.Listener onReceived = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            ObjectMapper mapper = new ObjectMapper();
            RtmMessageJks msg = null;
            try {
                msg = mapper.readValue(args[0].toString(), RtmMessageJks.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String type = msg.getRtmType();
        }
    };

Also for some reasons that are not very clear to me I've been forced to convert the object with jackson to avoid memory freezes of gson.

I think anyhow, that having an adapter will be more efficient and clean in terms of implementation.

JackieKu commented 8 years ago

I think ideally it should decouple from any particular JSON implementation and let the client to choose their favorite. And org.json just sucks.

engineers-bojan commented 7 years ago

Is there any update on this proposed enhancement/feature? Socket-io client could really benefit by enabling us to easily integrate gson, jackson or some other json libraries for parsing POJO's..

Minikloon commented 7 years ago

I think just updating the dependency to the latest org.json would be good. The version packaged right now doesn't implement Iterable on JSONArray.

emanuelet commented 7 years ago

I agree, that will be a quick fix (considering that is running on version 20090211 that is 7 years old), but still I reckon the decoupling the implementation will be the way to go

nkzawa commented 7 years ago

@Minikloon @emanuelet The reason we use the old version of org.json is for Android.

emanuelet commented 7 years ago

what do you mean? the newer version doesn't work with Android?

nkzawa commented 7 years ago

@emanuelet no, it's incompatible.

ksv510 commented 6 years ago

I believe that would be great if you add an opportunity to get raw data, I mean byte[] or String etc. Then there will be a possibility to create an adapter with any serializer.

darrachequesne commented 2 years ago

For future readers:

I've added an example in the documentation: https://socketio.github.io/socket.io-client-java/faq.html#How_to_map_the_event_arguments_to_POJO

A raw packet will look like: <packet type><nsp><json stringified version of the args>, for example 2/admin["hello","world"].

The thing is, we need to parse the packet in order to extract the event name and potentially inject the binary attachments, so I don't see how we could provide an agnostic parser.

Please reopen if needed.