| 10 | import java.io.IOException; |
| 11 | |
| 12 | public class YamlSerializer implements Serializer, java.io.Serializable { |
| 13 | |
| 14 | private static final long serialVersionUID = 1L; |
| 15 | |
| 16 | @SuppressWarnings("unused") |
| 17 | private final org.rumbledb.serialization.SerializationParameters params; |
| 18 | |
| 19 | public YamlSerializer(SerializationParameters params) { |
| 20 | this.params = params; |
| 21 | } |
| 22 | |
| 23 | @Override |
| 24 | public String serialize(Item i) { |
| 25 | StringBuffer sb = new StringBuffer(); |
| 26 | serialize(i, sb, "", true); |
| 27 | return sb.toString(); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public void serialize(Item item, StringBuffer sb, String indent, boolean isTopLevel) { |
| 32 | YAMLFactory yamlFactory = new YAMLFactory(); |
| 33 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 34 | try { |
| 35 | YAMLGenerator yamlGenerator = yamlFactory.createGenerator(baos); |
| 36 | generateYAML(item, yamlGenerator); |
| 37 | yamlGenerator.flush(); |
| 38 | } catch (IOException ioe) { |
| 39 | RuntimeException e = new OurBadException("Not able to output YAML."); |
| 40 | e.initCause(ioe); |
| 41 | throw e; |
| 42 | } |
| 43 | sb.append(baos.toString()); |
| 44 | } |
| 45 | |
| 46 | private void generateYAML(Item item, YAMLGenerator yamlGenerator) throws IOException { |
| 47 | if (item.isFunction()) { |
| 48 | throw new FunctionsNonSerializableException(); |
| 49 | } |
| 50 | if (item.isAtomic()) { |
| 51 | generateYAMLAtomicValue(item, yamlGenerator); |
| 52 | return; |
| 53 | } |
| 54 | if (item.isArray()) { |
| 55 | yamlGenerator.writeStartArray(); |
| 56 | for (Item member : item.getItemMembers()) { |
| 57 | generateYAML(member, yamlGenerator); |
| 58 | } |
| 59 | yamlGenerator.writeEndArray(); |
| 60 | return; |
| 61 | } |
| 62 | if (item.isMap() && !item.isObject()) { |
| 63 | yamlGenerator.writeStartObject(); |
| 64 | for (Item key : item.getItemKeys()) { |
| 65 | yamlGenerator.writeFieldName(key.getStringValue()); |
| 66 | appendMapValue(item, key, yamlGenerator); |
| 67 | } |
| 68 | yamlGenerator.writeEndObject(); |
| 69 | return; |
nothing calls this directly
no outgoing calls
no test coverage detected