Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Parsing a nested JSON containment references list
Parsing a nested JSON containment references list [message #1842423] Fri, 18 June 2021 08:56 Go to next message
Jan Hicken is currently offline Jan HickenFriend
Messages: 4
Registered: June 2021
Junior Member
Hello,

I am trying to define an model using Xcore for the following simplified JSON data:

(The category object contains many more attributes I have omitted here.)

{
  "foo": "bar",
  "categories": [
    [
      {
        "categoryName": "root"
      },
      {
        "categoryName": "a"
      }
    ],
    [
      {
        "categoryName": "root"
      },
      {
        "categoryName": "b"
      }
    ]
  ]
}


I have come up with the following model so far:

class Root {
  String foo
  contains CategoryPath[] categories
}

interface CategoryPath wraps org.eclipse.emf.common.util.EList<Category>

class Category {
  String categoryName
}


This model generates the following Java Getter in my Root class, which is fine:

EList<EList<Category>> getCategories();


Now I am using the emfjson library, which moved to EMF.cloud recently, to parse the above JSON instance into EObjects. Sadly, this parsing fails because of the nested list.

Is this a limitation of the emfjson library or should I maybe model this differently?

Kind regards
Jan

[Updated on: Thu, 24 June 2021 09:14]

Report message to a moderator

Re: Parsing a nested JSON containment references list [message #1842442 is a reply to message #1842423] Sat, 19 June 2021 11:47 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 111
Registered: September 2016
Senior Member
Hi Jan,

could you post this in the channel of EMF.cloud please? https://www.eclipse.org/emfcloud/support/

Also could you add the error message you get? The model seems pretty standard, should actually work with the converter. Ideally you post a small reproducer, too.

best regards,
Jonas


--
Jonas Helming
Get professional Eclipse developer support:
http://eclipsesource.com/en/services/developer-support/
Re: Parsing a nested JSON containment references list [message #1842579 is a reply to message #1842442] Thu, 24 June 2021 09:07 Go to previous messageGo to next message
Jan Hicken is currently offline Jan HickenFriend
Messages: 4
Registered: June 2021
Junior Member
Hi Jonas,

even without the JSON part I still get an error.

This is my model:

class Root {
  String foo
  contains CategoryPath[] categories
}

class Category {
  String categoryName
}

interface CategoryPath wraps org.eclipse.emf.common.util.EList<Category> {}


I generated the Java classes and tried to do the following:

var root = SampleFactory.eINSTANCE.createRoot();
var category = SampleFactory.eINSTANCE.createCategory();
var categoryPath = new BasicEList<Category>();
categoryPath.add(category);
root.getCategories().add(categoryPath);


When executing this, the following error is thrown:

class org.eclipse.emf.common.util.BasicEList cannot be cast to class org.eclipse.emf.ecore.InternalEObject (org.eclipse.emf.common.util.BasicEList and org.eclipse.emf.ecore.InternalEObject are in unnamed module of loader 'app')
java.lang.ClassCastException: class org.eclipse.emf.common.util.BasicEList cannot be cast to class org.eclipse.emf.ecore.InternalEObject (org.eclipse.emf.common.util.BasicEList and org.eclipse.emf.ecore.InternalEObject are in unnamed module of loader 'app')
	at org.eclipse.emf.ecore.util.EcoreEList.inverseAdd(EcoreEList.java:261)
	at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:312)
	at org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:304)

[Updated on: Thu, 24 June 2021 09:14]

Report message to a moderator

Re: Parsing a nested JSON containment references list [message #1842584 is a reply to message #1842579] Thu, 24 June 2021 11:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Yes, the model looks nonsensical. The interface CategoryPath wrapping a list and then used in a containment reference would only work if you have some EObject that also implements EList<Category>, but I don't think it's sensible to create such a thing.



Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Parsing a nested JSON containment references list [message #1842668 is a reply to message #1842584] Mon, 28 June 2021 08:34 Go to previous messageGo to next message
Jan Hicken is currently offline Jan HickenFriend
Messages: 4
Registered: June 2021
Junior Member
I agree, it feels like I'm doing something, that is not supposed to be done here.

Does this mean the above JSON payload cannot be mapped to an Ecore model?
Re: Parsing a nested JSON containment references list [message #1842669 is a reply to message #1842668] Mon, 28 June 2021 09:17 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If you really want to instantiate a CategoryPath in your model then it needs to be a Class not an Interface.

Probably much simpler to leave it out so that "contains Category[] categories" maintains one aggregate for free rather than an aggregate of aggregates

Regards

Ed Willink
Re: Parsing a nested JSON containment references list [message #1842671 is a reply to message #1842669] Mon, 28 June 2021 11:19 Go to previous messageGo to next message
Jan Hicken is currently offline Jan HickenFriend
Messages: 4
Registered: June 2021
Junior Member
Thanks, I understand.

Flattening the list sadly discards some information, which is not obvious from my example: In this particular case above, the object belongs to the category path a/b. It could also belong to a/d/e and f/g/h for example. Having a flattened list of a, b, a, d, e, f, g, h would be problematic because I cannot restore the categories' hierarchy any more.

I will probably create a CategoryPath class holding a list of categories. This means, I need to preprocess the above JSON first, before being able to parse it then.
Re: Parsing a nested JSON containment references list [message #1842672 is a reply to message #1842671] Mon, 28 June 2021 11:48 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Perhaps your model should be a little more like the com.google.gson.JsonElement hierarchy where there is a JsonArray class that represents a "list". I.e., if you want a list of lists, the outer list needs to be some type of EClass such that the instance is some type of EObject.

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Client context org.eclipse.egf.pattern.ui.patternContext failed with a run-time exception.
Next Topic:[EMF] parse enum values surrounded by whitespace
Goto Forum:
  


Current Time: Fri Apr 26 22:52:56 GMT 2024

Powered by FUDForum. Page generated in 0.03837 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top