[OLINGO-704] ODataClient IEEE754Compatible seralization support

This commit is contained in:
Christian Holzer 2015-06-17 17:26:50 +02:00
parent 588df780bf
commit 628e2e7182
10 changed files with 200 additions and 39 deletions

View File

@ -148,7 +148,7 @@ public abstract class AbstractServices {
atomDeserializer = new FITAtomDeserializer(); atomDeserializer = new FITAtomDeserializer();
jsonDeserializer = new JsonDeserializer(true); jsonDeserializer = new JsonDeserializer(true);
atomSerializer = new AtomSerializer(true); atomSerializer = new AtomSerializer(true);
jsonSerializer = new JsonSerializer(true); jsonSerializer = new JsonSerializer(true, ContentType.JSON_FULL_METADATA);
xml = new XMLUtilities(metadata); xml = new XMLUtilities(metadata);
json = new JSONUtilities(metadata); json = new JSONUtilities(metadata);

View File

@ -48,6 +48,7 @@ import org.apache.olingo.commons.api.data.Link;
import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.serialization.ODataDeserializer; import org.apache.olingo.commons.api.serialization.ODataDeserializer;
import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.apache.olingo.commons.api.serialization.ODataDeserializerException;
import org.apache.olingo.commons.api.serialization.ODataSerializer; import org.apache.olingo.commons.api.serialization.ODataSerializer;
@ -104,7 +105,7 @@ public abstract class AbstractUtilities {
atomDeserializer = new FITAtomDeserializer(); atomDeserializer = new FITAtomDeserializer();
jsonDeserializer = new JsonDeserializer(true); jsonDeserializer = new JsonDeserializer(true);
atomSerializer = new AtomSerializer(true); atomSerializer = new AtomSerializer(true);
jsonSerializer = new JsonSerializer(true); jsonSerializer = new JsonSerializer(true, ContentType.JSON_FULL_METADATA);
} }
public boolean isMediaContent(final String entityName) { public boolean isMediaContent(final String entityName) {

View File

@ -41,6 +41,7 @@ import org.apache.commons.vfs2.VFS;
import org.apache.olingo.commons.api.data.Entity; import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.serialization.ODataSerializerException; import org.apache.olingo.commons.api.serialization.ODataSerializerException;
import org.apache.olingo.commons.core.serialization.AtomSerializer; import org.apache.olingo.commons.core.serialization.AtomSerializer;
import org.apache.olingo.commons.core.serialization.JsonSerializer; import org.apache.olingo.commons.core.serialization.JsonSerializer;
@ -125,7 +126,7 @@ public class FSManager {
putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM)); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM));
content.reset(); content.reset();
new JsonSerializer(true).write(writer, container); new JsonSerializer(true, ContentType.JSON_FULL_METADATA).write(writer, container);
writer.flush(); writer.flush();
putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA)); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA));

View File

