-
- private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type) throws IOException {
- if (!(node.getValue() instanceof String)) {
- throw new IllegalStateException("Value in SimpleNode should be type String");
- }
-
- String value = (String) node.getValue();
- // TODO check Leafref, InstanceIdentifierTypeDefinition, IdentityrefTypeDefinition, UnionTypeDefinition
- if (type.getBaseType() != null) {
- writeValueOfNodeByType(writer, node, type.getBaseType());
- } else if (type instanceof InstanceIdentifierTypeDefinition) {
- writer.value(((InstanceIdentifierTypeDefinition) type).getPathStatement().toString());
- } else if (type instanceof DecimalTypeDefinition
- || type instanceof IntegerTypeDefinition
- || type instanceof UnsignedIntegerTypeDefinition) {
- writer.value(new NumberForJsonWriter(value));
- } else if (type instanceof BooleanTypeDefinition) {
- writer.value(Boolean.parseBoolean(value));
- } else if (type instanceof EmptyTypeDefinition) {
- writer.beginArray();
- writer.nullValue();
- writer.endArray();
+
+ private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type,
+ DataSchemaNode schema) throws IOException {
+
+ TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(type);
+
+ if (node.getValue() == null && !(baseType instanceof EmptyTypeDefinition)) {
+ logger.debug("While generationg JSON output null value was found for type "
+ + baseType.getClass().getSimpleName() + ".");
+ }
+
+ // TODO check InstanceIdentifierTypeDefinition
+ if (baseType instanceof IdentityrefTypeDefinition) {
+ if (node.getValue() instanceof QName) {
+ IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType, mountPoint).serialize(
+ node.getValue());
+ IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0);
+ String moduleName;
+ if (mountPoint != null) {
+ moduleName = ControllerContext.getInstance().findModuleNameByNamespace(mountPoint,
+ URI.create(valueFromDTO.getNamespace()));
+ } else {
+ moduleName = ControllerContext.getInstance().findModuleNameByNamespace(
+ URI.create(valueFromDTO.getNamespace()));
+ }
+ writer.value(moduleName + ":" + valueFromDTO.getValue());
+ } else {
+ writeStringRepresentation(writer, node, baseType, QName.class);
+ }
+ } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
+ if (node.getValue() instanceof InstanceIdentifier) {
+ IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType, mountPoint).serialize(
+ node.getValue());
+ writeIdentityValuesDTOToJson(writer, valueDTO);
+ } else {
+ writeStringRepresentation(writer, node, baseType, InstanceIdentifier.class);
+ }
+ } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
+ || baseType instanceof UnsignedIntegerTypeDefinition) {
+ writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType, mountPoint).serialize(
+ node.getValue())));
+ } else if (baseType instanceof BooleanTypeDefinition) {
+ writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType, mountPoint).serialize(node.getValue())));
+ } else if (baseType instanceof EmptyTypeDefinition) {
+ writeEmptyDataTypeToJson(writer);