/*
 * The MIT License
 *
 * Copyright (c) 2017 WANG Lingsong
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.jsfr.json.provider;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.jsfr.json.exception.JsonSurfingException;

public class JacksonProvider implements JsonProvider<ObjectNode, ArrayNode, JsonNode> {
    
    public static final JacksonProvider INSTANCE = new JacksonProvider();
    
    private ObjectMapper om;
    
    private JsonNodeFactory factory;
    
    public JacksonProvider() {
        this(new ObjectMapper());
    }
    
    public JacksonProvider(ObjectMapper om) {
        this.om = om;
        this.factory = om.getNodeFactory();
    }
    
    @Override
    public ObjectNode createObject() {
        return factory.objectNode();
    }
    
    @Override
    public ArrayNode createArray() {
        return factory.arrayNode();
    }
    
    @Override
    public void put(ObjectNode object, String key, JsonNode value) {
        object.set(key, value);
    }
    
    @Override
    public void add(ArrayNode array, JsonNode value) {
        array.add(value);
    }
    
    @Override
    public Object resolve(ObjectNode object, String key) {
        return object.get(key);
    }
    
    @Override
    public Object resolve(ArrayNode array, int index) {
        return array.get(index);
    }
    
    @Override
    public JsonNode primitive(boolean value) {
        return factory.booleanNode(value);
    }
    
    @Override
    public JsonNode primitive(int value) {
        return factory.numberNode(value);
    }
    
    @Override
    public JsonNode primitive(double value) {
        return factory.numberNode(value);
    }
    
    @Override
    public JsonNode primitive(long value) {
        return factory.numberNode(value);
    }
    
    @Override
    public JsonNode primitive(String value) {
        return factory.textNode(value);
    }
    
    @Override
    public JsonNode primitiveNull() {
        return factory.nullNode();
    }
    
    @Override
    public <T> T cast(JsonNode value, Class<T> tClass) {
        try {
            if (value == null) {
                return null;
            } else if (om.canDeserialize(om.getTypeFactory().constructType(tClass))) {
                return om.treeToValue(value, tClass);
            } else {
                return tClass.cast(value);
            }
        } catch (JsonProcessingException e) {
            throw new JsonSurfingException(e);
        }
    }
    
}

@@ -1637,21 +1637,17 @@
 Node%3E %7B%0A
-    
 %0A
+
     publ
@@ -1712,21 +1712,17 @@
 ider();%0A
-    
 %0A
+
     priv
@@ -1742,21 +1742,17 @@
 per om;%0A
-    
 %0A
+
     priv
@@ -1780,21 +1780,17 @@
 actory;%0A
-    
 %0A
+
     publ
@@ -1848,29 +1848,25 @@
 r());%0A    %7D%0A
-    
 %0A
+
     public J
@@ -1963,37 +1963,33 @@
 actory();%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2060,37 +2060,33 @@
 ctNode();%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2154,37 +2154,33 @@
 ayNode();%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2276,37 +2276,33 @@
 , value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2378,37 +2378,33 @@
 d(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2490,37 +2490,33 @@
 get(key);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2600,37 +2600,33 @@
 t(index);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2711,37 +2711,33 @@
 e(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2817,37 +2817,33 @@
 e(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -2926,37 +2926,33 @@
 e(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -3033,37 +3033,33 @@
 e(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -3140,37 +3140,33 @@
 e(value);%0A    %7D%0A
-    
 %0A
+
     @Override%0A  
@@ -3242,21 +3242,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -3718,15 +3718,11 @@
 %7D%0A    %7D%0A
-    
 %0A%7D%0A

format code