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 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.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec {
+final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec implements JSONCodec<QName> {
private final SchemaContext context;
+ private final QNameModule parentModuleQname;
- JSONStringIdentityrefCodec(final SchemaContext context) {
+ JSONStringIdentityrefCodec(final SchemaContext context, final QNameModule parentModule) {
this.context = Preconditions.checkNotNull(context);
+ this.parentModuleQname = Preconditions.checkNotNull(parentModule);
}
@Override
protected Module moduleForPrefix(final String prefix) {
- return context.findModuleByName(prefix, null);
+ if (prefix.isEmpty()) {
+ return context.findModuleByNamespaceAndRevision(parentModuleQname.getNamespace(),
+ parentModuleQname.getRevision());
+ } else {
+ return context.findModuleByName(prefix, null);
+ }
}
@Override
return module == null ? null : module.getName();
}
+ @Override
+ public boolean needQuotes() {
+ return true;
+ }
+
+ /**
+ * 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));
+ }
}