1 package org.opendaylight.controller.sal.rest.impl;
3 import static org.opendaylight.controller.sal.restconf.impl.MediaTypes.API;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.lang.annotation.Annotation;
9 import java.lang.reflect.Type;
11 import javax.ws.rs.Produces;
12 import javax.ws.rs.WebApplicationException;
13 import javax.ws.rs.core.MediaType;
14 import javax.ws.rs.core.MultivaluedMap;
15 import javax.ws.rs.ext.MessageBodyWriter;
16 import javax.ws.rs.ext.Provider;
18 import org.opendaylight.controller.sal.rest.api.RestconfService;
19 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import com.google.gson.stream.JsonWriter;
25 @Produces({ API + RestconfService.JSON })
26 public enum StructuredDataToJsonProvider implements MessageBodyWriter<StructuredData> {
30 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
35 public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
40 public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
41 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
42 throws IOException, WebApplicationException {
43 JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, "UTF-8"));
44 writer.setIndent(" ");
45 JsonMapper jsonMapper = new JsonMapper();
46 jsonMapper.write(writer, t.getData(), (DataNodeContainer) t.getSchema());