| 6 | import org.rumbledb.items.xml.NamespaceItem; |
| 7 | |
| 8 | public class JsonSerializer implements Serializer, java.io.Serializable { |
| 9 | |
| 10 | private static final long serialVersionUID = 1L; |
| 11 | |
| 12 | private final org.rumbledb.serialization.SerializationParameters params; |
| 13 | |
| 14 | public JsonSerializer(SerializationParameters params) { |
| 15 | this.params = params; |
| 16 | } |
| 17 | |
| 18 | @Override |
| 19 | public String serialize(Item i) { |
| 20 | StringBuffer sb = new StringBuffer(); |
| 21 | serialize(i, sb, "", true); |
| 22 | return sb.toString(); |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public void serialize(Item item, StringBuffer sb, String indent, boolean isTopLevel) { |
| 27 | if (item.isFunction()) { |
| 28 | throw new FunctionsNonSerializableException(); |
| 29 | } |
| 30 | if (item.isAtomic()) { |
| 31 | appendJSONAtomicItem(item, sb); |
| 32 | return; |
| 33 | } |
| 34 | if (item.isArray()) { |
| 35 | sb.append("["); |
| 36 | |
| 37 | String separator = " "; |
| 38 | if (this.params.getIndent()) { |
| 39 | separator = "\n" + indent + " "; |
| 40 | } |
| 41 | boolean firstTime = true; |
| 42 | for (Item member : item.getItemMembers()) { |
| 43 | sb.append(separator); |
| 44 | if (firstTime) { |
| 45 | separator = "," + separator; |
| 46 | firstTime = false; |
| 47 | } |
| 48 | if (this.params.getIndent()) { |
| 49 | serialize(member, sb, indent + " ", false); |
| 50 | } else { |
| 51 | serialize(member, sb, "", false); |
| 52 | } |
| 53 | } |
| 54 | if (this.params.getIndent()) { |
| 55 | sb.append("\n" + indent); |
| 56 | } else { |
| 57 | sb.append(" "); |
| 58 | } |
| 59 | sb.append("]"); |
| 60 | return; |
| 61 | } |
| 62 | if (item.isObject()) { |
| 63 | sb.append("{"); |
| 64 | String separator = " "; |
| 65 | if (this.params.getIndent()) { |
nothing calls this directly
no outgoing calls
no test coverage detected