package com.github.nsnjson.encoding;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;

import java.util.Optional;

import static com.github.nsnjson.format.CustomFormat.*;

public class CustomEncoding extends AbstractEncoding {

    @Override
    public Optional<JsonNode> encodeNull() {
        ArrayNode presentation = new ObjectMapper().createArrayNode();
        presentation.add(TYPE_NAME_NULL);

        return Optional.of(presentation);
    }

    @Override
    public Optional<JsonNode> encodeNumber(NumericNode value) {
        ArrayNode presentation = new ObjectMapper().createArrayNode();
        presentation.add(TYPE_NAME_NUMBER);
        presentation.add(value);

        return Optional.of(presentation);
    }

    @Override
    public Optional<JsonNode> encodeString(TextNode value) {
        ArrayNode presentation = new ObjectMapper().createArrayNode();
        presentation.add(TYPE_NAME_STRING);
        presentation.add(value);

        return Optional.of(presentation);
    }

    @Override
    public Optional<JsonNode> encodeBoolean(BooleanNode value) {
        ArrayNode presentation = new ObjectMapper().createArrayNode();
        presentation.add(TYPE_NAME_BOOLEAN);
        presentation.add(value);

        return Optional.of(presentation);
    }

    @Override
    public Optional<JsonNode> encodeArray(ArrayNode array) {
        ObjectMapper objectMapper = new ObjectMapper();

        ArrayNode presentation = objectMapper.createArrayNode();
        presentation.add(TYPE_NAME_ARRAY);

        for (JsonNode value : array) {
            Optional<JsonNode> itemPresentationOption = encode(value);

            if (itemPresentationOption.isPresent()) {
                JsonNode itemPresentation = itemPresentationOption.get();

                presentation.add(itemPresentation);
            }
        }

        return Optional.of(presentation);
    }

    @Override
    public Optional<JsonNode> encodeObject(ObjectNode object) {
        ObjectMapper objectMapper = new ObjectMapper();

        ArrayNode presentation = objectMapper.createArrayNode();
        presentation.add(TYPE_NAME_OBJECT);

        object.fieldNames().forEachRemaining(name -> {
            JsonNode value = object.get(name);

            Optional<JsonNode> valuePresentationOption = encode(value);

            if (valuePresentationOption.isPresent()) {
                JsonNode valuePresentation = valuePresentationOption.get();

                ArrayNode fieldPresentation = objectMapper.createArrayNode();
                fieldPresentation.add(name);
                fieldPresentation.add(valuePresentation);

                presentation.add(fieldPresentation);
            }
        });

        return Optional.of(presentation);
    }

}
@@ -1605,213 +1605,32 @@
     
-Optional%3CJsonNode%3E itemPresentationOption = encode(value);%0A%0A            if (itemPresentationOption.isPresent()) %7B%0A                JsonNode itemPresentation = itemPresentationOption.get();%0A%0A                
+encode(value).ifPresent(
 pres
@@ -1641,45 +1641,15 @@
 tion
-.add(itemPresentation);%0A            %7D
+::add);
 %0A   
@@ -2069,119 +2069,32 @@
     
-Optional%3CJsonNode%3E valuePresentationOption = encode(value);%0A%0A            if (valuePresentationOption.is
+encode(value).if
 Present(
 )) %7B
@@ -2093,89 +2093,30 @@
 ent(
-)) %7B%0A                JsonNode valuePresentation = valuePresentationOption.get();%0A
+valuePresentation -%3E %7B
 %0A   
@@ -2343,24 +2343,24 @@
 sentation);%0A
-
             
@@ -2360,16 +2360,18 @@
        %7D
+);
 %0A       

Refactor CustomEncoding