Parsing a nested JSON containment references list [message #1842423] |
Fri, 18 June 2021 08:56 |
Jan Hicken 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 #1842579 is a reply to message #1842442] |
Thu, 24 June 2021 09:07 |
Jan Hicken 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 #1842671 is a reply to message #1842669] |
Mon, 28 June 2021 11:19 |
Jan Hicken 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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.01948 seconds