Remove HackJsonWriter 10/111710/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 16 May 2024 17:04:16 +0000 (19:04 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 17 May 2024 12:24:39 +0000 (12:24 +0000)
We have a fix for JSONCodec.unparseValue() available, hence we can
remove our HackJsonWriter.

Change-Id: I2c42920835236c3090f3f7fa6752962583b9c1d5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathCanonizer.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/HackJsonWriter.java [deleted file]

index 0ba9c63f3e76d0102b41f835dacbd0765468fb48..8b0b21210bfd1415d012a1fc99fea38eff1648f4 100644 (file)
@@ -11,7 +11,6 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableList;
-import java.io.IOException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.api.ApiPath.ApiIdentifier;
@@ -172,12 +171,7 @@ public final class ApiPathCanonizer {
             final T value) {
         @SuppressWarnings("unchecked")
         final var codec = (JSONCodec<T>) databind.jsonCodecs().codecFor(schema, stack);
-        try (var jsonWriter = new HackJsonWriter()) {
-            codec.writeValue(jsonWriter, value);
-            return jsonWriter.acquireCaptured().rawString();
-        } catch (IOException e) {
-            throw new IllegalStateException("Failed to serialize '" + value + "'", e);
-        }
+        return codec.unparseValue(value).rawString();
     }
 
     /**
diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/HackJsonWriter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/HackJsonWriter.java
deleted file mode 100644 (file)
index 55cd76e..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.restconf.server.spi;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.gson.stream.JsonWriter;
-import java.io.IOException;
-import java.io.StringWriter;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodec;
-
-/**
- * A hack to intercept {@link JSONCodec#writeValue(JsonWriter, Object)} output. This class is closely tailored to
- * respond implementation behaviour of JSONCodecs.
- */
-// FIXME: remove this class once we have YANGTOOLS-1569
-final class HackJsonWriter extends JsonWriter {
-    @NonNullByDefault
-    record Value(String rawString, Kind kind) {
-        enum Kind {
-            BOOLEAN,
-            NULL,
-            NUMBER,
-            STRING
-        }
-
-        Value {
-            requireNonNull(rawString);
-            requireNonNull(kind);
-        }
-    }
-
-    private static final Value FALSE = new Value("false", Value.Kind.BOOLEAN);
-    private static final Value TRUE = new Value("true", Value.Kind.BOOLEAN);
-    private static final Value NULL = new Value("[null]", Value.Kind.NULL);
-
-    private Value captured = null;
-
-    HackJsonWriter() {
-        super(new StringWriter());
-    }
-
-    @Override
-    public JsonWriter nullValue() throws IOException {
-        capture(NULL);
-        return super.nullValue();
-    }
-
-    @Override
-    public JsonWriter value(final boolean value) throws IOException {
-        capture(value ? TRUE : FALSE);
-        return super.value(value);
-    }
-
-    @Override
-    public JsonWriter value(final Boolean value) throws IOException {
-        // We assume non-null values
-        return value(value.booleanValue());
-    }
-
-    @Override
-    public JsonWriter value(final Number value) throws IOException {
-        capture(new Value(value.toString(), Value.Kind.NUMBER));
-        return super.value(value);
-    }
-
-    @Override
-    public JsonWriter value(final String value) throws IOException {
-        if (value == null) {
-            return nullValue();
-        }
-        capture(new Value(value, Value.Kind.STRING));
-        return super.value(value);
-    }
-
-    @NonNull Value acquireCaptured() throws IOException {
-        final var local = captured;
-        if (local == null) {
-            throw new IOException("No value set");
-        }
-        return local;
-    }
-
-    private void capture(final Value newValue) throws IOException {
-        if (captured != null) {
-            throw new IOException("Value already set to " + captured);
-        }
-        captured = newValue;
-    }
-}