Skip to content

Commit 798ea6a

Browse files
author
anquetil
committed
modified printing of properties to avoid extra "," for entity with no property
1 parent 81c88f2 commit 798ea6a

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public void beginElement(String name) {
3939
lntabs();
4040
append("\"FM3\":\"");
4141
append(name);
42-
append("\",");
43-
lntabs();
42+
append("\"");
4443
}
4544

4645
public void beginMultivalue(String name) {
@@ -121,9 +120,10 @@ public void reference(String name, int index) {
121120

122121
@Override
123122
public void serial(int index) {
123+
printPropertySeparator();
124+
lntabs();
124125
append("\"id\":");
125126
append(String.valueOf(index));
126-
append(",");
127127
}
128128

129129
public void printEntitySeparator() {

lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void beginElement(String name) {
3030
append("{");
3131
append("\"FM3\":\"");
3232
append(name);
33-
append("\",");
33+
append("\"");
3434
}
3535

3636
public void beginMultivalue(String name) {
@@ -104,9 +104,9 @@ public void reference(String name, int index) {
104104

105105
@Override
106106
public void serial(int index) {
107+
printPropertySeparator();
107108
append("\"id\":");
108109
append(String.valueOf(index));
109-
append(",");
110110
}
111111

112112
public void printEntitySeparator() {

lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col
7979
continue;
8080
}*/
8181
if (!values.isEmpty()) {
82+
visitor.printEntitySeparator();
83+
8284
visitor.beginAttribute(property.getName());
8385
if (property.isMultivalued()) {
8486
visitor.beginMultivalue(property.getName());
@@ -131,9 +133,6 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col
131133
visitor.endMultivalue(property.getName());
132134
}
133135
visitor.endAttribute(property.getName());
134-
if (propertiesIterator.hasNext()) {
135-
visitor.printEntitySeparator();
136-
}
137136
}
138137
}
139138
}

lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ch.akuhn.fame.test;
22

3+
import ch.akuhn.fame.MetaRepository;
34
import ch.akuhn.fame.Tower;
45
import ch.akuhn.fame.internal.JSONPrettyPrinter;
6+
import ch.akuhn.fame.internal.JSONPrinter;
57
import ch.akuhn.fame.parser.InputSource;
68
import junit.framework.TestCase;
79

@@ -10,6 +12,9 @@ public class JSONPrinterTest extends TestCase {
1012
private JSONPrettyPrinter printer;
1113
private Appendable stream;
1214

15+
/** FIXME
16+
* This is not a test but a method to export a meta-model
17+
*/
1318
public void testExportJSON() {
1419
InputSource input = InputSource.fromResource("ch/unibe/fame/resources/lib.mse");
1520
Tower t = new Tower();
@@ -28,46 +33,83 @@ public void setUp() throws Exception {
2833

2934
public void testBeginAttributeSimple() {
3035
printer.beginAttribute("hello");
31-
assertEquals(stream.toString(), "\"hello\":");
36+
assertEquals("\"hello\":", stream.toString());
3237
}
3338

3439
public void testPrimitive() {
3540
printer.primitive("value");
36-
assertEquals(stream.toString(), "\"value\"");
41+
assertEquals("\"value\"", stream.toString());
3742
}
3843

3944
public void testPrimitiveWithSpecialCharacter() {
4045
printer.primitive("MySuper\"String");
41-
assertEquals(stream.toString(), "\"MySuper\\\"String\"");
46+
assertEquals("\"MySuper\\\"String\"", stream.toString());
4247
}
4348

4449
public void testPrimitiveWithSpecialCharacterAndActualExample() {
4550
printer.primitive("print(\"Printer \" + name() + \" prints \"+ thePacket.contents(),false)");
46-
assertEquals(stream.toString(), "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\"");
51+
assertEquals( "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\"", stream.toString());
4752
}
4853

4954
public void testReference() {
5055
printer.reference("hello");
51-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":\"hello\"}");
56+
assertEquals("{\"ref\":\"hello\"}", removeWhiteSpaces(stream.toString()));
5257
}
5358

5459
public void testReferenceIndex() {
5560
printer.reference(2);
56-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":2}");
61+
assertEquals("{\"ref\":2}", removeWhiteSpaces(stream.toString()));
5762
}
5863

5964
public void testSerial() {
6065
printer.serial(2);
61-
assertEquals(removeWhiteSpaces(stream.toString()), "\"id\":2,");
66+
assertEquals(",\"id\":2", removeWhiteSpaces(stream.toString()));
6267
}
6368

6469
public void testBeginElement() {
6570
printer.beginElement("Java.Class");
66-
assertEquals(removeWhiteSpaces(stream.toString()), "{\"FM3\":\"Java.Class\",");
71+
assertEquals("{\"FM3\":\"Java.Class\"", removeWhiteSpaces(stream.toString()));
6772
}
6873

6974

7075
private static String removeWhiteSpaces(String input) {
7176
return input.replaceAll("\\s+", "");
7277
}
78+
79+
public void testEmptyEntityPrettyPrinter() {
80+
String str = "((FM3.Package))";
81+
Tower t = new Tower();
82+
t.getMetamodel().importMSE(str);
83+
MetaRepository repo = t.getMetamodel();
84+
repo.accept( printer);
85+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", removeWhiteSpaces(stream.toString()));
86+
}
87+
88+
public void testEmptyEntityJSONPrinter() {
89+
String str = "((FM3.Package))";
90+
Tower t = new Tower();
91+
t.getMetamodel().importMSE(str);
92+
MetaRepository repo = t.getMetamodel();
93+
repo.accept( new JSONPrinter(stream));
94+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", stream.toString());
95+
}
96+
97+
public void testEntityWithAttributePrettyPrinter() {
98+
String str = "((FM3.Package (name 'Blah')))";
99+
Tower t = new Tower();
100+
t.getMetamodel().importMSE(str);
101+
MetaRepository repo = t.getMetamodel();
102+
repo.accept( printer);
103+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", removeWhiteSpaces(stream.toString()));
104+
}
105+
106+
public void testEntityWithAttributeJSONPrinter() {
107+
String str = "((FM3.Package (name 'Blah')))";
108+
Tower t = new Tower();
109+
t.getMetamodel().importMSE(str);
110+
MetaRepository repo = t.getMetamodel();
111+
repo.accept( new JSONPrinter(stream));
112+
assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", stream.toString());
113+
}
114+
73115
}

0 commit comments

Comments
 (0)