-
- 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) {
+
+ private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
+ throws IOException {
+
+ String value = String.valueOf(node.getValue());
+ // TODO check Leafref, InstanceIdentifierTypeDefinition,
+ // IdentityrefTypeDefinition, UnionTypeDefinition
+ TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
+ if (baseType instanceof InstanceIdentifierTypeDefinition) {
+ writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString());
+ } else if (baseType instanceof UnionTypeDefinition) {
+ processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value);
+ } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
+ || baseType instanceof UnsignedIntegerTypeDefinition) {
+ writer.value(new NumberForJsonWriter(value));
+ } else if (baseType instanceof BooleanTypeDefinition) {
+ writer.value(Boolean.parseBoolean(value));
+ } else if (baseType instanceof EmptyTypeDefinition) {
+ writeEmptyDataTypeToJson(writer);
+ } else {
+ writer.value(value.equals("null") ? "" : value);
+ }
+ }
+
+ private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value)
+ throws IOException {
+ if (value == null) {
+ writeEmptyDataTypeToJson(writer);
+ } else if ((isNumber(value))
+ && containsType(unionType, UnsignedIntegerTypeDefinition.class, IntegerTypeDefinition.class,
+ DecimalTypeDefinition.class)) {