@ -41,6 +41,7 @@ import org.apache.olingo.client.api.communication.ODataClientErrorException;
import org.apache.olingo.client.api.communication.request.cud.ODataDeleteRequest; import org.apache.olingo.client.api.communication.request.cud.ODataDeleteRequest;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest; import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest; import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
import org.apache.olingo.client.api.communication.request.cud.ODataPropertyUpdateRequest;
import org.apache.olingo.client.api.communication.request.cud.UpdateType; import org.apache.olingo.client.api.communication.request.cud.UpdateType;
import org.apache.olingo.client.api.communication.request.retrieve.EdmMetadataRequest; import org.apache.olingo.client.api.communication.request.retrieve.EdmMetadataRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest; import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
@ -52,6 +53,7 @@ import org.apache.olingo.client.api.communication.request.retrieve.XMLMetadataRe
import org.apache.olingo.client.api.communication.response.ODataDeleteResponse; import org.apache.olingo.client.api.communication.response.ODataDeleteResponse;
import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse; import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse;
import org.apache.olingo.client.api.communication.response.ODataEntityUpdateResponse; import org.apache.olingo.client.api.communication.response.ODataEntityUpdateResponse;
import org.apache.olingo.client.api.communication.response.ODataPropertyUpdateResponse;
import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse; import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse;
import org.apache.olingo.client.api.domain.ClientAnnotation; import org.apache.olingo.client.api.domain.ClientAnnotation;
import org.apache.olingo.client.api.domain.ClientComplexValue; import org.apache.olingo.client.api.domain.ClientComplexValue;
@ -945,9 +947,9 @@ public class BasicITCase extends AbstractBaseTestITCase {
final ClientEntity newEntity = of.newEntity(ET_ALL_PRIM); final ClientEntity newEntity = of.newEntity(ET_ALL_PRIM);
newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64, newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildString("" + Long.MAX_VALUE))); of.newPrimitiveValueBuilder().buildInt64(Long.MAX_VALUE)));
newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL, newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildString("" + 34L))); of.newPrimitiveValueBuilder().buildDecimal(BigDecimal.valueOf(34))));
newEntity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE, linkURI)); newEntity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE, linkURI));
final ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory() final ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory()
@ -962,7 +964,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
} }
@Test @Test
public void createEntityInt64AndDecimalAsStringWithoutIEEE754ComaptibleParameter() { public void createEntityWithIEEE754CompatibleParameterNull() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI); final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON); client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory(); final ClientObjectFactory of = client.getObjectFactory();
@ -972,21 +974,163 @@ public class BasicITCase extends AbstractBaseTestITCase {
final ClientEntity newEntity = of.newEntity(ET_ALL_PRIM); final ClientEntity newEntity = of.newEntity(ET_ALL_PRIM);
newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64, newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildString("" + Long.MAX_VALUE))); of.newPrimitiveValueBuilder().buildInt64(null)));
newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL, newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildString("" + 34L))); of.newPrimitiveValueBuilder().buildDecimal(null)));
newEntity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE, linkURI)); newEntity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE, linkURI));
final ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory() final ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory()
.getEntityCreateRequest(uri, newEntity); .getEntityCreateRequest(uri, newEntity);
request.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
request.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataEntityCreateResponse<ClientEntity> response = request.execute();
assertTrue(response.getBody().getProperty(PROPERTY_INT64).hasNullValue());
assertTrue(response.getBody().getProperty(PROPERTY_DECIMAL).hasNullValue());
}
@Test
public void updateEntityWithIEEE754CompatibleParameter() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory();
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(0).build();
final ClientEntity entity = of.newEntity(ET_ALL_PRIM);
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildInt64(Long.MAX_VALUE)));
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildDecimal(BigDecimal.valueOf(Long.MAX_VALUE))));
final ODataEntityUpdateRequest<ClientEntity> requestUpdate = client.getCUDRequestFactory()
.getEntityUpdateRequest(uri, UpdateType.PATCH, entity);
requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataEntityUpdateResponse<ClientEntity> responseUpdate = requestUpdate.execute();
String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next();
final ODataEntityRequest<ClientEntity> requestGet = client.getRetrieveRequestFactory().getEntityRequest(uri);
requestGet.addCustomHeader(HttpHeader.COOKIE, cookie);
requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataRetrieveResponse<ClientEntity> responseGet = requestGet.execute();
assertEquals(Long.MAX_VALUE, responseGet.getBody().getProperty(PROPERTY_INT64).getPrimitiveValue().toValue());
assertEquals(BigDecimal.valueOf(Long.MAX_VALUE), responseGet.getBody().getProperty(PROPERTY_DECIMAL)
.getPrimitiveValue()
.toValue());
}
@Test
public void updateEntityWithIEEE754CompatibleParameterNull() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory();
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(0).build();
final ClientEntity entity = of.newEntity(ET_ALL_PRIM);
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildInt64(null)));
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildDecimal(null)));
final ODataEntityUpdateRequest<ClientEntity> requestUpdate = client.getCUDRequestFactory()
.getEntityUpdateRequest(uri, UpdateType.PATCH, entity);
requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataEntityUpdateResponse<ClientEntity> responseUpdate = requestUpdate.execute();
String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next();
final ODataEntityRequest<ClientEntity> requestGet = client.getRetrieveRequestFactory().getEntityRequest(uri);
requestGet.addCustomHeader(HttpHeader.COOKIE, cookie);
requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataRetrieveResponse<ClientEntity> responseGet = requestGet.execute();
assertTrue(responseGet.getBody().getProperty(PROPERTY_INT64).hasNullValue());
assertTrue(responseGet.getBody().getProperty(PROPERTY_DECIMAL).hasNullValue());
}
@Test
public void updateEntityWithIEEE754CompatibleParameterWithNullString() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory();
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(0).build();
final ClientEntity entity = of.newEntity(ET_ALL_PRIM);
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildString("null")));
entity.getProperties().add(of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildString("null")));
final ODataEntityUpdateRequest<ClientEntity> requestUpdate = client.getCUDRequestFactory()
.getEntityUpdateRequest(uri, UpdateType.PATCH, entity);
requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
try { try {
request.execute(); requestUpdate.execute();
fail(); fail();
} catch(ODataClientErrorException e) { } catch(ODataClientErrorException e) {
assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), e.getStatusLine().getStatusCode()); assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), e.getStatusLine().getStatusCode());
} }
} }
@Test
public void updateEdmInt64PropertyWithIEE754CompatibleParameter() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory();
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM)
.appendKeySegment(0)
.appendPropertySegment(PROPERTY_INT64).build();
final ODataPropertyUpdateRequest requestUpdate = client.getCUDRequestFactory()
.getPropertyPrimitiveValueUpdateRequest(uri,
of.newPrimitiveProperty(PROPERTY_INT64,
of.newPrimitiveValueBuilder().buildInt64(Long.MAX_VALUE)));
requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataPropertyUpdateResponse responseUpdate = requestUpdate.execute();
String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next();
final ODataPropertyRequest<ClientProperty> requestGet = client.getRetrieveRequestFactory().getPropertyRequest(uri);
requestGet.addCustomHeader(HttpHeader.COOKIE, cookie);
requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataRetrieveResponse<ClientProperty> responseGet = requestGet.execute();
assertEquals(Long.MAX_VALUE, responseGet.getBody().getPrimitiveValue().toValue());
}
@Test
public void updateProperyEdmDecimaltWithIEE754CompatibleParameter() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);
client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
final ClientObjectFactory of = client.getObjectFactory();
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM)
.appendKeySegment(0)
.appendPropertySegment(PROPERTY_DECIMAL).build();
final ODataPropertyUpdateRequest requestUpdate = client.getCUDRequestFactory()
.getPropertyPrimitiveValueUpdateRequest(uri,
of.newPrimitiveProperty(PROPERTY_DECIMAL,
of.newPrimitiveValueBuilder().buildInt64(Long.MAX_VALUE)));
requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataPropertyUpdateResponse responseUpdate = requestUpdate.execute();
String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next();
final ODataPropertyRequest<ClientProperty> requestGet = client.getRetrieveRequestFactory().getPropertyRequest(uri);
requestGet.addCustomHeader(HttpHeader.COOKIE, cookie);
requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE);
final ODataRetrieveResponse<ClientProperty> responseGet = requestGet.execute();
assertEquals(BigDecimal.valueOf(Long.MAX_VALUE), responseGet.getBody().getPrimitiveValue().toValue());
}
@Test @Test
public void readESAllPrimCollectionWithIEEE754CompatibleParameter() { public void readESAllPrimCollectionWithIEEE754CompatibleParameter() {
final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI); final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI);

