Skip to content

Commit 45c18d6

Browse files
committed
Added nullable handler for JsonObject;
1 parent b0ea464 commit 45c18d6

File tree

18 files changed

+236
-27
lines changed

18 files changed

+236
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ To subscribe on notifications you just need to create `NotificationFilter` where
192192
</details>
193193

194194
### Working with User
195-
To create `User` you just need to call `createUser(String login, String password, User.RoleEnum role, StatusEnum status, JsonObject data)` method of `DeviceHive` class
195+
To create `User` you just need to call `createUser(String lastLogin, String password, User.RoleEnum role, StatusEnum status, JsonObject data)` method of `DeviceHive` class
196196

197197
```java
198198
DHResponse<User> response = deviceHive.createUser("javaLibTest", "123456", RoleEnum.ADMIN, StatusEnum.ACTIVE, null);
@@ -207,7 +207,7 @@ To subscribe on notifications you just need to create `NotificationFilter` where
207207

208208
Properties:
209209
* `id` (read only)
210-
* `login`
210+
* `lastLogin`
211211
* `role`
212212
* `password` (write only)
213213
* `data`

client/src/main/java/com/github/devicehive/client/service/User.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.github.devicehive.client.model.DHResponse;
2525
import com.github.devicehive.rest.model.RoleEnum;
26+
import com.github.devicehive.rest.model.UserInsert;
2627
import com.github.devicehive.rest.model.UserUpdate;
2728
import com.github.devicehive.rest.model.UserVO;
2829
import com.github.devicehive.rest.model.UserWithNetwork;
@@ -37,7 +38,7 @@ public class User {
3738
private String login;
3839
private RoleEnum role;
3940
private String password;
40-
private JsonObject data=new JsonObject();
41+
private JsonObject data = new JsonObject();
4142

4243
private User() {
4344
}
@@ -55,6 +56,16 @@ public static User create(UserWithNetwork user) {
5556
return result;
5657
}
5758

59+
public static User create(UserInsert user) {
60+
if (user == null) {
61+
return null;
62+
}
63+
User result = new User();
64+
result.setId(user.getId());
65+
result.setData(user.getData());
66+
return result;
67+
}
68+
5869
public static User create(UserVO user) {
5970
if (user == null) {
6071
return null;

client/src/main/java/com/github/devicehive/client/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public DHResponse<User> createUser(String login, String password, RoleEnum role,
7474
UserApi userApi = createService(UserApi.class);
7575
UserUpdate userUpdate = createUserBody(login, password, role, statusEnum, data);
7676
DHResponse<User> response;
77-
DHResponse<UserVO> result = execute(userApi.insertUser(userUpdate));
77+
DHResponse<UserInsert> result = execute(userApi.insertUser(userUpdate));
7878
response = DHResponse.create(User.create(result.getData()), result.getFailureData());
7979
if (response.isSuccessful()) {
8080
updateUser(response, login, role, data);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
*
3+
*
4+
* NullJsonAdapter.java
5+
*
6+
* Copyright (C) 2018 DataArt
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
*/
21+
22+
package com.github.devicehive.rest.adapters;
23+
24+
import com.google.gson.JsonDeserializationContext;
25+
import com.google.gson.JsonDeserializer;
26+
import com.google.gson.JsonElement;
27+
import com.google.gson.JsonNull;
28+
import com.google.gson.JsonObject;
29+
import com.google.gson.JsonParseException;
30+
31+
import java.lang.reflect.Type;
32+
import java.util.Objects;
33+
34+
public class NullJsonAdapter implements JsonDeserializer<JsonObject> {
35+
@Override
36+
public JsonObject deserialize(JsonElement jsonElement, Type type,
37+
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
38+
if (Objects.equals(type, JsonNull.class)) {
39+
return new JsonObject();
40+
}
41+
if (jsonElement.isJsonNull()) {
42+
return new JsonObject();
43+
}
44+
return jsonElement.getAsJsonObject();
45+
}
46+
}

rest/src/main/java/com/github/devicehive/rest/api/UserApi.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@
2121

2222
package com.github.devicehive.rest.api;
2323

24+
import com.github.devicehive.rest.model.UserInsert;
2425
import com.github.devicehive.rest.model.UserNetworkResponse;
2526
import com.github.devicehive.rest.model.UserUpdate;
27+
import com.github.devicehive.rest.model.UserVO;
2628
import com.github.devicehive.rest.model.UserWithNetwork;
27-
import retrofit2.Call;
28-
import retrofit2.http.*;
2929

3030
import java.util.List;
3131

32+
import retrofit2.Call;
33+
import retrofit2.http.Body;
34+
import retrofit2.http.DELETE;
35+
import retrofit2.http.GET;
36+
import retrofit2.http.Headers;
37+
import retrofit2.http.POST;
38+
import retrofit2.http.PUT;
39+
import retrofit2.http.Path;
40+
import retrofit2.http.Query;
41+
3242

3343
public interface UserApi {
3444
/**
@@ -110,8 +120,8 @@ Call<com.github.devicehive.rest.model.UserResponse> getUser(
110120
"Content-Type:application/json"
111121
})
112122
@POST("user")
113-
Call<com.github.devicehive.rest.model.UserVO> insertUser(
114-
@Body com.github.devicehive.rest.model.UserUpdate body
123+
Call<UserInsert> insertUser(
124+
@Body UserUpdate body
115125
);
116126

117127
/**
@@ -131,7 +141,7 @@ Call<com.github.devicehive.rest.model.UserVO> insertUser(
131141
"Content-Type:application/json"
132142
})
133143
@GET("user")
134-
Call<List<com.github.devicehive.rest.model.UserVO>> list(
144+
Call<List<UserVO>> list(
135145
@Query("login") String login, @Query("loginPattern") String loginPattern, @Query("role") Integer role,
136146
@Query("status") Integer status, @Query("sortField") String sortField, @Query("sortOrder") String sortOrder,
137147
@Query("take") Integer take, @Query("skip") Integer skip
@@ -163,7 +173,7 @@ Call<Void> unassignNetwork(
163173
})
164174
@PUT("user/current")
165175
Call<Void> updateCurrentUser(
166-
@Body com.github.devicehive.rest.model.UserUpdate body
176+
@Body UserUpdate body
167177
);
168178

169179
/**

rest/src/main/java/com/github/devicehive/rest/model/Device.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
package com.github.devicehive.rest.model;
3636

37+
import com.github.devicehive.rest.adapters.NullJsonAdapter;
3738
import com.google.gson.JsonObject;
39+
import com.google.gson.annotations.JsonAdapter;
3840
import com.google.gson.annotations.SerializedName;
3941

4042
/**
@@ -47,7 +49,7 @@ public class Device {
4749

4850
@SerializedName("name")
4951
private String name = null;
50-
52+
@JsonAdapter(value = NullJsonAdapter.class)
5153
@SerializedName("data")
5254
private JsonObject data = new JsonObject();
5355

rest/src/main/java/com/github/devicehive/rest/model/DeviceCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
package com.github.devicehive.rest.model;
3636

37+
import com.github.devicehive.rest.adapters.NullJsonAdapter;
3738
import com.google.gson.JsonObject;
39+
import com.google.gson.annotations.JsonAdapter;
3840
import com.google.gson.annotations.SerializedName;
3941

4042
import org.joda.time.DateTime;
@@ -61,7 +63,7 @@ public class DeviceCommand implements Comparable<DeviceCommand> {
6163

6264
@SerializedName("networkId")
6365
private Long networkId = null;
64-
66+
@JsonAdapter(value = NullJsonAdapter.class)
6567
@SerializedName("parameters")
6668
private JsonObject parameters = new JsonObject();
6769

@@ -71,6 +73,7 @@ public class DeviceCommand implements Comparable<DeviceCommand> {
7173
@SerializedName("status")
7274
private String status = null;
7375

76+
@JsonAdapter(value = NullJsonAdapter.class)
7477
@SerializedName("result")
7578
private JsonObject result = new JsonObject();
7679

rest/src/main/java/com/github/devicehive/rest/model/DeviceCommandWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
package com.github.devicehive.rest.model;
3636

37+
import com.github.devicehive.rest.adapters.NullJsonAdapter;
3738
import com.google.gson.JsonObject;
39+
import com.google.gson.annotations.JsonAdapter;
3840
import com.google.gson.annotations.SerializedName;
3941

4042
import org.joda.time.DateTime;
@@ -50,6 +52,7 @@ public class DeviceCommandWrapper {
5052
@SerializedName("timestamp")
5153
private DateTime timestamp = null;
5254

55+
@JsonAdapter(value = NullJsonAdapter.class)
5356
@SerializedName("parameters")
5457
private JsonObject parameters = new JsonObject();
5558

@@ -59,6 +62,7 @@ public class DeviceCommandWrapper {
5962
@SerializedName("status")
6063
private String status = null;
6164

65+
@JsonAdapter(value = NullJsonAdapter.class)
6266
@SerializedName("result")
6367
private JsonObject result = new JsonObject();
6468

rest/src/main/java/com/github/devicehive/rest/model/DeviceNotification.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
package com.github.devicehive.rest.model;
3636

37+
import com.github.devicehive.rest.adapters.NullJsonAdapter;
3738
import com.google.gson.JsonObject;
39+
import com.google.gson.annotations.JsonAdapter;
3840
import com.google.gson.annotations.SerializedName;
3941

4042
import org.joda.time.DateTime;
@@ -59,6 +61,7 @@ public class DeviceNotification implements Comparable<DeviceNotification> {
5961
@SerializedName("timestamp")
6062
private DateTime timestamp = null;
6163

64+
@JsonAdapter(value = NullJsonAdapter.class)
6265
@SerializedName("parameters")
6366
private JsonObject parameters = new JsonObject();
6467

rest/src/main/java/com/github/devicehive/rest/model/DeviceNotificationWrapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434

3535
package com.github.devicehive.rest.model;
3636

37+
import com.github.devicehive.rest.adapters.NullJsonAdapter;
3738
import com.google.gson.JsonObject;
39+
import com.google.gson.annotations.JsonAdapter;
3840
import com.google.gson.annotations.SerializedName;
3941

4042
import org.joda.time.DateTime;
@@ -50,7 +52,8 @@ public class DeviceNotificationWrapper {
5052
@SerializedName("timestamp")
5153
private DateTime timestamp = null;
5254

53-
@SerializedName("parameters")
55+
@JsonAdapter(value = NullJsonAdapter.class)
56+
@SerializedName("data")
5457
private JsonObject parameters = new JsonObject();
5558

5659
public String getNotification() {

0 commit comments

Comments
 (0)