-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from moosetechnology/suport-json-printer
Add JSONPrinter
- Loading branch information
Showing
7 changed files
with
367 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
( | ||
(FM3.Package (id: 1) | ||
(name 'LIB') | ||
(classes | ||
(FM3.Class (id: 2) | ||
(name 'Person') | ||
(abstract false) | ||
(package (ref: 1)) | ||
(superclass (ref: Object)) | ||
(traits (ref: 3)) | ||
(properties | ||
(FM3.Property (id: 4) | ||
(name 'name') | ||
(class (ref: 2)) | ||
(container false) | ||
(derived false) | ||
(multivalued false) | ||
(type (ref: String))) | ||
(FM3.Property (id: 5) | ||
(name 'books') | ||
(class (ref: 2)) | ||
(container false) | ||
(derived false) | ||
(multivalued true) | ||
(opposite (ref: 6)) | ||
(type (ref: 7))))) | ||
(FM3.Class (id: 8) | ||
(name 'Library') | ||
(abstract false) | ||
(package (ref: 1)) | ||
(superclass (ref: Object)) | ||
(traits (ref: 3)) | ||
(properties | ||
(FM3.Property (id: 9) | ||
(name 'books') | ||
(class (ref: 8)) | ||
(container false) | ||
(derived false) | ||
(multivalued true) | ||
(type (ref: 7))) | ||
(FM3.Property (id: 10) | ||
(name 'librarian') | ||
(class (ref: 8)) | ||
(container false) | ||
(derived false) | ||
(multivalued false) | ||
(type (ref: 2))))) | ||
(FM3.Trait (id: 3) | ||
(name 'TNamed') | ||
(package (ref: 1))) | ||
(FM3.Class (id: 7) | ||
(name 'Book') | ||
(abstract false) | ||
(package (ref: 1)) | ||
(superclass (ref: Object)) | ||
(traits (ref: 3)) | ||
(properties | ||
(FM3.Property (id: 6) | ||
(name 'authors') | ||
(class (ref: 7)) | ||
(container false) | ||
(derived false) | ||
(multivalued true) | ||
(opposite (ref: 5)) | ||
(type (ref: 2))) | ||
(FM3.Property (id: 11) | ||
(name 'title') | ||
(class (ref: 7)) | ||
(container false) | ||
(derived false) | ||
(multivalued false) | ||
(type (ref: String)))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package ch.akuhn.fame.internal; | ||
|
||
|
||
/** | ||
* This class a helps printing model in JSON | ||
* | ||
* @author Sabri.BENBRAHIM, Benoit "badetitou" VERHAEGHE | ||
*/ | ||
public class JSONPrinter extends AbstractPrintClient { | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param stream in which we have to write | ||
*/ | ||
public JSONPrinter(Appendable stream) { | ||
super(stream); | ||
} | ||
|
||
@Override | ||
public void beginAttribute(String name) { | ||
lntabs(); | ||
append("\""); | ||
append(name); | ||
append("\""); | ||
append(":"); | ||
|
||
} | ||
|
||
@Override | ||
public void beginDocument() { | ||
append("["); | ||
} | ||
|
||
@Override | ||
public void beginElement(String name) { | ||
this.indentation++; | ||
append("{"); | ||
lntabs(); | ||
append("\"FM3\":\""); | ||
append(name); | ||
append("\","); | ||
lntabs(); | ||
} | ||
|
||
public void beginMultivalue(String name) { | ||
append("["); | ||
} | ||
|
||
@Override | ||
public void directive(String name, String... params) { | ||
} | ||
|
||
@Override | ||
public void endAttribute(String name) { | ||
} | ||
|
||
@Override | ||
public void endDocument() { | ||
lntabs(); | ||
append("]"); | ||
close(); | ||
} | ||
|
||
@Override | ||
public void endElement(String name) { | ||
lntabs(); | ||
append("}"); | ||
this.indentation--; | ||
} | ||
|
||
public void endMultivalue(String name) { | ||
append("]"); | ||
} | ||
|
||
@Override | ||
public void primitive(Object value) { | ||
append('"'); | ||
if (value.getClass() == String.class){ | ||
for (char c : ((String) value).toCharArray()) { | ||
if (c == '"') { | ||
append('\\'); | ||
} else if (c == '\\') { | ||
append('\\'); | ||
} | ||
append(c); | ||
} | ||
} else { | ||
append(value.toString()); | ||
} | ||
append('"'); | ||
} | ||
|
||
@Override | ||
public void reference(int index) { | ||
append(" "); | ||
append("{"); | ||
append("\"ref\":"); | ||
append(" "); | ||
append(String.valueOf(index)); | ||
append("}"); | ||
} | ||
|
||
@Override | ||
public void reference(String name) { | ||
append(" "); | ||
append("{"); | ||
append("\"ref\":"); | ||
append(" "); | ||
append("\""); | ||
append(name); | ||
append("\""); | ||
append("}"); | ||
} | ||
|
||
@Override | ||
public void reference(String name, int index) { | ||
} | ||
|
||
@Override | ||
public void serial(int index) { | ||
append("\"id\":"); | ||
append(String.valueOf(index)); | ||
append(","); | ||
} | ||
|
||
public void printEntitySeparator() { | ||
append(","); | ||
} | ||
|
||
public void printPropertySeparator() { | ||
append(","); | ||
} | ||
|
||
} |
Oops, something went wrong.