Write first CompositeNode into Json 10/2610/2
authormsunal <msunal@cisco.com>
Mon, 11 Nov 2013 14:45:31 +0000 (15:45 +0100)
committermsunal <msunal@cisco.com>
Tue, 12 Nov 2013 15:09:42 +0000 (16:09 +0100)
- Json format is include first CompositeNode in object
- resolved bug: translation of EmptyTypeDefinition type to Json

Change-Id: I3c9041a5c4444d9e3e15eca7d9ca3e1ef3121a22
Signed-off-by: Martin Sunal <msunal@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java

index 55751e5ac7c33a583ea8d4ea36fdf2a4dbe2ce14..04556bbe547a8d22acd9d8584be1ff4386c1ed5f 100644 (file)
@@ -7,6 +7,8 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import javax.activation.UnsupportedDataTypeException;
+
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
@@ -33,8 +35,18 @@ class JsonMapper {
     
     public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
         writer.beginObject();
-        writeChildrenOfParent(writer, data, schema);
+        
+        if (schema instanceof ContainerSchemaNode) {
+            writeContainer(writer, (CompositeNode) data, (ContainerSchemaNode) schema);
+        } else if (schema instanceof ListSchemaNode) {
+            writeList(writer, (CompositeNode) data, (ListSchemaNode) schema);
+        } else {
+            throw new UnsupportedDataTypeException(
+                    "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
+        }
+        
         writer.endObject();
+        
         foundLeafLists.clear();
         foundLists.clear();
     }
@@ -45,6 +57,11 @@ class JsonMapper {
         
         for (Node<?> child : parent.getChildren()) {
             DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes());
+            if (childSchema == null) {
+                throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
+                        + "\" is not conform to schema");
+            }
+            
             if (childSchema instanceof ContainerSchemaNode) {
                 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
             } else if (childSchema instanceof ListSchemaNode) {
@@ -59,6 +76,9 @@ class JsonMapper {
                 }
             } else if (childSchema instanceof LeafSchemaNode) {
                 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
+            } else {
+                throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
+                        + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
             }
         }
         
@@ -145,7 +165,9 @@ class JsonMapper {
         } else if (type instanceof BooleanTypeDefinition) {
             writer.value(Boolean.parseBoolean(value));
         } else if (type instanceof EmptyTypeDefinition) {
-            writer.value("[null]");
+            writer.beginArray();
+            writer.nullValue();
+            writer.endArray();
         } else {
             writer.value(value);
         }