JSON returned from business logic data query

I have a custom business logic method:

public List<Membership> getMemberships() {

    StringBuilder whereClause = new StringBuilder();
    whereClause.append("user.objectId='").append(InvocationContext.getUserId()).append("'");

    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause(whereClause.toString());
    List<Membership> result = Backendless.Persistence.of(Membership.class).find(dataQuery).getCurrentPage();

    return result;

which looks fine and returns data, 2 memberships with the current database content. The database content looks fine, but the JSON has an error in it. The memberships have a relation to a Business, and each Business has a relationship to all the Memberships. So in this example 1 user is querying to find out what business they are in and who else is in that business.

The first business in the JSON is fine, it contains an object with "___objectref": 1 in the list of memberships and everything works nicely. The second business however contains an array of strings where there should be an array of objects like the first business has. The single string in the array appears to be a datestamp "1458304017000" but that’s just a guess. It certainly seems to have nothing to do with the membership that should actually be in the array.

The returned JSON is:


[
  {
    "___jsonclass": "Membership",
    "business": {
      "___jsonclass": "Business",
      "created": "1458304017000",
      "ownerId": null,
      "members": [
        {
          "___objectref": 1
        }
      ],
      "___class": "Business",
      "updated": null,
      "objectId": "58E47B88-5E12-1C5D-FF14-E9887EC5AB00",
      "___dates___": [
        "1458304017000"
      ]
    },
    "created": "1458304017000",
    "ownerId": null,
    "___class": "Membership",
    "updated": null,
    "objectId": "6191CFF7-DF43-C697-FF6E-C131E1851000",
    "___dates___": [
      "1458303983000",
      "1458304017000"
    ]
  },
  {
    "___jsonclass": "Membership",
    "business": {
      "___jsonclass": "Business",
      "created": "1458073011000",
      "ownerId": null,
      "members": [
        [
          "1458304017000"
        ]
      ],
      "___class": "Business",
      "updated": null,
      "objectId": "0F82D274-F114-2169-FFC9-89EAAF443900",
      "___dates___": [
        "1458073011000"
      ]
    },
    "created": "1458073011000",
    "ownerId": null,
    "___class": "Membership",
    "updated": null,
    "user": null,
    "objectId": "48BEB1F3-3B3C-5A90-FFD8-78F2951D9500",
    "___dates___": [
      "1458072980000",
      "1458073011000"
    ]
  }
]

That was collected from running in debug with CodeRunner. I’ve just noticed a new version has been released a few days ago so I’ll download that and give it a try too.

Updated to Version: 3.0.12 Build date: 20160321-1136 and the same issue still exists

Hi Wain,

To help us reproduce the problem, would you be able to put together a Java program that recreates the structure contained in the “result” object? Once we have a Java object which is being serialized, we can very efficiently diagnose the serialization problem.

Regards,
Mark

As a note for anyone looking at this in the future, the issue was related to bi-directional relationships in the data model. In theory the issue can be resolved in the SDK, but I just removed the bi-directional aspect of the relationships and changed my queries. In fact I’ve removed a lot of the relationships and moved to a simple string id reference in many cases as the query is almost identical.

As an aside I’ve kept some of the relationships in the model classes I return from my custom business logic. This is a bit weird as it pollutes the data model on the server because it’s aware of these classes, but it does allow me to be very flexible with the data returned from the server and the depth and circularities in the object graph…

Hello Wein,

Your problem is resolved. We have updated our production server, please download new Code Runner using code generation tool in Console > Business Logic > Service Events Handlers > Generated Code > Download and try deploy your service from there.
Now you will be able to return objects with cyclic relations.

Regards, Artur.