Skip to content

Commit

Permalink
Added nullable handler for JsonObject;
Browse files Browse the repository at this point in the history
  • Loading branch information
edubovik committed Jan 6, 2018
1 parent b0ea464 commit 45c18d6
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ To subscribe on notifications you just need to create `NotificationFilter` where
</details>

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

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

Properties:
* `id` (read only)
* `login`
* `lastLogin`
* `role`
* `password` (write only)
* `data`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.rest.model.RoleEnum;
import com.github.devicehive.rest.model.UserInsert;
import com.github.devicehive.rest.model.UserUpdate;
import com.github.devicehive.rest.model.UserVO;
import com.github.devicehive.rest.model.UserWithNetwork;
Expand All @@ -37,7 +38,7 @@ public class User {
private String login;
private RoleEnum role;
private String password;
private JsonObject data=new JsonObject();
private JsonObject data = new JsonObject();

private User() {
}
Expand All @@ -55,6 +56,16 @@ public static User create(UserWithNetwork user) {
return result;
}

public static User create(UserInsert user) {
if (user == null) {
return null;
}
User result = new User();
result.setId(user.getId());
result.setData(user.getData());
return result;
}

public static User create(UserVO user) {
if (user == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public DHResponse<User> createUser(String login, String password, RoleEnum role,
UserApi userApi = createService(UserApi.class);
UserUpdate userUpdate = createUserBody(login, password, role, statusEnum, data);
DHResponse<User> response;
DHResponse<UserVO> result = execute(userApi.insertUser(userUpdate));
DHResponse<UserInsert> result = execute(userApi.insertUser(userUpdate));
response = DHResponse.create(User.create(result.getData()), result.getFailureData());
if (response.isSuccessful()) {
updateUser(response, login, role, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
*
* NullJsonAdapter.java
*
* Copyright (C) 2018 DataArt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.github.devicehive.rest.adapters;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.Objects;

public class NullJsonAdapter implements JsonDeserializer<JsonObject> {
@Override
public JsonObject deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (Objects.equals(type, JsonNull.class)) {
return new JsonObject();
}
if (jsonElement.isJsonNull()) {
return new JsonObject();
}
return jsonElement.getAsJsonObject();
}
}
22 changes: 16 additions & 6 deletions rest/src/main/java/com/github/devicehive/rest/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@

package com.github.devicehive.rest.api;

import com.github.devicehive.rest.model.UserInsert;
import com.github.devicehive.rest.model.UserNetworkResponse;
import com.github.devicehive.rest.model.UserUpdate;
import com.github.devicehive.rest.model.UserVO;
import com.github.devicehive.rest.model.UserWithNetwork;
import retrofit2.Call;
import retrofit2.http.*;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;


public interface UserApi {
/**
Expand Down Expand Up @@ -110,8 +120,8 @@ Call<com.github.devicehive.rest.model.UserResponse> getUser(
"Content-Type:application/json"
})
@POST("user")
Call<com.github.devicehive.rest.model.UserVO> insertUser(
@Body com.github.devicehive.rest.model.UserUpdate body
Call<UserInsert> insertUser(
@Body UserUpdate body
);

/**
Expand All @@ -131,7 +141,7 @@ Call<com.github.devicehive.rest.model.UserVO> insertUser(
"Content-Type:application/json"
})
@GET("user")
Call<List<com.github.devicehive.rest.model.UserVO>> list(
Call<List<UserVO>> list(
@Query("login") String login, @Query("loginPattern") String loginPattern, @Query("role") Integer role,
@Query("status") Integer status, @Query("sortField") String sortField, @Query("sortOrder") String sortOrder,
@Query("take") Integer take, @Query("skip") Integer skip
Expand Down Expand Up @@ -163,7 +173,7 @@ Call<Void> unassignNetwork(
})
@PUT("user/current")
Call<Void> updateCurrentUser(
@Body com.github.devicehive.rest.model.UserUpdate body
@Body UserUpdate body
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

/**
Expand All @@ -47,7 +49,7 @@ public class Device {

@SerializedName("name")
private String name = null;

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("data")
private JsonObject data = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

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

@SerializedName("networkId")
private Long networkId = null;

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("parameters")
private JsonObject parameters = new JsonObject();

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

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("result")
private JsonObject result = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

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

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("parameters")
private JsonObject parameters = new JsonObject();

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

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("result")
private JsonObject result = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

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

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("parameters")
private JsonObject parameters = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

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

@SerializedName("parameters")
@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("data")
private JsonObject parameters = new JsonObject();

public String getNotification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

/**
Expand All @@ -46,6 +48,7 @@ public class DeviceUpdate {
@SerializedName("name")
private String name = null;

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("data")
private JsonObject data = new JsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

package com.github.devicehive.rest.model;

import com.github.devicehive.rest.adapters.NullJsonAdapter;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,7 +70,7 @@ public class User {

@SerializedName("entityVersion")
private Long entityVersion = null;

@JsonAdapter(value = NullJsonAdapter.class)
@SerializedName("data")
private JsonObject data = new JsonObject();

Expand Down
Loading

0 comments on commit 45c18d6

Please sign in to comment.