Skip to content

Commit

Permalink
fixed serialization of typed values. Addressed issue #45
Browse files Browse the repository at this point in the history
  • Loading branch information
benni committed Dec 20, 2019
1 parent b228bf2 commit 3d77934
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,18 @@ private List<Object> getCompoundValue(List<Object> value, String[] compound) thr
}

private List<Object> getValue(BaseProperty myProperty) {
List<Object> result = new ArrayList<Object>();
if (myProperty instanceof Property) {
return ((Property) myProperty).getValue();
for(Object item: ((Property) myProperty).getValue()) {
if(item instanceof TypedValue) {
result.add(((TypedValue) item).getValue());
}else {
result.add(item);
}
}
return result;
} else if (myProperty instanceof Relationship) {
List<Object> result = new ArrayList<Object>();

result.add(((Relationship) myProperty).getObject().toString());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eu.neclab.ngsildbroker.commons.datatypes;

public class TypedValue {
private String type;
private Object value;




public TypedValue(String type, Object value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import eu.neclab.ngsildbroker.commons.datatypes.Subscription;
import eu.neclab.ngsildbroker.commons.datatypes.SubscriptionRequest;
import eu.neclab.ngsildbroker.commons.datatypes.TemporalEntityStorageKey;
import eu.neclab.ngsildbroker.commons.datatypes.TypedValue;

public class DataSerializer {

Expand Down Expand Up @@ -86,6 +87,7 @@ private static void registerTypes(GsonBuilder builder) {
builder.registerTypeAdapter(BatchResult.class, new BatchResultGsonAdapter());
builder.registerTypeAdapterFactory(new GeometryAdapterFactory());
builder.registerTypeAdapter(Notification.class, new NotificationGsonAdapter());
builder.registerTypeAdapter(TypedValue.class, new TypedValueGsonAdapter());
builder.registerTypeAdapter(SerializationTypes.entitiesType, new EntitiesGsonAdapter());
// builder.registerTypeAdapter(propertiesType, new PropertiesGsonAdapter());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.neclab.ngsildbroker.commons.serialization;

import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import eu.neclab.ngsildbroker.commons.constants.NGSIConstants;
import eu.neclab.ngsildbroker.commons.datatypes.TypedValue;

public class TypedValueGsonAdapter implements JsonSerializer<TypedValue> {


@Override
public JsonElement serialize(TypedValue src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject top = new JsonObject();
top.add(NGSIConstants.JSON_LD_TYPE, new JsonPrimitive(src.getType()));
top.add(NGSIConstants.JSON_LD_VALUE, context.serialize(src.getValue()));
return top;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import eu.neclab.ngsildbroker.commons.datatypes.GeoProperty;
import eu.neclab.ngsildbroker.commons.datatypes.Property;
import eu.neclab.ngsildbroker.commons.datatypes.Relationship;
import eu.neclab.ngsildbroker.commons.datatypes.TypedValue;
import eu.neclab.ngsildbroker.commons.enums.ErrorType;
import eu.neclab.ngsildbroker.commons.exceptions.ResponseException;
import eu.neclab.ngsildbroker.commons.serialization.DataSerializer;
Expand Down Expand Up @@ -235,6 +236,21 @@ public void accept(JsonElement t) {
return result;
} else if (element.isJsonObject()) {
JsonObject jsonObj = element.getAsJsonObject();
if(jsonObj.has(NGSIConstants.JSON_LD_VALUE) && jsonObj.has(NGSIConstants.JSON_LD_TYPE)) {
Object objValue;
JsonPrimitive atValue = jsonObj.get(NGSIConstants.JSON_LD_VALUE).getAsJsonPrimitive();
if (atValue.isBoolean()) {
objValue = atValue.getAsBoolean();
}else if (atValue.isNumber()) {
objValue = atValue.getAsDouble();
}else if (atValue.isString()) {
objValue = atValue.getAsString();
}else {
objValue = jsonObj.get(NGSIConstants.JSON_LD_VALUE).getAsString();
}

return new TypedValue(jsonObj.get(NGSIConstants.JSON_LD_TYPE).getAsString(), objValue);
}
if (jsonObj.has(NGSIConstants.JSON_LD_VALUE)) {
JsonPrimitive atValue = jsonObj.get(NGSIConstants.JSON_LD_VALUE).getAsJsonPrimitive();
if (atValue.isBoolean()) {
Expand Down Expand Up @@ -461,6 +477,9 @@ private static JsonElement getJson(List<Object> values, JsonSerializationContext
// should never happen
// }
else {
if(value instanceof TypedValue) {
return context.serialize(value);
}
obj = new JsonObject();
obj.add(NGSIConstants.JSON_LD_VALUE, context.serialize(value));

Expand Down

0 comments on commit 3d77934

Please sign in to comment.