You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by Moritz Löser <mo...@prodyna.com> on 2019/07/07 08:42:51 UTC

WG: How to deserialize objects

Hi there,

I have to integrate an ODataV4 service, only reading entities, but I am struggling to get Objects out of the Json. I am new to OData and Olingo. I am able to send a request and receive the json. It is a set of entities. For example countries:
"
{
    "d": {
        "__count": "313",
        "results": [
            {
                "__metadata": {
                    "id": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "uri": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "type": "Y0MXR_REFMDS_API_1_SRV.CountryEntity"
                },
                "CountryId": "3Y",
                "LanguageId": "EN",
                "CountryShortText": "a",
                "CountryLongText": "sad",
                "OfficialFullForm": "",
                "Status": "40",
                "StatusRemark": ""
            },
            {....
"

At the moment I tried around with something like this:
"
        InputStream input = response.getRawResponse();
        ResWrap<Entity> entity = client.getDeserializer(ContentType.APPLICATION_JSON).toEntity(input);
        Entity payload = entity.getPayload();
"

I could also acquire "d":
        Property property = payload.getProperty("d");

But at the end I only need "d.results"?

So the questions are:


  *   How to get a "Collection<ClientEntity>" where ClientEntity contains the objects within "result"?
  *   Is there a way not relying on String literals (property names) to deserialize into a real object - containing the same fields? (Annotation based?)


Thanks, regards and a nice weekend

Moritz


RE: How to deserialize objects

Posted by "Grande, Oliver" <ol...@sap.com>.
 Hello Moritz,

I used a very simple approach to read the data. As ClientEntitySet provides a list of all entities getEntities() you can loop over all the retrieved entities. Each entity provides you the option to either loop over them getProperties() or to get them by name getProperty(String name). I used the later one and hard coded on the property names and directly set the value into my class:

myclass.setFirstProperty((String) convertPrimitiveProperty(entity.getProperty(FIRST_PROPERTY_NAME)));

private Object convertPrimitiveProperty(ClientProperty property) {
      return property.getPrimitiveValue() == null ? null : property.getPrimitiveValue().toValue();
}

I'm sure that one can implement something more generic.

I like to mention that Olingo also provides a Maven plugin to generate classes form metadata. Unfortunately I only found the following stackoverflow post: https://stackoverflow.com/questions/24481630/olingo-create-strongly-typed-pojos-for-client-library-of-odata-service%22 and no other documentation.


Regards,
Oliver


From: Moritz Löser <mo...@prodyna.com>
Sent: Donnerstag, 11. Juli 2019 08:58
To: Grande, Oliver <ol...@sap.com>
Cc: user@olingo.apache.org
Subject: AW: How to deserialize objects

Thanks,  but i have already all that in place. My question is how to get a meaningful object out of this.
Meantime I started using Jackson to deserialize to specific objects. My hope was that odata/oling provides means to de-/serialize into domain objects similar to Jackson.


Von: Grande, Oliver <ol...@sap.com>>
Gesendet: Montag, 8. Juli 2019 09:14
An: Moritz Löser <mo...@prodyna.com>>
Cc: user@olingo.apache.org<ma...@olingo.apache.org>
Betreff: RE: How to deserialize objects

Hello Moritz,

I have used the Olingo OData Client in the past:

    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>odata-client-core</artifactId>
      <version>${odata.version}</version>
    </dependency>

With the following call sequence:

ODataClient client = ODataClientFactory.getClient();
RetrieveRequestFactory requestFactory = client.getRetrieveRequestFactory();
ODataEntitySetRequest<ClientEntitySet> getRequest = requestFactory.getEntitySetRequest(uri); getRequest.setAccept("application/json;odata.metadata=minimal");
ODataRetrieveResponse<ClientEntitySet> response = getRequest.execute();
ClientEntitySet body = response.getBody();

Regards,
Oliver


From: Moritz Löser <mo...@prodyna.com>>
Sent: Sonntag, 7. Juli 2019 10:43
To: user@olingo.apache.org<ma...@olingo.apache.org>
Subject: WG: How to deserialize objects

Hi there,

I have to integrate an ODataV4 service, only reading entities, but I am struggling to get Objects out of the Json. I am new to OData and Olingo. I am able to send a request and receive the json. It is a set of entities. For example countries:
"
{
    "d": {
        "__count": "313",
        "results": [
            {
                "__metadata": {
                    "id": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')<%20%20>",
                    "uri": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')<%20%20>",
                    "type": "Y0MXR_REFMDS_API_1_SRV.CountryEntity"
                },
                "CountryId": "3Y",
                "LanguageId": "EN",
                "CountryShortText": "a",
                "CountryLongText": "sad",
                "OfficialFullForm": "",
                "Status": "40",
                "StatusRemark": ""
            },
            {....
"

At the moment I tried around with something like this:
"
        InputStream input = response.getRawResponse();
        ResWrap<Entity> entity = client.getDeserializer(ContentType.APPLICATION_JSON).toEntity(input);
        Entity payload = entity.getPayload();
"

I could also acquire "d":
        Property property = payload.getProperty("d");

But at the end I only need "d.results"?

So the questions are:


  *   How to get a "Collection<ClientEntity>" where ClientEntity contains the objects within "result"?
  *   Is there a way not relying on String literals (property names) to deserialize into a real object - containing the same fields? (Annotation based?)


Thanks, regards and a nice weekend

Moritz


AW: How to deserialize objects

Posted by Moritz Löser <mo...@prodyna.com>.
Thanks,  but i have already all that in place. My question is how to get a meaningful object out of this.
Meantime I started using Jackson to deserialize to specific objects. My hope was that odata/oling provides means to de-/serialize into domain objects similar to Jackson.


Von: Grande, Oliver <ol...@sap.com>
Gesendet: Montag, 8. Juli 2019 09:14
An: Moritz Löser <mo...@prodyna.com>
Cc: user@olingo.apache.org
Betreff: RE: How to deserialize objects

Hello Moritz,

I have used the Olingo OData Client in the past:

    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>odata-client-core</artifactId>
      <version>${odata.version}</version>
    </dependency>

With the following call sequence:

ODataClient client = ODataClientFactory.getClient();
RetrieveRequestFactory requestFactory = client.getRetrieveRequestFactory();
ODataEntitySetRequest<ClientEntitySet> getRequest = requestFactory.getEntitySetRequest(uri); getRequest.setAccept("application/json;odata.metadata=minimal");
ODataRetrieveResponse<ClientEntitySet> response = getRequest.execute();
ClientEntitySet body = response.getBody();

Regards,
Oliver


From: Moritz Löser <mo...@prodyna.com>>
Sent: Sonntag, 7. Juli 2019 10:43
To: user@olingo.apache.org<ma...@olingo.apache.org>
Subject: WG: How to deserialize objects

Hi there,

I have to integrate an ODataV4 service, only reading entities, but I am struggling to get Objects out of the Json. I am new to OData and Olingo. I am able to send a request and receive the json. It is a set of entities. For example countries:
"
{
    "d": {
        "__count": "313",
        "results": [
            {
                "__metadata": {
                    "id": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "uri": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "type": "Y0MXR_REFMDS_API_1_SRV.CountryEntity"
                },
                "CountryId": "3Y",
                "LanguageId": "EN",
                "CountryShortText": "a",
                "CountryLongText": "sad",
                "OfficialFullForm": "",
                "Status": "40",
                "StatusRemark": ""
            },
            {....
"

At the moment I tried around with something like this:
"
        InputStream input = response.getRawResponse();
        ResWrap<Entity> entity = client.getDeserializer(ContentType.APPLICATION_JSON).toEntity(input);
        Entity payload = entity.getPayload();
"

I could also acquire "d":
        Property property = payload.getProperty("d");

But at the end I only need "d.results"?

So the questions are:


  *   How to get a "Collection<ClientEntity>" where ClientEntity contains the objects within "result"?
  *   Is there a way not relying on String literals (property names) to deserialize into a real object - containing the same fields? (Annotation based?)


Thanks, regards and a nice weekend

Moritz


RE: How to deserialize objects

Posted by "Grande, Oliver" <ol...@sap.com>.
Hello Moritz,

I have used the Olingo OData Client in the past:

    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>odata-client-core</artifactId>
      <version>${odata.version}</version>
    </dependency>

With the following call sequence:

ODataClient client = ODataClientFactory.getClient();
RetrieveRequestFactory requestFactory = client.getRetrieveRequestFactory();
ODataEntitySetRequest<ClientEntitySet> getRequest = requestFactory.getEntitySetRequest(uri); getRequest.setAccept("application/json;odata.metadata=minimal");
ODataRetrieveResponse<ClientEntitySet> response = getRequest.execute();
ClientEntitySet body = response.getBody();

Regards,
Oliver


From: Moritz Löser <mo...@prodyna.com>
Sent: Sonntag, 7. Juli 2019 10:43
To: user@olingo.apache.org
Subject: WG: How to deserialize objects

Hi there,

I have to integrate an ODataV4 service, only reading entities, but I am struggling to get Objects out of the Json. I am new to OData and Olingo. I am able to send a request and receive the json. It is a set of entities. For example countries:
"
{
    "d": {
        "__count": "313",
        "results": [
            {
                "__metadata": {
                    "id": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "uri": "https://.../sap/opu/odata/sap/Y0MXR_REFMDS_API_1_SRV/CountryEntitySet(CountryId='3Y',LanguageId='EN')",
                    "type": "Y0MXR_REFMDS_API_1_SRV.CountryEntity"
                },
                "CountryId": "3Y",
                "LanguageId": "EN",
                "CountryShortText": "a",
                "CountryLongText": "sad",
                "OfficialFullForm": "",
                "Status": "40",
                "StatusRemark": ""
            },
            {....
"

At the moment I tried around with something like this:
"
        InputStream input = response.getRawResponse();
        ResWrap<Entity> entity = client.getDeserializer(ContentType.APPLICATION_JSON).toEntity(input);
        Entity payload = entity.getPayload();
"

I could also acquire "d":
        Property property = payload.getProperty("d");

But at the end I only need "d.results"?

So the questions are:


  *   How to get a "Collection<ClientEntity>" where ClientEntity contains the objects within "result"?
  *   Is there a way not relying on String literals (property names) to deserialize into a real object - containing the same fields? (Annotation based?)


Thanks, regards and a nice weekend

Moritz