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.annotations.Beta;
11 import java.util.List;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
16 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
17 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
21 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
22 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
23 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
46 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
47 * a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
50 public abstract class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
51 JSONCodecFactory(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
52 super(context, cache);
56 protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
57 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
61 protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
62 return new BooleanJSONCodec(BooleanStringCodec.from(type));
66 protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
67 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
71 protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
72 return wrapDecimalCodec(DecimalStringCodec.from(type));
76 protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
77 return EmptyJSONCodec.INSTANCE;
81 protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
82 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
86 protected final JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
87 return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
91 protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
92 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
96 protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
97 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
101 protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
102 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
106 protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
107 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
111 protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
112 return new QuotedJSONCodec<>(StringStringCodec.from(type));
116 protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
117 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
121 protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
122 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
126 protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
127 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
131 protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
132 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
136 protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
137 return UnionJSONCodec.create(type, codecs);
141 protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
142 return NullJSONCodec.INSTANCE;
146 protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
148 // Returns a one-off factory for the purposes of normalizing an anydata tree.
150 // FIXME: 6.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
151 // whether our cache is thread-safe -- in which case we should just return this.
152 // The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
153 // to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
155 // The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
156 // factory due to that potentially creating a circular reference.
157 final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
158 return rebaseTo(newSchemaContext, new LazyCodecCache<>());
161 abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
163 abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
165 abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);