BUG-7983: unify JSONCodec and XmlCodec methods
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONStringIdentityrefCodec.java
index 7b00dc5dd2c132c3987cfd833d7a1bf0240c6213..c8ecc9e34e7d9dfdcc87b22fd48e0a2cf4fb1785 100644 (file)
@@ -7,55 +7,42 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
-import com.google.common.base.Preconditions;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
-import java.net.URI;
-
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringIdentityrefCodec;
+import org.opendaylight.yangtools.yang.data.util.ModuleStringIdentityrefCodec;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec implements JSONCodec<QName> {
-    private final SchemaContext context;
-    private final QNameModule parentModuleQname;
-
+final class JSONStringIdentityrefCodec extends ModuleStringIdentityrefCodec implements JSONCodec<QName> {
     JSONStringIdentityrefCodec(final SchemaContext context, final QNameModule parentModule) {
-        this.context = Preconditions.checkNotNull(context);
-        this.parentModuleQname = Preconditions.checkNotNull(parentModule);
+        super(context, parentModule);
     }
 
     @Override
-    protected Module moduleForPrefix(final String prefix) {
+    protected Module moduleForPrefix(@Nonnull final String prefix) {
         if (prefix.isEmpty()) {
             return context.findModuleByNamespaceAndRevision(parentModuleQname.getNamespace(),
                     parentModuleQname.getRevision());
-        } else {
-            return context.findModuleByName(prefix, null);
         }
+
+        return context.findModuleByName(prefix, null);
     }
 
     @Override
-    protected String prefixForNamespace(final URI namespace) {
-        final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
-        return module == null ? null : module.getName();
+    public Class<QName> getDataType() {
+        return QName.class;
     }
 
     @Override
-    public boolean needQuotes() {
-        return true;
+    public QName parseValue(final Object ctx, final String str) {
+        return deserialize(str);
     }
 
-    /**
-     * Serialize QName with specified JsonWriter.
-     *
-     * @param writer JsonWriter
-     * @param value QName
-     */
     @Override
-    public void serializeToWriter(JsonWriter writer, QName value) throws IOException {
-        writer.value(serialize(value));
+    public void writeValue(final JsonWriter ctx, final QName value) throws IOException {
+        ctx.value(serialize(value));
     }
 }