View File

@ -23,6 +23,7 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.EdmType;
import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
public interface ClientPrimitiveValue extends ClientValue { public interface ClientPrimitiveValue extends ClientValue {
@ -54,6 +55,8 @@ public interface ClientPrimitiveValue extends ClientValue {
ClientPrimitiveValue buildGuid(UUID value); ClientPrimitiveValue buildGuid(UUID value);
ClientPrimitiveValue buildBinary(byte[] value); ClientPrimitiveValue buildBinary(byte[] value);
ClientPrimitiveValue buildDecimal(BigDecimal value);
} }
EdmPrimitiveTypeKind getTypeKind(); EdmPrimitiveTypeKind getTypeKind();

View File

@ -18,6 +18,7 @@
*/ */
package org.apache.olingo.client.core.domain; package org.apache.olingo.client.core.domain;
import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.Constants;
@ -133,6 +134,11 @@ public class ClientPrimitiveValueImpl extends AbstractClientValue implements Cli
return setType(EdmPrimitiveTypeKind.Binary).setValue(value).build(); return setType(EdmPrimitiveTypeKind.Binary).setValue(value).build();
} }
@Override
public ClientPrimitiveValue buildDecimal(BigDecimal value) {
return setType(EdmPrimitiveTypeKind.Decimal).setValue(value).build();
}
} }
/** /**

View File

@ -30,6 +30,7 @@ import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.format.Format;
import org.apache.olingo.commons.core.edm.EdmTypeInfo; import org.apache.olingo.commons.core.edm.EdmTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
@ -39,10 +40,6 @@ import com.fasterxml.jackson.core.JsonGenerator;
*/ */
public class JsonEntitySerializer extends JsonSerializer { public class JsonEntitySerializer extends JsonSerializer {
public JsonEntitySerializer(final boolean serverMode) {
super(serverMode);
}
public JsonEntitySerializer(final boolean serverMode, final ContentType contentType) { public JsonEntitySerializer(final boolean serverMode, final ContentType contentType) {
super(serverMode, contentType); super(serverMode, contentType);
} }
@ -73,12 +70,12 @@ public class JsonEntitySerializer extends JsonSerializer {
} }
} }
if (StringUtils.isNotBlank(entity.getType()) && contentType != ContentType.JSON_NO_METADATA) { if (StringUtils.isNotBlank(entity.getType()) && contentType.getODataFormat() != Format.JSON_NO_METADATA) {
jgen.writeStringField(Constants.JSON_TYPE, jgen.writeStringField(Constants.JSON_TYPE,
new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external()); new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external());
} }
if (entity.getId() != null && contentType != ContentType.JSON_NO_METADATA) { if (entity.getId() != null && contentType.getODataFormat() != Format.JSON_NO_METADATA) {
jgen.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString()); jgen.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString());
} }
@ -100,7 +97,7 @@ public class JsonEntitySerializer extends JsonSerializer {
} }
} }
if (contentType != ContentType.JSON_NO_METADATA) { if (contentType.getODataFormat() != Format.JSON_NO_METADATA) {
links(entity, jgen); links(entity, jgen);
} }

