Renaming the Expanded Field

Renaming the Expanded Field

The JSON representation of the result is used only for better readability. The actual result will be a deserialized object according to the properties of the Activity and User classes. In order to have a proper deserialization, the same properties of a matching type should be defined in the client model classes.

When expanding fields, the default behavior is to use the content type field name for the expanded field in the result.

You can rename the return field by passing a second parameter to the ExpandDefinition constructor. You can use it regardless of whether the relation field is held in the queried content type or the related content type. It is designed to give the option to return a completely different object when expanding a field. For example in an Android (Java) environment the field return type will be changed from a "UUID" to an object and will break the deserialization process of any existing application. And since that is what expand do - return an object instead of an "Id", there is an option to rename the property of the result. Let's take a standard activity (from Telerik Platform social app sample):

{
    Activity: {
        "Id": "activityId",
        "Likes": [
            "user1Id",
            "user2Id"
        ]
    }
}

where "Likes" is a relation field. In Java this field will be deserialized to ArrayList<UUID>(). Adding a simple expand expression like:

ExpandDefinition expandDefinition = new ExpandDefinition("Likes");
everliveApp.workWith().data(Activity.class).get().expand(expandDefinition).executeSync();

will break any existing application, since Telerik Platform will return:

{
    Activity: {
        "Id": "activityId",
        "Likes": [{
            User1Object...
        }, {
            User2Object...
        }]
    }
}

which cannot be deserialized to a Guid list.

Instead, you can use the ReturnAs option that can be used to return the result like "UsersWhoLikeThisActivity" and add a new property of type ArrayList<User> into which the result will be deserialized.

To preserve the data integrity, these renamed fields should not be returned back to the server, since this will add the new field in the database with the nested object inside (which usually is not the desired behavior). In order to restrict a specific field from writing to the server the expanded field should be deleted from the update (create) object. In order to restrict a specific property from writing to the server, take a look at Android object model help article.

ExpandDefinition expandDefinition = new ExpandDefinition("Likes", "UsersWhoLikeThisActivity");
everliveApp.workWith().data(Activity.class).get().expand(expandDefinition).executeSync();

which will result in:

{
    Count: 2,
    Result: [{
        "UsersWhoLikeThisActivity": [{
            "Username": "seth",
            "DisplayName": "Seth Peterson",
        ....
        }],
        "Text": "It is finally time for graduation! Good job everyone, we made it."
            ....
    }, {
        "UsersWhoLikeThisActivity": [],
        "Text": "The most amazing sunset I have ever seen at Phuket, Thailand",
        ...
    }]
}
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.