PL-9071 REST API serialization error if entity is a POJO field

This commit is contained in:
Maxim Gorbunkov 2017-05-11 14:57:19 +04:00
parent 556a7c63d1
commit c0043485b7
4 changed files with 18 additions and 9 deletions

View File

@ -108,6 +108,12 @@ public class EntitySerialization implements EntitySerializationAPI {
return createGsonForSerialization(view, options).toJson(entities);
}
@Override
public String objectToJson(Object object, EntitySerializationOption... options) {
context.remove();
return createGsonForSerialization(null, options).toJson(object);
}
@Override
public Entity entityFromJson(String json,
@Nullable MetaClass metaClass,

View File

@ -53,7 +53,7 @@ public interface EntitySerializationAPI {
EntitySerializationOption... options);
/**
* Serialized a collection of entities to the JSON array. Method works like the {@link #toJson(Entity, View,
* Serializes a collection of entities to the JSON array. Method works like the {@link #toJson(Entity, View,
* EntitySerializationOption...)}, but return a JSON array as a result.
*
* @param entities a list of entities to be serialized
@ -83,6 +83,15 @@ public interface EntitySerializationAPI {
*/
String toJson(Collection<? extends Entity> entities);
/**
* Serializes any custom POJO or collection of POJOs or JSON. If some field in POJO is an entity then this field
* will be serialized according to entity serialization rules
*
* @param object any POJO or collection of POJOs
* @param options options specifying how a JSON object graph for fields with type 'Entity' will be serialized
* @return a string that represents a JSON object or JSON array
*/
String objectToJson(Object object, EntitySerializationOption... options);
/**
* Deserializes a JSON object to the entity.

View File

@ -123,14 +123,8 @@ public class RestParseUtils {
return gson.fromJson(json, clazz);
}
public String serializePOJO(Object pojoInstance, Class clazz) {
Gson gson = new Gson();
return gson.toJson(pojoInstance, clazz);
}
public String serialize(Object instance) {
Gson gson = new Gson();
return gson.toJson(instance);
return entitySerializationAPI.objectToJson(instance);
}
public Map<String, String> parseParamsJson(String paramsJson) {

View File

@ -180,7 +180,7 @@ public class ServicesControllerManager {
if (datatype != null) {
return new ServiceCallResult(datatype.format(methodResult), false);
} else {
return new ServiceCallResult(restParseUtils.serializePOJO(methodResult, methodReturnType), true);
return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
}
}
}