+ writeName(node, schema, writer);
+ writeValueOfNodeByType(writer, node, schema.getType(), schema);
+ }
+
+ 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);
+ } else {
+ String value = String.valueOf(RestCodec.from(baseType, mountPoint).serialize(node.getValue()));
+ if (value == null) {
+ value = String.valueOf(node.getValue());
+ }
+ writer.value(value.equals("null") ? "" : value);
+ }
+ }
+
+ private void writeIdentityValuesDTOToJson(JsonWriter writer, IdentityValuesDTO valueDTO) throws IOException {
+ StringBuilder result = new StringBuilder();
+ for (IdentityValue identityValue : valueDTO.getValuesWithNamespaces()) {
+ result.append("/");
+
+ writeModuleNameAndIdentifier(result, identityValue);
+ if (identityValue.getPredicates() != null && !identityValue.getPredicates().isEmpty()) {
+ for (Predicate predicate : identityValue.getPredicates()) {
+ IdentityValue identityValuePredicate = predicate.getName();
+ result.append("[");
+ if (identityValuePredicate == null) {
+ result.append(".");
+ } else {
+ writeModuleNameAndIdentifier(result, identityValuePredicate);
+ }
+ result.append("='");
+ result.append(predicate.getValue());
+ result.append("'");
+ result.append("]");
+ }
+ }
+ }
+
+ writer.value(result.toString());