Close nested JSON writers 68/67868/4
authorStephen Kitt <skitt@redhat.com>
Fri, 2 Feb 2018 15:57:30 +0000 (16:57 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 7 Feb 2018 17:10:59 +0000 (18:10 +0100)
With I9c597fe96937867cffdcbeeddd4b1507de20f2ee,
JSONNormalizedNodeStreamWriter.createNestedWriter() leaves it up to
the caller to close the provided JsonWriter. This patch ensures that
that is the case.

Change-Id: If20a596db06e40d1636de168dd11677372964364
Signed-off-by: Stephen Kitt <skitt@redhat.com>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/jersey/providers/NormalizedNodeJsonBodyWriter.java

index 7891d4037295d4e2a7abffec93bd7a34431db1bd..b4a807f10ecdc72da66c1ff71a97bf860e1126af 100644 (file)
@@ -87,12 +87,14 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
                 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
 
         final SchemaPath path = identifierCtx.getSchemaNode().getPath();
-        final JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint());
-        jsonWriter.beginObject();
-        writeNormalizedNode(
-                jsonWriter,path,identifierCtx,data, Optional.fromNullable(context.getWriterParameters().getDepth()));
-        jsonWriter.endObject();
-        jsonWriter.flush();
+        try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
+            jsonWriter.beginObject();
+            writeNormalizedNode(
+                    jsonWriter, path, identifierCtx, data,
+                    Optional.fromNullable(context.getWriterParameters().getDepth()));
+            jsonWriter.endObject();
+            jsonWriter.flush();
+        }
     }
 
     private static void writeNormalizedNode(final JsonWriter jsonWriter, SchemaPath path,
index b89b30c12f9081f7c4da3b8943437c95de83e178..01a725f3fe27b33c49f1dd6c611bcca17eb2de48 100644 (file)
@@ -86,14 +86,14 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
         final InstanceIdentifierContext<SchemaNode> identifierCtx =
                 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
         final SchemaPath path = identifierCtx.getSchemaNode().getPath();
-        final JsonWriter jsonWriter = createJsonWriter(entityStream,
-                context.getWriterParameters().isPrettyPrint());
-
-        jsonWriter.beginObject();
-        writeNormalizedNode(jsonWriter, path, identifierCtx, data,
-                context.getWriterParameters().getDepth(), context.getWriterParameters().getFields());
-        jsonWriter.endObject();
-        jsonWriter.flush();
+
+        try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
+            jsonWriter.beginObject();
+            writeNormalizedNode(jsonWriter, path, identifierCtx, data,
+                    context.getWriterParameters().getDepth(), context.getWriterParameters().getFields());
+            jsonWriter.endObject();
+            jsonWriter.flush();
+        }
     }
 
     private static void writeNormalizedNode(final JsonWriter jsonWriter,