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.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
47 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
48 * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
51 public abstract class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
52 JSONCodecFactory(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
53 super(context, cache);
57 protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
58 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
62 protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
63 return new BooleanJSONCodec(BooleanStringCodec.from(type));
67 protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
68 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
72 protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
73 return wrapDecimalCodec(DecimalStringCodec.from(type));
77 protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
78 return EmptyJSONCodec.INSTANCE;
82 protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
83 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
87 protected final JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
88 return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
92 protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
93 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
97 protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
98 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
102 protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
103 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
107 protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
108 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
112 protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
113 return new QuotedJSONCodec<>(StringStringCodec.from(type));
117 protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
118 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
122 protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
123 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
127 protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
128 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
132 protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
133 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
137 protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
138 return UnionJSONCodec.create(type, codecs);
142 protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
143 return NullJSONCodec.INSTANCE;
147 protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
149 // Returns a one-off factory for the purposes of normalizing an anydata tree.
151 // 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
152 // whether our cache is thread-safe -- in which case we should just return this.
153 // The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
154 // to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
156 // The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
157 // factory due to that potentially creating a circular reference.
158 final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
159 return rebaseTo(newSchemaContext, new LazyCodecCache<>());
162 abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
164 abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
166 abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);