diff --git a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java index a5ca33334..1ecf3fcf9 100644 --- a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java +++ b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java @@ -148,7 +148,7 @@ public abstract class AbstractServices { atomDeserializer = new FITAtomDeserializer(); jsonDeserializer = new JsonDeserializer(true); atomSerializer = new AtomSerializer(true); - jsonSerializer = new JsonSerializer(true); + jsonSerializer = new JsonSerializer(true, ContentType.JSON_FULL_METADATA); xml = new XMLUtilities(metadata); json = new JSONUtilities(metadata); diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java b/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java index 7fa920150..172e37a09 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java @@ -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.ResWrap; 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.ODataDeserializerException; import org.apache.olingo.commons.api.serialization.ODataSerializer; @@ -104,7 +105,7 @@ public abstract class AbstractUtilities { atomDeserializer = new FITAtomDeserializer(); jsonDeserializer = new JsonDeserializer(true); atomSerializer = new AtomSerializer(true); - jsonSerializer = new JsonSerializer(true); + jsonSerializer = new JsonSerializer(true, ContentType.JSON_FULL_METADATA); } public boolean isMediaContent(final String entityName) { diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java b/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java index 02dcadd23..0d0e491ba 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java @@ -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.ResWrap; 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.core.serialization.AtomSerializer; import org.apache.olingo.commons.core.serialization.JsonSerializer; @@ -125,7 +126,7 @@ public class FSManager { putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM)); content.reset(); - new JsonSerializer(true).write(writer, container); + new JsonSerializer(true, ContentType.JSON_FULL_METADATA).write(writer, container); writer.flush(); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA)); diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java index fc0a32ec0..ae258941e 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java @@ -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.ODataEntityCreateRequest; 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.retrieve.EdmMetadataRequest; 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.ODataEntityCreateResponse; 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.domain.ClientAnnotation; 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); 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, - of.newPrimitiveValueBuilder().buildString("" + 34L))); + of.newPrimitiveValueBuilder().buildDecimal(BigDecimal.valueOf(34)))); newEntity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE, linkURI)); final ODataEntityCreateRequest request = client.getCUDRequestFactory() @@ -962,7 +964,7 @@ public class BasicITCase extends AbstractBaseTestITCase { } @Test - public void createEntityInt64AndDecimalAsStringWithoutIEEE754ComaptibleParameter() { + public void createEntityWithIEEE754CompatibleParameterNull() { final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI); client.getConfiguration().setDefaultPubFormat(ContentType.JSON); final ClientObjectFactory of = client.getObjectFactory(); @@ -972,21 +974,163 @@ public class BasicITCase extends AbstractBaseTestITCase { final ClientEntity newEntity = of.newEntity(ET_ALL_PRIM); newEntity.getProperties().add(of.newPrimitiveProperty(PROPERTY_INT64, - of.newPrimitiveValueBuilder().buildString("" + Long.MAX_VALUE))); + of.newPrimitiveValueBuilder().buildInt64(null))); 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)); final ODataEntityCreateRequest request = client.getCUDRequestFactory() .getEntityCreateRequest(uri, newEntity); + request.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + request.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataEntityCreateResponse 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 requestUpdate = client.getCUDRequestFactory() + .getEntityUpdateRequest(uri, UpdateType.PATCH, entity); + requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataEntityUpdateResponse responseUpdate = requestUpdate.execute(); + + String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next(); + + final ODataEntityRequest requestGet = client.getRetrieveRequestFactory().getEntityRequest(uri); + requestGet.addCustomHeader(HttpHeader.COOKIE, cookie); + requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataRetrieveResponse 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 requestUpdate = client.getCUDRequestFactory() + .getEntityUpdateRequest(uri, UpdateType.PATCH, entity); + requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataEntityUpdateResponse responseUpdate = requestUpdate.execute(); + + String cookie = responseUpdate.getHeader(HttpHeader.SET_COOKIE).iterator().next(); + + final ODataEntityRequest requestGet = client.getRetrieveRequestFactory().getEntityRequest(uri); + requestGet.addCustomHeader(HttpHeader.COOKIE, cookie); + requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataRetrieveResponse 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 requestUpdate = client.getCUDRequestFactory() + .getEntityUpdateRequest(uri, UpdateType.PATCH, entity); + requestUpdate.setContentType(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + requestUpdate.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + try { - request.execute(); + requestUpdate.execute(); fail(); } catch(ODataClientErrorException e) { 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 requestGet = client.getRetrieveRequestFactory().getPropertyRequest(uri); + requestGet.addCustomHeader(HttpHeader.COOKIE, cookie); + requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataRetrieveResponse 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 requestGet = client.getRetrieveRequestFactory().getPropertyRequest(uri); + requestGet.addCustomHeader(HttpHeader.COOKIE, cookie); + requestGet.setAccept(CONTENT_TYPE_JSON_IEEE754_COMPATIBLE); + final ODataRetrieveResponse responseGet = requestGet.execute(); + + assertEquals(BigDecimal.valueOf(Long.MAX_VALUE), responseGet.getBody().getPrimitiveValue().toValue()); + } + @Test public void readESAllPrimCollectionWithIEEE754CompatibleParameter() { final ODataClient client = ODataClientFactory.getEdmEnabledClient(SERVICE_URI); diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientPrimitiveValue.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientPrimitiveValue.java index 350efdbed..445af8435 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientPrimitiveValue.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientPrimitiveValue.java @@ -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.EdmType; +import java.math.BigDecimal; import java.util.UUID; public interface ClientPrimitiveValue extends ClientValue { @@ -54,6 +55,8 @@ public interface ClientPrimitiveValue extends ClientValue { ClientPrimitiveValue buildGuid(UUID value); ClientPrimitiveValue buildBinary(byte[] value); + + ClientPrimitiveValue buildDecimal(BigDecimal value); } EdmPrimitiveTypeKind getTypeKind(); diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java index f8f85d50f..e6f7fa614 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/domain/ClientPrimitiveValueImpl.java @@ -18,6 +18,7 @@ */ package org.apache.olingo.client.core.domain; +import java.math.BigDecimal; import java.util.UUID; 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(); } + @Override + public ClientPrimitiveValue buildDecimal(BigDecimal value) { + return setType(EdmPrimitiveTypeKind.Decimal).setValue(value).build(); + } + } /** diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java index 9117ade01..6cfd78db3 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java @@ -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.edm.EdmPrimitiveTypeException; 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 com.fasterxml.jackson.core.JsonGenerator; @@ -39,10 +40,6 @@ import com.fasterxml.jackson.core.JsonGenerator; */ public class JsonEntitySerializer extends JsonSerializer { - public JsonEntitySerializer(final boolean serverMode) { - super(serverMode); - } - public JsonEntitySerializer(final boolean serverMode, final ContentType 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, 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()); } @@ -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); } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java index b7a21da00..c6c0c6389 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySetSerializer.java @@ -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.ResWrap; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.format.ContentType; import com.fasterxml.jackson.core.JsonGenerator; public class JsonEntitySetSerializer extends JsonSerializer { - public JsonEntitySetSerializer(final boolean serverMode) { - super(serverMode); + public JsonEntitySetSerializer(final boolean serverMode, final ContentType contentType) { + super(serverMode, contentType); } protected void doSerialize(final EntityCollection entitySet, final JsonGenerator jgen) @@ -81,7 +82,7 @@ public class JsonEntitySetSerializer extends JsonSerializer { } jgen.writeArrayFieldStart(Constants.VALUE); - final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); + final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType); for (Entity entity : entitySet.getEntities()) { entitySerializer.doSerialize(entity, jgen); } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java index cd2227fef..cd7bdc86a 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonPropertySerializer.java @@ -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.ResWrap; 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 com.fasterxml.jackson.core.JsonGenerator; @@ -36,8 +37,8 @@ import com.fasterxml.jackson.core.JsonGenerator; */ public class JsonPropertySerializer extends JsonSerializer { - public JsonPropertySerializer(final boolean serverMode) { - super(serverMode); + public JsonPropertySerializer(final boolean serverMode, final ContentType contentType) { + super(serverMode, contentType); } protected void doSerialize(final Property property, final JsonGenerator jgen) diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java index 1cc7e1a5d..7fb1b06b4 100755 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java @@ -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.geo.Geospatial; 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.ODataSerializerException; import org.apache.olingo.commons.core.edm.EdmTypeInfo; @@ -64,16 +65,13 @@ public class JsonSerializer implements ODataSerializer { private final JsonGeoValueSerializer geoSerializer = new JsonGeoValueSerializer(); protected boolean serverMode; - + protected final boolean isIEEE754Compatible; protected ContentType contentType; - public JsonSerializer(final boolean serverMode) { - this(serverMode, ContentType.JSON_FULL_METADATA); - } - public JsonSerializer(final boolean serverMode, final ContentType contentType) { this.serverMode = serverMode; this.contentType = contentType; + this.isIEEE754Compatible = isIEEE754Compatible(); } @Override @@ -81,11 +79,11 @@ public class JsonSerializer implements ODataSerializer { try { final JsonGenerator json = new JsonFactory().createGenerator(writer); if (obj instanceof EntityCollection) { - new JsonEntitySetSerializer(serverMode).doSerialize((EntityCollection) obj, json); + new JsonEntitySetSerializer(serverMode, contentType).doSerialize((EntityCollection) obj, json); } else if (obj instanceof Entity) { new JsonEntitySerializer(serverMode, contentType).doSerialize((Entity) obj, json); } 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) { link((Link) obj, json); } @@ -113,11 +111,12 @@ public class JsonSerializer implements ODataSerializer { try { final JsonGenerator json = new JsonFactory().createGenerator(writer); if (obj instanceof EntityCollection) { - new JsonEntitySetSerializer(serverMode).doContainerSerialize((ResWrap) container, json); + new JsonEntitySetSerializer(serverMode, contentType).doContainerSerialize( + (ResWrap) container, json); } else if (obj instanceof Entity) { - new JsonEntitySerializer(serverMode).doContainerSerialize((ResWrap) container, json); + new JsonEntitySerializer(serverMode, contentType).doContainerSerialize((ResWrap) container, json); } else if (obj instanceof Property) { - new JsonPropertySerializer(serverMode).doContainerSerialize((ResWrap) container, json); + new JsonPropertySerializer(serverMode, contentType).doContainerSerialize((ResWrap) container, json); } else if (obj instanceof Link) { link((Link) obj, json); } else if (obj instanceof URI) { @@ -175,10 +174,10 @@ public class JsonSerializer implements ODataSerializer { if (link.getInlineEntity() != null) { jgen.writeFieldName(link.getTitle()); - new JsonEntitySerializer(serverMode).doSerialize(link.getInlineEntity(), jgen); + new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen); } else if (link.getInlineEntitySet() != null) { jgen.writeArrayFieldStart(link.getTitle()); - final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); + final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType); for (Entity subEntry : link.getInlineEntitySet().getEntities()) { entitySerializer.doSerialize(subEntry, jgen); } @@ -233,10 +232,10 @@ public class JsonSerializer implements ODataSerializer { if (link.getInlineEntity() != null) { jgen.writeFieldName(link.getTitle()); - new JsonEntitySerializer(serverMode).doSerialize(link.getInlineEntity(), jgen); + new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen); } else if (link.getInlineEntitySet() != null) { jgen.writeArrayFieldStart(link.getTitle()); - JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode); + JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType); for (Entity subEntry : link.getInlineEntitySet().getEntities()) { entitySerializer.doSerialize(subEntry, jgen); } @@ -294,12 +293,15 @@ public class JsonSerializer implements ODataSerializer { } else if (isBoolean) { jgen.writeBoolean((Boolean) value); } else { - final String serialized = kind == null + String serialized = kind == null ? value.toString() // TODO: add facets : EdmPrimitiveTypeFactory.getInstance(kind). 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); } else { jgen.writeString(serialized); @@ -312,7 +314,7 @@ public class JsonSerializer implements ODataSerializer { throws IOException, EdmPrimitiveTypeException { 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()); } @@ -359,7 +361,7 @@ public class JsonSerializer implements ODataSerializer { valuable.isPrimitive()) || valuable.isNull()) { 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( name + StringUtils.prependIfMissing(Constants.JSON_TYPE, "@")); jgen.writeString(new EdmTypeInfo.Builder().setTypeExpression(type).build().external()); @@ -373,4 +375,9 @@ public class JsonSerializer implements ODataSerializer { jgen.writeFieldName(name); 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()); + } } \ No newline at end of file