2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import com.google.common.base.Preconditions;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
16 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 final class JSONStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
23 implements JSONCodec<YangInstanceIdentifier> {
24 private final DataSchemaContextTree dataContextTree;
25 private final JSONCodecFactory codecFactory;
26 private final SchemaContext context;
28 JSONStringInstanceIdentifierCodec(final SchemaContext context, final JSONCodecFactory jsonCodecFactory) {
29 this.context = Preconditions.checkNotNull(context);
30 this.dataContextTree = DataSchemaContextTree.from(context);
31 this.codecFactory = Preconditions.checkNotNull(jsonCodecFactory);
35 protected Module moduleForPrefix(final String prefix) {
36 return context.findModuleByName(prefix, null);
40 protected String prefixForNamespace(final URI namespace) {
41 final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
42 return module == null ? null : module.getName();
46 protected DataSchemaContextTree getDataContextTree() {
47 return dataContextTree;
51 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final String value) {
52 Preconditions.checkNotNull(schemaNode, "schemaNode cannot be null");
53 Preconditions.checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
54 final JSONCodec<?> objectJSONCodec = codecFactory.codecFor(schemaNode);
55 return objectJSONCodec.deserialize(value);
59 * Serialize YangInstanceIdentifier with specified JsonWriter.
61 * @param writer JsonWriter
62 * @param value YangInstanceIdentifier
65 public void serializeToWriter(final JsonWriter writer, final YangInstanceIdentifier value) throws IOException {
66 writer.value(serialize(value));