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

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

Go to download

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

There is a newer version: 1.0.0
Show newest version
package com.github.pgelinas.jackson.javax.json;

import java.math.*;

import javax.json.*;

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

public class JacksonArrayBuilder implements JsonArrayBuilder {
    private final JsonNodeFactory _jsonNodeFactory;
    private final NodeFactory _nodeFactory;
    private final ArrayNode _delegate;

    public JacksonArrayBuilder(JsonNodeFactory jsonNodeFactory, NodeFactory nodeFactory) {
        _jsonNodeFactory = jsonNodeFactory;
        _nodeFactory = nodeFactory;
        _delegate = jsonNodeFactory.arrayNode();
    }

    public ArrayNode delegate() {
        return _delegate;
    }

    @Override
    public JsonArrayBuilder add(JsonValue value) {
        if (value == JsonValue.NULL) {
            _delegate.addNull();
        } else if (value == JsonValue.FALSE) {
            _delegate.add(false);
        } else if (value == JsonValue.TRUE) {
            _delegate.add(true);
        } else if (value instanceof JacksonValue) {
            _delegate.add(((JacksonValue) value).delegate());
        } else throw new UnsupportedOperationException("No compatibility with other implementation yet.");

        return this;
    }

    @Override
    public JsonArrayBuilder add(String value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder add(BigDecimal value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder add(BigInteger value) {
        _delegate.add(_jsonNodeFactory.numberNode(value));
        return this;
    }

    @Override
    public JsonArrayBuilder add(int value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder add(long value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder add(double value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder add(boolean value) {
        _delegate.add(value);
        return this;
    }

    @Override
    public JsonArrayBuilder addNull() {
        _delegate.addNull();
        return this;
    }

    @Override
    public JsonArrayBuilder add(JsonObjectBuilder builder) {
        if (!(builder instanceof JacksonObjectBuilder))
            throw new UnsupportedOperationException("No compatibility with other implementation yet.");
        _delegate.add(((JacksonObjectBuilder) builder).delegate());
        return this;
    }

    @Override
    public JsonArrayBuilder add(JsonArrayBuilder builder) {
        if (!(builder instanceof JacksonArrayBuilder))
            throw new UnsupportedOperationException("No compatibility with other implementation yet.");
        _delegate.add(((JacksonArrayBuilder) builder).delegate());
        return this;
    }

    @Override
    public JsonArray build() {
        return new JacksonArray(_delegate, _nodeFactory);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy