*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import static com.google.common.base.Verify.verifyNotNull;
+
import com.google.common.annotations.Beta;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
+import java.util.function.BiFunction;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
* a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
*/
@Beta
-public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>>
- permits Lhotka02JSONCodecFactory, RFC7951JSONCodecFactory {
- JSONCodecFactory(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
+ static final class Lhotka02 extends JSONCodecFactory {
+ Lhotka02(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+ super(context, cache, JSONInstanceIdentifierCodec.Lhotka02::new);
+ }
+
+ @Override
+ Lhotka02 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
+ return new Lhotka02(newSchemaContext, newCache);
+ }
+
+ @Override
+ JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
+ return new NumberJSONCodec<>(decimalCodec);
+ }
+
+ @Override
+ JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
+ return new NumberJSONCodec<>(integerCodec);
+ }
+ }
+
+ static final class RFC7951 extends JSONCodecFactory {
+ RFC7951(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
+ super(context, cache, JSONInstanceIdentifierCodec.RFC7951::new);
+ }
+
+ @Override
+ RFC7951 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
+ return new RFC7951(newSchemaContext, newCache);
+ }
+
+ @Override
+ JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
+ return new QuotedJSONCodec<>(decimalCodec);
+ }
+
+ @Override
+ JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
+ return new QuotedJSONCodec<>(integerCodec);
+ }
+ }
+
+ private final @NonNull JSONInstanceIdentifierCodec iidCodec;
+
+ @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
+ justification = "https://github.com/spotbugs/spotbugs/issues/1867")
+ private JSONCodecFactory(final @NonNull EffectiveModelContext context,
+ final @NonNull CodecCache<JSONCodec<?>> cache,
+ final BiFunction<EffectiveModelContext, JSONCodecFactory, @NonNull JSONInstanceIdentifierCodec> iidCodec) {
super(context, cache);
+ this.iidCodec = verifyNotNull(iidCodec.apply(context, this));
}
@Override
return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
}
+ @Override
+ protected final JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
+ return iidCodec;
+ }
+
@Override
protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
return NullJSONCodec.INSTANCE;
}
- @Override
- protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
-
// Returns a one-off factory for the purposes of normalizing an anydata tree.
//
// FIXME: 7.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
RFC7951() {
@Override
JSONCodecFactory createFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
- return new RFC7951JSONCodecFactory(context, cache);
+ return new JSONCodecFactory.RFC7951(context, cache);
}
},
/**
DRAFT_LHOTKA_NETMOD_YANG_JSON_02() {
@Override
JSONCodecFactory createFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
- return new Lhotka02JSONCodecFactory(context, cache);
+ return new JSONCodecFactory.Lhotka02(context, cache);
}
};
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
-abstract class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
+abstract sealed class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
implements JSONCodec<YangInstanceIdentifier> {
+ static final class Lhotka02 extends JSONInstanceIdentifierCodec {
+ Lhotka02(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
+ super(context, jsonCodecFactory);
+ }
+ }
+
+ static final class RFC7951 extends JSONInstanceIdentifierCodec {
+ RFC7951(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
+ super(context, jsonCodecFactory);
+ }
+
+ @Override
+ protected StringBuilder appendQName(final StringBuilder sb, final QName qname, final QNameModule lastModule) {
+ return qname.getModule().equals(lastModule) ? sb.append(qname.getLocalName())
+ : super.appendQName(sb, qname, lastModule);
+ }
+
+ @Override
+ protected QName createQName(final QNameModule lastModule, final String localName) {
+ checkArgument(lastModule != null, "Unprefixed leading name %s", localName);
+ return QName.create(lastModule, localName);
+ }
+ }
+
private final @NonNull DataSchemaContextTree dataContextTree;
private final JSONCodecFactory codecFactory;
private final EffectiveModelContext context;
final String value) {
requireNonNull(schemaNode, "schemaNode cannot be null");
checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
- final JSONCodec<?> objectJSONCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
- return objectJSONCodec.parseValue(null, value);
+ return codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver).parseValue(null, value);
}
@Override
+++ /dev/null
-/*
- * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
-import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
-import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-
-final class Lhotka02JSONCodecFactory extends JSONCodecFactory {
- private final JSONInstanceIdentifierCodec iidCodec;
-
- Lhotka02JSONCodecFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
- super(context, cache);
- iidCodec = new Lhotka02JSONInstanceIdentifierCodec(context, this);
- }
-
- @Override
- protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
- return iidCodec;
- }
-
- @Override
- Lhotka02JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext,
- final CodecCache<JSONCodec<?>> newCache) {
- return new Lhotka02JSONCodecFactory(newSchemaContext, newCache);
- }
-
- @Override
- JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
- return new NumberJSONCodec<>(decimalCodec);
- }
-
- @Override
- JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
- return new NumberJSONCodec<>(integerCodec);
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-
-final class Lhotka02JSONInstanceIdentifierCodec extends JSONInstanceIdentifierCodec {
- Lhotka02JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
- super(context, jsonCodecFactory);
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
-import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
-import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-
-final class RFC7951JSONCodecFactory extends JSONCodecFactory {
- private final RFC7951JSONInstanceIdentifierCodec iidCodec;
-
- RFC7951JSONCodecFactory(final EffectiveModelContext context, final CodecCache<JSONCodec<?>> cache) {
- super(context, cache);
- iidCodec = new RFC7951JSONInstanceIdentifierCodec(context, this);
- }
-
- @Override
- protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
- return iidCodec;
- }
-
- @Override
- JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
- return new RFC7951JSONCodecFactory(newSchemaContext, newCache);
- }
-
- @Override
- JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
- return new QuotedJSONCodec<>(decimalCodec);
- }
-
- @Override
- JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
- return new QuotedJSONCodec<>(integerCodec);
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import static com.google.common.base.Preconditions.checkArgument;
-
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-
-final class RFC7951JSONInstanceIdentifierCodec extends JSONInstanceIdentifierCodec {
- RFC7951JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
- super(context, jsonCodecFactory);
- }
-
- @Override
- protected StringBuilder appendQName(final StringBuilder sb, final QName qname, final QNameModule lastModule) {
- if (qname.getModule().equals(lastModule)) {
- return sb.append(qname.getLocalName());
- }
-
- return super.appendQName(sb, qname, lastModule);
- }
-
- @Override
- protected QName createQName(final QNameModule lastModule, final String localName) {
- checkArgument(lastModule != null, "Unprefixed leading name %s", localName);
- return QName.create(lastModule, localName);
- }
-}