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

com.github.pgelinas.jackson.javax.json.JacksonObject Maven / Gradle / Ivy

Go to download

An alternate implementation of JSR-353 based on Jackson, which aims to bring better performance and configurability.

The newest version!
package com.github.pgelinas.jackson.javax.json;

import java.util.*;

import javax.json.*;

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

/**
 * JsonObject extends the Map interface, so let's have some fun...
 */
public class JacksonObject extends AbstractMap implements JsonObject, JacksonValue {
    private final NodeFactory _nodeFactory;
    private final ObjectNode _delegate;
    
    private Set> _entries;

    public JacksonObject(ObjectNode delegate, NodeFactory nodeFactory) {
        _delegate = delegate;
        _nodeFactory = nodeFactory;
    }

    @Override
    public Set> entrySet() {
        if (_entries == null) {
            _entries = new HashSet>();
            for (Iterator> iterator = _delegate.fields(); iterator.hasNext();) {
                _entries.add(new JacksonEntry(iterator.next(), _nodeFactory));
            }
            _entries = Collections.unmodifiableSet(_entries);
        }
        return _entries;
    }

    @Override
    public JsonArray getJsonArray(String name) {
        return (JsonArray) get(name);
    }

    @Override
    public JsonObject getJsonObject(String name) {
        return (JsonObject) get(name);
    }

    @Override
    public JsonNumber getJsonNumber(String name) {
        return (JsonNumber) get(name);
    }

    @Override
    public JsonString getJsonString(String name) {
        return (JsonString) get(name);
    }

    @Override
    public String getString(String name) {
        return _delegate.get(name).asText();
    }

    @Override
    public String getString(String name, String defaultValue) {
        JsonNode jsonNode = _delegate.get(name);
        return (jsonNode == null) ? defaultValue : jsonNode.asText();
    }

    @Override
    public int getInt(String name) {
        return _delegate.get(name).asInt();
    }

    @Override
    public int getInt(String name, int defaultValue) {
        JsonNode jsonNode = _delegate.get(name);
        return (jsonNode == null) ? defaultValue : jsonNode.asInt(defaultValue);
    }

    @Override
    public boolean getBoolean(String name) {
        return _delegate.get(name).asBoolean();
    }

    @Override
    public boolean getBoolean(String name, boolean defaultValue) {
        JsonNode jsonNode = _delegate.get(name);
        return (jsonNode == null) ? defaultValue : jsonNode.asBoolean(defaultValue);
    }

    @Override
    public boolean isNull(String name) {
        return _delegate.get(name).isNull();
    }

    @Override
    public ValueType getValueType() {
        return JacksonValueUtils.getValueType(this);
    }

    @Override
    public ObjectNode delegate() {
        return _delegate;
    }
    
    @Override
    public String toString() {
        return _delegate.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy