1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.json;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.winery.model.tosca.TEntityTemplate;
19 import org.eclipse.winery.model.tosca.TNodeTemplate;
20 import org.eclipse.winery.model.tosca.TRelationshipTemplate;
21 import org.eclipse.winery.model.tosca.TTopologyTemplate;
23 import com.fasterxml.jackson.core.JsonGenerator;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonSerializer;
26 import com.fasterxml.jackson.databind.SerializerProvider;
28 public class TTopologyTemplateSerializer extends JsonSerializer<TTopologyTemplate> {
31 * Does NOT wrap the result into an object. Assumes that the current
32 * position at jgen is in an object
34 * @param value the list of entity templates to serialize
36 public void serialize(List<TEntityTemplate> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
37 List<TRelationshipTemplate> relationshipTemplates = new ArrayList<TRelationshipTemplate>();
39 jgen.writeFieldName("nodeTemplates");
40 jgen.writeStartObject();
41 for (TEntityTemplate template : value) {
42 if (template instanceof TNodeTemplate) {
43 // write out as <id> : <default serialization>
44 jgen.writeFieldName(template.getId());
45 provider.defaultSerializeValue(template, jgen);
48 assert (template instanceof TRelationshipTemplate);
49 relationshipTemplates.add((TRelationshipTemplate) template);
52 jgen.writeEndObject();
54 jgen.writeFieldName("relationshipTemplates");
55 jgen.writeStartObject();
56 for (TRelationshipTemplate template : relationshipTemplates) {
57 // write out as <id> : <default serialization>
58 jgen.writeFieldName(template.getId());
59 provider.defaultSerializeValue(template, jgen);
61 jgen.writeEndObject();
65 public void serialize(TTopologyTemplate topologyTemplate, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
66 jgen.writeStartObject();
68 // write out the other fields unmodified
69 jgen.writeFieldName("documentation");
70 provider.defaultSerializeValue(topologyTemplate.getDocumentation(), jgen);
71 jgen.writeFieldName("any");
72 provider.defaultSerializeValue(topologyTemplate.getAny(), jgen);
73 jgen.writeFieldName("otherAttributes");
74 provider.defaultSerializeValue(topologyTemplate.getOtherAttributes(), jgen);
76 // finally, write the topology template
77 this.serialize(topologyTemplate.getNodeTemplateOrRelationshipTemplate(), jgen, provider);
79 jgen.writeEndObject();