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

com.javanut.json.encode.JSONObject Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.json.encode;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.javanut.json.JSONType;
import com.javanut.json.encode.function.*;

public abstract class JSONObject {
    private final JSONBuilder builder;
    private boolean declaredEmpty = false;

    JSONObject(JSONBuilder builder) {
        this.builder = builder;
    }

    public P endObject() {
        builder.endObject();
        return objectEnded();
    }

    abstract P objectEnded();

    // Object

    @Deprecated //use startObject
    public JSONObject> beginObject(String name) {
        return startObject(name);
    }

    @Deprecated //use startObject
    public  JSONObject> beginObject(String name, ToMemberFunction accessor) {
        return startObject(name, accessor);
    }
    
    public JSONObject> startObject(String name) {
        return startObject(name, new ToMemberFunction() {
            @Override
            public T get(T o) {
                return o;
            }
        });
    }

    public  JSONObject> startObject(String name, ToMemberFunction accessor) {
        return new JSONObject>(builder.addFieldPrefix(name).beginObject(accessor)) {
            @Override
            JSONObject objectEnded() {
                return JSONObject.this;
            }
        };
    }

    // Array
    
    public  JSONArray, N> array(String name, IteratorFunction iterator) {
        return this.array(name, new ToMemberFunction() {
            @Override
            public T get(T o) {
                return o;
            }
        }, iterator);
    }

    public  JSONArray, N> array(String name, ToMemberFunction accessor, IteratorFunction iterator) {
        return JSONArray.createArray(builder.addFieldPrefix(name), accessor, iterator,  new JSONArray.ArrayCompletion>() {
            @Override
            public JSONObject end() {
                return JSONObject.this;
            }
        });
    }

    public , N> JSONArray, M> listArray(String name, ToMemberFunction accessor) {
        return JSONArray.createListArray(builder.addFieldPrefix(name), accessor, new JSONArray.ArrayCompletion>() {
            @Override
            public JSONObject end() {
                return JSONObject.this;
            }
        });
    }

    public  JSONArray, N[]> basicArray(String name, ToMemberFunction accessor) {
        return JSONArray.createBasicArray(builder.addFieldPrefix(name), accessor, new JSONArray.ArrayCompletion>() {
            @Override
            public JSONObject end() {
                return JSONObject.this;
            }
        });
    }

    public , N> JSONArray, JSONObject, Iterator> iterArray(String name, ToMemberFunction accessor) {
        return JSONArray.createCollectionArray(builder.addFieldPrefix(name), accessor, new JSONArray.ArrayCompletion>() {
            @Override
            public JSONObject end() {
                return JSONObject.this;
            }
        });
    }

    // Renderer

    public  JSONObject renderer(String name, JSONRenderer renderer, ToMemberFunction accessor) {
        builder.addFieldPrefix(name).addBuilder(renderer.builder, accessor);
        return this;
    }

    public JSONObject recurseRoot(String name, ToMemberFunction accessor) {
        builder.addFieldPrefix(name).recurseRoot(accessor);
        return this;
    }

    public JSONSelect> beginSelect(String name) {
        return new JSONSelect>(builder.addFieldPrefix(name).beginSelect()) {
            @Override
            JSONObject selectEnded() {
                return JSONObject.this;
            }
        };
    }

    // Null

    public JSONObject constantNull(String name) {
        builder.addFieldPrefix(name).addNull();
        return this;
    }

    // Allow for documented empty
    public JSONObject empty() {
        //assert(objectElementIndex == -1) : "empty can only be called on empty object";
        declaredEmpty = true;
        return this;
    }

    // Boolean

    public JSONObject bool(String name, ToBoolFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addBool(null, func);
        return this;
    }

    public JSONObject bool(String name, ToBoolFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addBool(null, func, encode);
        return this;
    }

    public JSONObject nullableBool(String name, ToBoolFunction isNull, ToBoolFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addBool(isNull, func);
        return this;
    }

    public JSONObject nullableBool(String name, ToBoolFunction isNull, ToBoolFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addBool(isNull, func, encode);
        return this;
    }

    // Integer

    public JSONObject integer(String name, ToLongFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addInteger(null, func);
        return this;
    }

    public JSONObject integer(String name, ToLongFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addInteger(null, func, encode);
        return this;
    }

    public JSONObject nullableInteger(String name, ToBoolFunction isNull, ToLongFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addInteger(isNull, func);
        return this;
    }

    public JSONObject nullableInteger(String name, ToBoolFunction isNull, ToLongFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addInteger(isNull, func, encode);
        return this;
    }

    // Decimal

    public JSONObject decimal(String name, int precision, ToDoubleFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addDecimal(precision, null, func);
        return this;
    }

    public JSONObject decimal(String name, int precision, ToDoubleFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addDecimal(precision, null, func, encode);
        return this;
    }

    public JSONObject nullableDecimal(String name, int precision, ToBoolFunction isNull, ToDoubleFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addDecimal(precision, isNull, func);
        return this;
    }

    public JSONObject nullableDecimal(String name, int precision, ToBoolFunction isNull, ToDoubleFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addDecimal(precision, isNull, func, encode);
        return this;
    }

    // String

    public JSONObject string(String name, ToStringFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addString(false, func);
        return this;
    }
    
    public JSONObject string(byte[] utf8EncodedName, ToStringFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(utf8EncodedName).addString(false, func);
        return this;
    }

    public JSONObject string(String name, ToStringFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addString(false, func, encode);
        return this;
    }

    public JSONObject nullableString(String name, ToStringFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addString(true, func);
        return this;
    }

    public JSONObject nullableString(String name, ToStringFunction func, JSONType encode) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addString(true, func, encode);
        return this;
    }

    // Enum

    public > JSONObject enumName(String name, ToEnumFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addEnumName(func);
        return this;
    }

    public > JSONObject enumOrdinal(String name, ToEnumFunction func) {
        assert(!declaredEmpty);
        builder.addFieldPrefix(name).addEnumOrdinal(func);
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy