forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnwrapSingleField.java
More file actions
22 lines (19 loc) · 969 Bytes
/
Copy pathUnwrapSingleField.java
File metadata and controls
22 lines (19 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.pubnub.api.utils;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
public class UnwrapSingleField<T> implements JsonDeserializer<T> {
@Override
public T deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.keySet().size() != 1) {
throw new IllegalStateException("Couldn't unwrap field for object containing more than 1 field. Actual number of fields: " + jsonObject.keySet().size());
}
final String key = jsonObject.keySet().toArray(new String[]{})[0];
final JsonElement element = jsonObject.get(key);
return context.deserialize(element, typeOfT);
}
}