View File

@ -27,13 +27,14 @@ import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection; import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.format.ContentType;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
public class JsonEntitySetSerializer extends JsonSerializer { public class JsonEntitySetSerializer extends JsonSerializer {
public JsonEntitySetSerializer(final boolean serverMode) { public JsonEntitySetSerializer(final boolean serverMode, final ContentType contentType) {
super(serverMode); super(serverMode, contentType);
} }
protected void doSerialize(final EntityCollection entitySet, final JsonGenerator jgen) protected void doSerialize(final EntityCollection entitySet, final JsonGenerator jgen)
@ -81,7 +82,7 @@ public class JsonEntitySetSerializer extends JsonSerializer {
} }
jgen.writeArrayFieldStart(Constants.VALUE); jgen.writeArrayFieldStart(Constants.VALUE);
final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType);
for (Entity entity : entitySet.getEntities()) { for (Entity entity : entitySet.getEntities()) {
entitySerializer.doSerialize(entity, jgen); entitySerializer.doSerialize(entity, jgen);
} }

View File

@ -27,6 +27,7 @@ import org.apache.olingo.commons.api.data.Annotation;
import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.data.ResWrap;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.core.edm.EdmTypeInfo; import org.apache.olingo.commons.core.edm.EdmTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
@ -36,8 +37,8 @@ import com.fasterxml.jackson.core.JsonGenerator;
*/ */
public class JsonPropertySerializer extends JsonSerializer { public class JsonPropertySerializer extends JsonSerializer {
public JsonPropertySerializer(final boolean serverMode) { public JsonPropertySerializer(final boolean serverMode, final ContentType contentType) {
super(serverMode); super(serverMode, contentType);
} }
protected void doSerialize(final Property property, final JsonGenerator jgen) protected void doSerialize(final Property property, final JsonGenerator jgen)

View File

@ -44,6 +44,7 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.geo.Geospatial; import org.apache.olingo.commons.api.edm.geo.Geospatial;
import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.format.Format;
import org.apache.olingo.commons.api.serialization.ODataSerializer; import org.apache.olingo.commons.api.serialization.ODataSerializer;
import org.apache.olingo.commons.api.serialization.ODataSerializerException; import org.apache.olingo.commons.api.serialization.ODataSerializerException;
import org.apache.olingo.commons.core.edm.EdmTypeInfo; import org.apache.olingo.commons.core.edm.EdmTypeInfo;
@ -64,16 +65,13 @@ public class JsonSerializer implements ODataSerializer {
private final JsonGeoValueSerializer geoSerializer = new JsonGeoValueSerializer(); private final JsonGeoValueSerializer geoSerializer = new JsonGeoValueSerializer();
protected boolean serverMode; protected boolean serverMode;
protected final boolean isIEEE754Compatible;
protected ContentType contentType; protected ContentType contentType;
public JsonSerializer(final boolean serverMode) {
this(serverMode, ContentType.JSON_FULL_METADATA);
}
public JsonSerializer(final boolean serverMode, final ContentType contentType) { public JsonSerializer(final boolean serverMode, final ContentType contentType) {
this.serverMode = serverMode; this.serverMode = serverMode;
this.contentType = contentType; this.contentType = contentType;
this.isIEEE754Compatible = isIEEE754Compatible();
} }
@Override @Override
@ -81,11 +79,11 @@ public class JsonSerializer implements ODataSerializer {
try { try {
final JsonGenerator json = new JsonFactory().createGenerator(writer); final JsonGenerator json = new JsonFactory().createGenerator(writer);
if (obj instanceof EntityCollection) { if (obj instanceof EntityCollection) {
new JsonEntitySetSerializer(serverMode).doSerialize((EntityCollection) obj, json); new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json);
} else if (obj instanceof Entity) { } else if (obj instanceof Entity) {
new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json);
} else if (obj instanceof Property) { } else if (obj instanceof Property) {
new JsonPropertySerializer(serverMode).doSerialize((Property) obj, json); new JsonPropertySerializer(serverMode, contentType).doSerialize((Property) obj, json);
} else if (obj instanceof Link) { } else if (obj instanceof Link) {
link((Link) obj, json); link((Link) obj, json);
} }
@ -113,11 +111,12 @@ public class JsonSerializer implements ODataSerializer {
try { try {
final JsonGenerator json = new JsonFactory().createGenerator(writer); final JsonGenerator json = new JsonFactory().createGenerator(writer);
if (obj instanceof EntityCollection) { if (obj instanceof EntityCollection) {
new JsonEntitySetSerializer(serverMode).doContainerSerialize((ResWrap<EntityCollection>) container, json); new JsonEntitySetSerializer(serverMode, contentType).doContainerSerialize(
(ResWrap<EntityCollection>) container, json);
} else if (obj instanceof Entity) { } else if (obj instanceof Entity) {
new JsonEntitySerializer(serverMode).doContainerSerialize((ResWrap<Entity>) container, json); new JsonEntitySerializer(serverMode, contentType).doContainerSerialize((ResWrap<Entity>) container, json);
} else if (obj instanceof Property) { } else if (obj instanceof Property) {
new JsonPropertySerializer(serverMode).doContainerSerialize((ResWrap<Property>) container, json); new JsonPropertySerializer(serverMode, contentType).doContainerSerialize((ResWrap<Property>) container, json);
} else if (obj instanceof Link) { } else if (obj instanceof Link) {
link((Link) obj, json); link((Link) obj, json);
} else if (obj instanceof URI) { } else if (obj instanceof URI) {
@ -175,10 +174,10 @@ public class JsonSerializer implements ODataSerializer {
if (link.getInlineEntity() != null) { if (link.getInlineEntity() != null) {
jgen.writeFieldName(link.getTitle()); jgen.writeFieldName(link.getTitle());
new JsonEntitySerializer(serverMode).doSerialize(link.getInlineEntity(), jgen); new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen);
} else if (link.getInlineEntitySet() != null) { } else if (link.getInlineEntitySet() != null) {
jgen.writeArrayFieldStart(link.getTitle()); jgen.writeArrayFieldStart(link.getTitle());
final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType);
for (Entity subEntry : link.getInlineEntitySet().getEntities()) { for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
entitySerializer.doSerialize(subEntry, jgen); entitySerializer.doSerialize(subEntry, jgen);
} }
@ -233,10 +232,10 @@ public class JsonSerializer implements ODataSerializer {
if (link.getInlineEntity() != null) { if (link.getInlineEntity() != null) {
jgen.writeFieldName(link.getTitle()); jgen.writeFieldName(link.getTitle());
new JsonEntitySerializer(serverMode).doSerialize(link.getInlineEntity(), jgen); new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen);
} else if (link.getInlineEntitySet() != null) { } else if (link.getInlineEntitySet() != null) {
jgen.writeArrayFieldStart(link.getTitle()); jgen.writeArrayFieldStart(link.getTitle());
JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType);
for (Entity subEntry : link.getInlineEntitySet().getEntities()) { for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
entitySerializer.doSerialize(subEntry, jgen); entitySerializer.doSerialize(subEntry, jgen);
} }
@ -294,12 +293,15 @@ public class JsonSerializer implements ODataSerializer {
} else if (isBoolean) { } else if (isBoolean) {
jgen.writeBoolean((Boolean) value); jgen.writeBoolean((Boolean) value);
} else { } else {
final String serialized = kind == null String serialized = kind == null
? value.toString() ? value.toString()
// TODO: add facets // TODO: add facets
: EdmPrimitiveTypeFactory.getInstance(kind). : EdmPrimitiveTypeFactory.getInstance(kind).
valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null); valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null);
if (isNumber) {
if(isIEEE754Compatible && (kind == EdmPrimitiveTypeKind.Int64 || kind == EdmPrimitiveTypeKind.Decimal)) {
jgen.writeString(serialized);
} else if(isNumber) {
jgen.writeNumber(serialized); jgen.writeNumber(serialized);
} else { } else {
jgen.writeString(serialized); jgen.writeString(serialized);
@ -312,7 +314,7 @@ public class JsonSerializer implements ODataSerializer {
throws IOException, EdmPrimitiveTypeException { throws IOException, EdmPrimitiveTypeException {
jgen.writeStartObject(); jgen.writeStartObject();
if (typeInfo != null && contentType != ContentType.JSON_NO_METADATA) { if (typeInfo != null && contentType.getODataFormat() != Format.JSON_NO_METADATA) {
jgen.writeStringField(Constants.JSON_TYPE, typeInfo.external()); jgen.writeStringField(Constants.JSON_TYPE, typeInfo.external());
} }
@ -359,7 +361,7 @@ public class JsonSerializer implements ODataSerializer {
valuable.isPrimitive()) || valuable.isNull()) { valuable.isPrimitive()) || valuable.isNull()) {
type = EdmPrimitiveTypeKind.String.getFullQualifiedName().toString(); type = EdmPrimitiveTypeKind.String.getFullQualifiedName().toString();
} }
if (StringUtils.isNotBlank(type) && contentType != ContentType.JSON_NO_METADATA) { if (StringUtils.isNotBlank(type) && contentType.getODataFormat() != Format.JSON_NO_METADATA) {
jgen.writeFieldName( jgen.writeFieldName(
name + StringUtils.prependIfMissing(Constants.JSON_TYPE, "@")); name + StringUtils.prependIfMissing(Constants.JSON_TYPE, "@"));
jgen.writeString(new EdmTypeInfo.Builder().setTypeExpression(type).build().external()); jgen.writeString(new EdmTypeInfo.Builder().setTypeExpression(type).build().external());
@ -373,4 +375,9 @@ public class JsonSerializer implements ODataSerializer {
jgen.writeFieldName(name); jgen.writeFieldName(name);
value(jgen, valuable.getType(), valuable); value(jgen, valuable.getType(), valuable);
} }
private boolean isIEEE754Compatible() {
final String parameter = contentType.getParameters().get(ContentType.PARAMETER_IEEE754_COMPATIBLE);
return parameter == null ? false : "true".equals(parameter.toLowerCase());
}
} }