Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problem serialize empty JsonObject and Array
Problem serialize empty JsonObject and Array [message #1100556] Tue, 03 September 2013 08:55 Go to next message
Mr Manner is currently offline Mr MannerFriend
Messages: 26
Registered: January 2013
Junior Member
Hi I have the following simple json grammar:

RootObject returns JsonObject:
	(JsonObject | EmptyObject)?; 

JsonObject:
	{JsonObject} '{' (members+=Member) (',' members+=Member)* '}';

EmptyObject returns JsonObject:
	{JsonObject} '{' '}';

Member:
	key=STRING ':' name=Value;
	
Value:
	JsonObject | EmptyObject | StringValue | Array | EmptyArray | Boolean | Null | NumberValue;

NumberValue:
	name=NUMBER;

StringValue:
	name=STRING;
	
Array:
	{Array} '[' ( => values+=Value) (',' values+=Value)* ']';
 
 EmptyArray returns Array:
 	{Array} '[' ']';
 
Boolean:
	{Boolean} (name='true' | 'false');

Null:
	{Null} name='null';

terminal NUMBER:
	'-'? INT ('.' INT)? (('E' | 'e') '-'? INT)?;
	
terminal INT returns ecore::EInt:
	('0'..'9')+;

terminal STRING returns ecore::EString:
	'"' ( '\\' ('"'|'/'|'\\'|'b'|'f'|'n'|'r'|'t') | !('\\'|'"') )* '"';

terminal WS:
	(' '|'\t'|'\r'|'\n')+;


If I try to serialise this simple json sniped I get an SyntaxException

{
  "version": "1.0",
  "foo": {}
}


Trace:

org.eclipse.xtext.validation.IConcreteSyntaxValidator$InvalidConcreteSyntaxException: These errors need to be fixed before the model can be serialized.
JsonObject.members[1]->Member'com.test.voconhint.json.impl.JsonObjectImpl@73ee70bd'.name->JsonObject: Feature JsonObject.members holds 0 non-transient value(s), but at least 1 are required. Constraint: (members members*) Quantities: members:0
JsonObject.members[1]->Member'com.test.voconhint.json.impl.JsonObjectImpl@73ee70bd'.name->JsonObject: Feature JsonObject.members holds 0 non-transient value(s), but at least 1 are required. Constraint: (members members*) Quantities: members:0

	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:96)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:122)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:51)
	at com.test.ValidateJob.validate(ValidateJob.java:83)


The code that serializes the json is just this simple line:

serializer.serialize(json)


Any pointers on whats happening wrong here?

Kind Regards
Manner
Re: Problem serialize empty JsonObject and Array [message #1100563 is a reply to message #1100556] Tue, 03 September 2013 09:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

the error says that JsonObject.members has at least one element (according to the grammar) but
the one you try to serialize has none.

Maybe the problem is that your grammar is a bit tricky on that part and changing it to

EmptyObject returns JsonObject:
	{EmptyObject} '{' '}';


may Help

or you /dev/null the emptyObject at all

JsonObject:
{JsonObject} '{' ((members+=Member) (',' members+=Member)*)? '}';
;


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem serialize empty JsonObject and Array [message #1107488 is a reply to message #1100563] Thu, 12 September 2013 13:30 Go to previous messageGo to next message
Mr Manner is currently offline Mr MannerFriend
Messages: 26
Registered: January 2013
Junior Member
Thank for your reply. Your first suggestion fixed the problem that I was having. I don't really understand why it works though. The {Rule} tells the grammar the always create a Rule object, but if EmptyObject returns a JsonObject it is still the same object type no?

I also tried the second solution but that one gave me the following error when I tried to serialise json:
java.lang.ClassCastException: org.eclipse.xtext.impl.GroupImpl cannot be cast to org.eclipse.xtext.Assignment
	at com.test.json.services.JsonGrammarAccess$JsonObjectElements.<init>(JsonGrammarAccess.java:46)
	at com.test.json.services.JsonGrammarAccess.getJsonObjectAccess(JsonGrammarAccess.java:421)
	at com.test.json.services.JsonGrammarAccess.getJsonObjectRule(JsonGrammarAccess.java:425)
	at com.test.json.formatting.JsonFormatter.configureFormatting(JsonFormatter.java:33)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.getConfig(AbstractDeclarativeFormatter.java:93)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.createFormatterStream(AbstractDeclarativeFormatter.java:75)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:103)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:122)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:51)
	at com.test.json.schema.Validator.validate(Validator.java:44)
	at com.test.json.schema.ValidateJob.runInWorkspace(ValidateJob.java:52)
	at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Re: Problem serialize empty JsonObject and Array [message #1107501 is a reply to message #1107488] Thu, 12 September 2013 13:53 Go to previous messageGo to next message
Mr Manner is currently offline Mr MannerFriend
Messages: 26
Registered: January 2013
Junior Member
There is an other (related) problem to this solution. If I use the following grammar:

EmptyObject returns JsonObject:
	{EmptyObject} '{' '}';


I don't have a problem serializing an empty object, bnut if I want to serialize an object that had all its members removed I get the following error:

org.eclipse.xtext.validation.IConcreteSyntaxValidator$InvalidConcreteSyntaxException: These errors need to be fixed before the model can be serialized.
JsonObject.members[1]->Member'com.test.json.json.impl.JsonObjectImpl@25cc4'.name->JsonObject.members[2]->Member'com.test.json.json.impl.JsonObjectImpl@1187e3b'.name->JsonObject.members[1]->Member'com.test.json.json.impl.JsonObjectImpl@10774e0'.name->JsonObject: Feature JsonObject.members holds 0 non-transient value(s), but at least 1 are required. Constraint: (members members*) Quantities: members:0
JsonObject.members[1]->Member'com.test.json.json.impl.JsonObjectImpl@25cc4'.name->JsonObject.members[2]->Member'com.test.json.json.impl.JsonObjectImpl@1187e3b'.name->JsonObject.members[1]->Member'com.test.json.json.impl.JsonObjectImpl@10774e0'.name->JsonObject: Feature JsonObject.members holds 0 non-transient value(s), but at least 1 are required. Constraint: (members members*) Quantities: members:0

	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:96)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:122)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:51)
	at com.test.json.schema.Validator.validate(Validator.java:44)
	at com.test.json.schema.ValidateJob.runInWorkspace(ValidateJob.java:52)
	at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)


I remove members from objects like so:

	/** Remove and return member with "key" from object. */
	public static Member removeMember(JsonObject object, String key) {
		if (object == null)
			return null;

		Member removedMember = null;
		EList<Member> members = object.getMembers();
		for (Member member : members) {
			if (member.getKey().equals(key)) {
				members.remove(member);
				removedMember = member;
				break;
			}
		}
		return removedMember;
	}


I suspect that the second solution that Christian suggested would fix this but there I have the cast exception.
Re: Problem serialize empty JsonObject and Array [message #1107511 is a reply to message #1107501] Thu, 12 September 2013 14:09 Go to previous message
Mr Manner is currently offline Mr MannerFriend
Messages: 26
Registered: January 2013
Junior Member
I had a more thorough look at why the second solution wasn't working, I made a mistake in my formatting code. The serializing is working as excepted now. Thanks for all your help.
Previous Topic:Eopposite Reference in Xtext
Next Topic: update Instance in the same resource automatically
Goto Forum:
  


Current Time: Thu Jul 09 21:49:48 GMT 2020

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

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

Back to the top