Skip to content

Commit 473a674

Browse files
Merge pull request #14 from jacek-marchwicki/serialize-more-than-gson
Serialize more than gson
2 parents 9c8a6ce + 61116cb commit 473a674

28 files changed

Lines changed: 586 additions & 295 deletions

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ class YourMessage {
8181
public String error;
8282
}
8383

84-
final RxJsonWebSockets rxJsonWebSockets = new RxJsonWebSockets(new RxWebSockets(new NewWebSocket(), SERVER_URI), new GsonBuilder().create(), Message.class);
85-
rxJsonWebSockets.webSocketObservable()
86-
.compose(MoreObservables.filterAndMap(RxJsonEventMessage.class))
87-
.compose(RxJsonEventMessage.filterAndMap(YourMessage.class))
84+
final RxObjectWebSockets webSockets = new RxObjectWebSockets(new RxWebSockets(new NewWebSocket(), SERVER_URI), new GsonSerializer(new Gson(), Message.class));
85+
webSockets.webSocketObservable()
86+
.compose(MoreObservables.filterAndMap(RxObjectEventMessage.class))
87+
.compose(RxObjectEventMessage.filterAndMap(YourMessage.class))
8888
.subscribe(new Action1<YourMessage>() {
8989
@Override
9090
public void call(YourMessage yourMessage) {
@@ -116,8 +116,8 @@ Imperative example:
116116
to your gradle file:
117117

118118
```groovy
119-
compile "com.appunite:websockets-java:2.0.0"
120-
compile "com.appunite:websockets-rxjava:2.0.0"
119+
compile "com.appunite:websockets-java:2.1.0"
120+
compile "com.appunite:websockets-rxjava:2.2.0"
121121
```
122122
123123
## License

websockets-rxjava-example/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies {
1919
compile 'io.reactivex:rxandroid:0.24.0'
2020
compile 'com.android.support:recyclerview-v7:21.0.0'
2121
compile 'com.google.guava:guava:18.0'
22+
compile "com.google.code.gson:gson:2.3"
2223
//compile "com.appunite:websockets:1.0"
2324

2425
testCompile "org.hamcrest:hamcrest-all:1.3"

websockets-rxjava-example/src/main/java/com/appunite/socket/MainActivity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
import com.appunite.websocket.NewWebSocket;
2727
import com.appunite.websocket.rx.RxWebSockets;
28-
import com.appunite.websocket.rx.json.RxJsonWebSockets;
28+
import com.appunite.websocket.rx.object.GsonObjectSerializer;
29+
import com.appunite.websocket.rx.object.RxObjectWebSockets;
2930
import com.example.Socket;
3031
import com.example.SocketConnection;
3132
import com.example.SocketConnectionImpl;
@@ -163,7 +164,8 @@ public RetentionFragment() {
163164
ADDRESS,
164165
ImmutableList.of("chat"),
165166
ImmutableList.<Header>of());
166-
final RxJsonWebSockets jsonWebSockets = new RxJsonWebSockets(webSockets, gson, Message.class);
167+
final GsonObjectSerializer serializer = new GsonObjectSerializer(gson, Message.class);
168+
final RxObjectWebSockets jsonWebSockets = new RxObjectWebSockets(webSockets, serializer);
167169
final SocketConnection socketConnection = new SocketConnectionImpl(jsonWebSockets, Schedulers.io());
168170
presenter = new MainPresenter(new Socket(socketConnection, Schedulers.io()), Schedulers.io(), AndroidSchedulers.mainThread());
169171
}

websockets-rxjava-example/src/main/java/com/appunite/socket/MainPresenter.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import android.util.Pair;
2020

2121
import com.appunite.detector.SimpleDetector;
22-
import com.appunite.websocket.rx.json.messages.RxJsonEvent;
23-
import com.appunite.websocket.rx.json.messages.RxJsonEventConn;
24-
import com.appunite.websocket.rx.json.messages.RxJsonEventDisconnected;
25-
import com.appunite.websocket.rx.json.messages.RxJsonEventMessage;
26-
import com.appunite.websocket.rx.json.messages.RxJsonEventWrongMessageFormat;
22+
import com.appunite.websocket.rx.object.messages.RxObjectEvent;
23+
import com.appunite.websocket.rx.object.messages.RxObjectEventConn;
24+
import com.appunite.websocket.rx.object.messages.RxObjectEventDisconnected;
25+
import com.appunite.websocket.rx.object.messages.RxObjectEventMessage;
26+
import com.appunite.websocket.rx.object.messages.RxObjectEventWrongMessageFormat;
27+
import com.appunite.websocket.rx.object.messages.RxObjectEventWrongStringMessageFormat;
2728
import com.example.Socket;
2829
import com.example.model.DataMessage;
2930
import com.google.common.collect.ImmutableList;
@@ -117,9 +118,9 @@ public ImmutableList<AdapterItem> call(ImmutableList<AdapterItem> adapterItems,
117118
.subscribe(addItem);
118119

119120
connected = socket.connectedAndRegistered()
120-
.map(new Func1<RxJsonEventConn, Boolean>() {
121+
.map(new Func1<RxObjectEventConn, Boolean>() {
121122
@Override
122-
public Boolean call(RxJsonEventConn rxJsonEventConn) {
123+
public Boolean call(RxObjectEventConn rxJsonEventConn) {
123124
return rxJsonEventConn != null;
124125
}
125126
})
@@ -158,11 +159,11 @@ public String call(Boolean connected) {
158159
};
159160
}
160161

161-
private Observable.Operator<Pair<String, String>, RxJsonEvent> liftRxJsonEventToPairMessage() {
162-
return new Observable.Operator<Pair<String, String>, RxJsonEvent>() {
162+
private Observable.Operator<Pair<String, String>, RxObjectEvent> liftRxJsonEventToPairMessage() {
163+
return new Observable.Operator<Pair<String, String>, RxObjectEvent>() {
163164
@Override
164-
public Subscriber<? super RxJsonEvent> call(final Subscriber<? super Pair<String, String>> subscriber) {
165-
return new Subscriber<RxJsonEvent>(subscriber) {
165+
public Subscriber<? super RxObjectEvent> call(final Subscriber<? super Pair<String, String>> subscriber) {
166+
return new Subscriber<RxObjectEvent>(subscriber) {
166167
@Override
167168
public void onCompleted() {
168169
subscriber.onCompleted();
@@ -174,17 +175,17 @@ public void onError(Throwable e) {
174175
}
175176

176177
@Override
177-
public void onNext(RxJsonEvent rxJsonEvent) {
178-
if (rxJsonEvent instanceof RxJsonEventMessage) {
179-
subscriber.onNext(new Pair<>("message", ((RxJsonEventMessage) rxJsonEvent).message().toString()));
180-
} else if (rxJsonEvent instanceof RxJsonEventWrongMessageFormat) {
181-
final RxJsonEventWrongMessageFormat wrongMessageFormat = (RxJsonEventWrongMessageFormat) rxJsonEvent;
178+
public void onNext(RxObjectEvent rxObjectEvent) {
179+
if (rxObjectEvent instanceof RxObjectEventMessage) {
180+
subscriber.onNext(new Pair<>("message", ((RxObjectEventMessage) rxObjectEvent).message().toString()));
181+
} else if (rxObjectEvent instanceof RxObjectEventWrongMessageFormat) {
182+
final RxObjectEventWrongStringMessageFormat wrongMessageFormat = (RxObjectEventWrongStringMessageFormat) rxObjectEvent;
182183
//noinspection ThrowableResultOfMethodCallIgnored
183184
subscriber.onNext(new Pair<>("could not parse message", wrongMessageFormat.message()
184185
+ ", " + wrongMessageFormat.exception().toString()));
185-
} else if (rxJsonEvent instanceof RxJsonEventDisconnected) {
186+
} else if (rxObjectEvent instanceof RxObjectEventDisconnected) {
186187
//noinspection ThrowableResultOfMethodCallIgnored
187-
final Exception exception = ((RxJsonEventDisconnected) rxJsonEvent).exception();
188+
final Exception exception = ((RxObjectEventDisconnected) rxObjectEvent).exception();
188189
if (!(exception instanceof InterruptedException)) {
189190
subscriber.onNext(new Pair<>("error", exception.toString()));
190191
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2015 Jacek Marchwicki <jacek.marchwicki@gmail.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License
15+
*/
16+
17+
package com.appunite.websocket.rx.object;
18+
19+
import com.google.gson.Gson;
20+
import com.google.gson.JsonParseException;
21+
22+
import java.lang.reflect.Type;
23+
24+
import javax.annotation.Nonnull;
25+
26+
public class GsonObjectSerializer implements ObjectSerializer {
27+
28+
@Nonnull
29+
private final Gson gson;
30+
@Nonnull
31+
private final Type typeOfT;
32+
33+
public GsonObjectSerializer(@Nonnull Gson gson, @Nonnull Type typeOfT) {
34+
this.gson = gson;
35+
this.typeOfT = typeOfT;
36+
}
37+
38+
@Nonnull
39+
@Override
40+
public Object serialize(@Nonnull String message) throws ObjectParseException {
41+
try {
42+
return gson.fromJson(message, typeOfT);
43+
} catch (JsonParseException e) {
44+
throw new ObjectParseException("Could not parse", e);
45+
}
46+
}
47+
48+
@Nonnull
49+
@Override
50+
public Object serialize(@Nonnull byte[] message) throws ObjectParseException {
51+
throw new ObjectParseException("Could not parse binary messages");
52+
}
53+
54+
@Nonnull
55+
@Override
56+
public byte[] deserializeBinary(@Nonnull Object message) throws ObjectParseException {
57+
throw new IllegalStateException("Only serialization to string is available");
58+
}
59+
60+
@Nonnull
61+
@Override
62+
public String deserializeString(@Nonnull Object message) throws ObjectParseException {
63+
try {
64+
return gson.toJson(message);
65+
} catch (JsonParseException e) {
66+
throw new ObjectParseException("Could not parse", e);
67+
}
68+
}
69+
70+
@Override
71+
public boolean isBinary(@Nonnull Object message) {
72+
return false;
73+
}
74+
}

websockets-rxjava-example/src/main/java/com/example/MoreObservables.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.example;
1818

19-
import com.appunite.websocket.rx.json.messages.RxJsonEvent;
19+
import com.appunite.websocket.rx.object.messages.RxObjectEvent;
2020

2121
import javax.annotation.Nonnull;
2222

@@ -26,7 +26,6 @@
2626
import rx.functions.Func1;
2727
import rx.internal.operators.OperatorMulticast;
2828
import rx.subjects.BehaviorSubject;
29-
import rx.subjects.PublishSubject;
3029
import rx.subjects.Subject;
3130

3231
public class MoreObservables {
@@ -81,11 +80,11 @@ public Object call(Throwable throwable) {
8180
};
8281
}
8382

84-
public static Observable.Operator<Object, RxJsonEvent> ignoreNext() {
85-
return new Observable.Operator<Object, RxJsonEvent>() {
83+
public static Observable.Operator<Object, RxObjectEvent> ignoreNext() {
84+
return new Observable.Operator<Object, RxObjectEvent>() {
8685
@Override
87-
public Subscriber<? super RxJsonEvent> call(final Subscriber<? super Object> subscriber) {
88-
return new Subscriber<RxJsonEvent>(subscriber) {
86+
public Subscriber<? super RxObjectEvent> call(final Subscriber<? super Object> subscriber) {
87+
return new Subscriber<RxObjectEvent>(subscriber) {
8988
@Override
9089
public void onCompleted() {
9190
subscriber.onCompleted();
@@ -97,7 +96,7 @@ public void onError(Throwable e) {
9796
}
9897

9998
@Override
100-
public void onNext(RxJsonEvent rxJsonEvent) {}
99+
public void onNext(RxObjectEvent rxObjectEvent) {}
101100
};
102101
}
103102
};

0 commit comments

Comments
 (0)