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
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.data.util.ModuleStringIdentityrefCodec;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 final class JSONStringIdentityrefCodec extends ModuleStringIdentityrefCodec implements JSONCodec<QName> {
20     JSONStringIdentityrefCodec(final SchemaContext context, final QNameModule parentModule) {
21         super(context, parentModule);
22     }
23
24     @Override
25     protected Module moduleForPrefix(@Nonnull final String prefix) {
26         if (prefix.isEmpty()) {
27             return context.findModuleByNamespaceAndRevision(parentModuleQname.getNamespace(),
28                     parentModuleQname.getRevision());
29         }
30
31         return context.findModuleByName(prefix, null);
32     }
33
34     @Override
35     public Class<QName> getDataType() {
36         return QName.class;
37     }
38
39     @Override
40     public QName parseValue(final Object ctx, final String str) {
41         return deserialize(str);
42     }
43
44     @Override
45     public void writeValue(final JsonWriter ctx, final QName value) throws IOException {
46         ctx.value(serialize(value));
47     }
48 }