import java.util.List;
import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.DataSchemaNodeAwareAdaptor;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.SchemaAwareNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
/**
* This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
@Beta
public final class JsonParserStream implements Closeable, Flushable {
private final Deque<URI> namespaces = new ArrayDeque<>();
- private final NormalizedNodeStreamWriter writer;
+ private final SchemaAwareNormalizedNodeStreamWriter writer;
private final JSONCodecFactory codecs;
private final SchemaContext schema;
private final DataSchemaNode parentNode;
- private JsonParserStream(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, final DataSchemaNode parentNode) {
+ private JsonParserStream(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
+ final DataSchemaNode parentNode) {
this.schema = Preconditions.checkNotNull(schemaContext);
- this.writer = Preconditions.checkNotNull(writer);
+ this.writer = DataSchemaNodeAwareAdaptor.forWriter(writer);
this.codecs = JSONCodecFactory.create(schemaContext);
this.parentNode = parentNode;
}
- public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, final SchemaNode parentNode ) {
- if(parentNode instanceof RpcDefinition) {
+ public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
+ final SchemaNode parentNode ) {
+ if (parentNode instanceof RpcDefinition) {
return new JsonParserStream(writer, schemaContext, new RpcAsContainer((RpcDefinition) parentNode));
}
Preconditions.checkArgument(parentNode instanceof DataSchemaNode, "Instance of DataSchemaNode class awaited.");
return new JsonParserStream(writer, schemaContext, schemaContext);
}
- public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
+ public JsonParserStream parse(final JsonReader reader) {
// code copied from gson's JsonParser and Stream classes
final boolean lenient = reader.isLenient();
compositeNodeDataWithSchema.write(writer);
return this;
- // return read(reader);
} catch (final EOFException e) {
if (isEmpty) {
return this;
- // return JsonNull.INSTANCE;
}
// The stream ended prematurely so it is likely a syntax error.
throw new JsonSyntaxException(e);
- } catch (final MalformedJsonException e) {
+ } catch (final MalformedJsonException | NumberFormatException e) {
throw new JsonSyntaxException(e);
} catch (final IOException e) {
throw new JsonIOException(e);
- } catch (final NumberFormatException e) {
- throw new JsonSyntaxException(e);
} catch (StackOverflowError | OutOfMemoryError e) {
throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
} finally {
}
}
- private final void setValue(final AbstractNodeDataWithSchema parent, final String value) {
- Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent.getSchema().getQName());
+ private void setValue(final AbstractNodeDataWithSchema parent, final String value) {
+ Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type",
+ parent.getSchema().getQName());
+ final SimpleNodeDataWithSchema parentSimpleNode = (SimpleNodeDataWithSchema) parent;
+ Preconditions.checkArgument(parentSimpleNode.getValue() == null, "Node '%s' has already set its value to '%s'",
+ parentSimpleNode.getSchema().getQName(), parentSimpleNode.getValue());
- final Object translatedValue = translateValueByType(value, parent.getSchema());
- ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
+ final Object translatedValue = translateValueByType(value, parentSimpleNode.getSchema());
+ parentSimpleNode.setValue(translatedValue);
}
public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException {
}
while (in.hasNext()) {
final String jsonElementName = in.nextName();
- final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema());
+ DataSchemaNode parentSchema = parent.getSchema();
+ if (parentSchema instanceof YangModeledAnyXmlSchemaNode) {
+ parentSchema = ((YangModeledAnyXmlSchemaNode) parentSchema).getSchemaOfAnyXmlData();
+ }
+ final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
final String localName = namespaceAndName.getName();
addNamespace(namespaceAndName.getUri());
if (namesakes.contains(jsonElementName)) {
throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
}
namesakes.add(jsonElementName);
- final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace(parent.getSchema(),
+
+ final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace(parentSchema,
localName, getCurrentNamespace());
if (childDataSchemaNodes.isEmpty()) {
throw new IllegalStateException("Schema for node with name " + localName + " and namespace "
return parent instanceof ListNodeDataWithSchema || parent instanceof LeafListNodeDataWithSchema;
}
- private AbstractNodeDataWithSchema newArrayEntry(final AbstractNodeDataWithSchema parent) {
+ private static AbstractNodeDataWithSchema newArrayEntry(final AbstractNodeDataWithSchema parent) {
AbstractNodeDataWithSchema newChild;
if (parent instanceof ListNodeDataWithSchema) {
newChild = new ListEntryNodeDataWithSchema(parent.getSchema());