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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import java.util.List;
15 import java.util.function.BiFunction;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
24 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
25 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
26 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
49 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
50 * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
53 public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
54 private final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier;
55 private final JSONCodec<?> iidCodec;
57 JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache,
58 final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier) {
59 super(context, cache);
60 this.iidCodecSupplier = requireNonNull(iidCodecSupplier);
61 iidCodec = verifyNotNull(iidCodecSupplier.apply(context, this));
65 protected JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
66 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
70 protected JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
71 return new BooleanJSONCodec(BooleanStringCodec.from(type));
75 protected JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
76 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
80 protected JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
81 return new NumberJSONCodec<>(DecimalStringCodec.from(type));
85 protected JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
86 return EmptyJSONCodec.INSTANCE;
90 protected JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
91 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
95 protected JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
96 return new IdentityrefJSONCodec(getSchemaContext(), module);
100 protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
105 protected JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
106 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
110 protected JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
111 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
115 protected JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
116 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
120 protected JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
121 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
125 protected JSONCodec<?> stringCodec(final StringTypeDefinition type) {
126 return new QuotedJSONCodec<>(StringStringCodec.from(type));
130 protected JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
131 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
135 protected JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
136 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
140 protected JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
141 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
145 protected JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
146 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
150 protected JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
151 return UnionJSONCodec.create(type, codecs);
155 protected JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
156 return NullJSONCodec.INSTANCE;
159 // Returns a one-off factory for the purposes of normalizing an anydata tree.
161 // FIXME: 5.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
162 // whether our cache is thread-safe -- in which case we should just return this.
163 // The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
164 // to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
166 // The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
167 // factory due to that potentially creating a circular reference.
168 JSONCodecFactory rebaseTo(final SchemaContext newSchemaContext) {
169 return new JSONCodecFactory(newSchemaContext, new LazyCodecCache<>(), iidCodecSupplier);