All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.harrel.jsonschema.providers.JettisonNode Maven / Gradle / Ivy

The newest version!
package dev.harrel.jsonschema.providers;

import dev.harrel.jsonschema.JsonNode;
import dev.harrel.jsonschema.JsonNodeFactory;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONTokener;

import java.util.*;

public final class JettisonNode extends SimpleJsonNode {

    private JettisonNode(Object node, String jsonPointer) {
        super(Objects.requireNonNull(node), jsonPointer);
    }

    public JettisonNode(Object node) {
        this(node, "");
    }

    @Override
    public boolean asBoolean() {
        return (Boolean) node;
    }

    @Override
    public String asString() {
        return Objects.toString(isNull() ? null : node);
    }

    @Override
    List createArray() {
        JSONArray arrayNode = (JSONArray) node;
        List elements = new ArrayList<>(arrayNode.length());
        for (int i = 0; i < arrayNode.length(); ++i) {
            elements.add(new JettisonNode(arrayNode.opt(i), jsonPointer + "/" + elements.size()));
        }
        return elements;
    }

    @Override
    @SuppressWarnings("unchecked")
    Map createObject() {
        JSONObject jsonObject = (JSONObject) node;
        Map map = MapUtil.newHashMap(jsonObject.length());
        for (Object object : jsonObject.toMap().entrySet()) {
            Map.Entry entry = (Map.Entry) object;
            map.put(entry.getKey().toString(), new JettisonNode(entry.getValue(), jsonPointer + "/" + JsonNode.encodeJsonPointer(entry.getKey().toString())));
        }
        return map;
    }

    @Override
    boolean isNull(Object node) {
        return JSONObject.NULL.equals(node) || JSONObject.EXPLICIT_NULL.equals(node);
    }

    @Override
    boolean isArray(Object node) {
        return node instanceof JSONArray;
    }

    @Override
    boolean isObject(Object node) {
        return node instanceof JSONObject;
    }

    public static final class Factory implements JsonNodeFactory {
        @Override
        public JsonNode wrap(Object node) {
            if (node instanceof JettisonNode) {
                return new JettisonNode(((JettisonNode) node).node);
            } else {
                return new JettisonNode(node);
            }
        }

        @Override
        public JsonNode create(String rawJson) {
            try {
                return new JettisonNode(new BigDecimalTokener(rawJson).nextValue());
            } catch (JSONException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }

    private static final class BigDecimalTokener extends JSONTokener {
        private BigDecimalTokener(String s) {
            super(s);
            this.useBigDecimal = true;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy