com.javanut.json.encode.JSONRoot Maven / Gradle / Ivy
Show all versions of pronghorn-pipes Show documentation
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 JSONRoot {
protected final JSONBuilder builder;
JSONRoot(JSONBuilder builder) {
this.builder = builder;
builder.start();
}
private class StartObjectJSON extends JSONObject {
StartObjectJSON(JSONBuilder builder) {
super(builder);
}
@Override
P objectEnded() {
return (P) childCompleted();
}
}
private ToMemberFunction passThoughMember =
new ToMemberFunction() {
@Override
public T get(T o) {
return o;
}
};
abstract P rootEnded();
private P childCompleted() {
builder.complete();
return rootEnded();
}
// Object
@Deprecated //use startObject
public JSONObject beginObject() {
return startObject();
}
@Deprecated //use startObject
public JSONObject beginObject(ToMemberFunction accessor) {
return startObject(accessor);
}
public JSONObject startObject() {
return startObject(passThoughMember);
}
public JSONObject startObject(ToMemberFunction accessor) {
return new StartObjectJSON(builder.beginObject(accessor));
}
// Array
public JSONArray array(IteratorFunction iterator) {
return this.array(new ToMemberFunction() {
@Override
public T get(T o) {
return o;
}
}, iterator);
}
public JSONArray array(ToMemberFunction accessor, IteratorFunction iterator) {
return JSONArray.createArray(builder, accessor, iterator, new JSONArray.ArrayCompletion() {
@Override
public P end() {
return childCompleted();
}
});
}
public , N> JSONArray listArray(ToMemberFunction accessor) {
return JSONArray.createListArray(builder, accessor, new JSONArray.ArrayCompletion() {
@Override
public P end() {
return childCompleted();
}
});
}
public JSONArray basicArray(ToMemberFunction accessor) {
return JSONArray.createBasicArray(builder, accessor, new JSONArray.ArrayCompletion() {
@Override
public P end() {
return childCompleted();
}
});
}
public , N> JSONArray, P, Iterator> iterArray(ToMemberFunction accessor) {
return JSONArray.createCollectionArray(builder, accessor, new JSONArray.ArrayCompletion() {
@Override
public P end() {
return childCompleted();
}
});
}
// Renderer
public P renderer(JSONRenderer renderer, ToMemberFunction accessor) {
builder.addBuilder(renderer.builder, accessor);
return this.childCompleted();
}
public P recurseRoot(ToMemberFunction accessor) {
builder.recurseRoot(accessor);
return this.childCompleted();
}
public JSONSelect beginSelect() {
return new JSONSelect(builder.beginSelect()) {
@Override
P selectEnded() {
return childCompleted();
}
};
}
// Null
public P empty() {
return this.childCompleted();
}
public P constantNull() {
builder.addNull();
return this.childCompleted();
}
// Bool
public P bool(ToBoolFunction func) {
builder.addBool(null, func);
return this.childCompleted();
}
public P bool(ToBoolFunction func, JSONType encode) {
builder.addBool(null, func, encode);
return this.childCompleted();
}
public P nullableBool(ToBoolFunction isNull, ToBoolFunction func) {
builder.addBool(isNull, func);
return this.childCompleted();
}
public P nullableBool(ToBoolFunction isNull, ToBoolFunction func, JSONType encode) {
builder.addBool(isNull, func, encode);
return this.childCompleted();
}
// Integer
public P integer(ToLongFunction func) {
builder.addInteger(null, func);
return this.childCompleted();
}
public P integer(ToLongFunction func, JSONType encode) {
builder.addInteger(null, func, encode);
return this.childCompleted();
}
public P nullableInteger(ToBoolFunction isNull, ToLongFunction func) {
builder.addInteger(isNull, func);
return this.childCompleted();
}
public P nullableInteger(ToBoolFunction isNull, ToLongFunction func, JSONType encode) {
builder.addInteger(isNull, func, encode);
return this.childCompleted();
}
// Decimal
public P decimal(int precision, ToDoubleFunction func) {
builder.addDecimal(precision, null, func);
return this.childCompleted();
}
public P decimal(int precision, ToDoubleFunction func, JSONType encode) {
builder.addDecimal(precision, null, func, encode);
return this.childCompleted();
}
public P nullableDecimal(int precision, ToBoolFunction isNull, ToDoubleFunction func) {
builder.addDecimal(precision, isNull, func);
return this.childCompleted();
}
public P nullableDecimal(int precision, ToBoolFunction isNull, ToDoubleFunction func, JSONType encode) {
builder.addDecimal(precision, isNull, func, encode);
return this.childCompleted();
}
// String
public P string(ToStringFunction func) {
builder.addString(false, func);
return this.childCompleted();
}
public P string(ToStringFunction func, JSONType encode) {
builder.addString(false, func, encode);
return this.childCompleted();
}
public P nullableString(final ToStringFunction func) {
builder.addString(true, func);
return this.childCompleted();
}
public P nullableString(ToStringFunction func, JSONType encode) {
builder.addString(true, func, encode);
return this.childCompleted();
}
// Enum
public > P enumName(ToEnumFunction func) {
builder.addEnumName(func);
return this.childCompleted();
}
public > P enumOrdinal(ToEnumFunction func) {
builder.addEnumOrdinal(func);
return this.childCompleted();
}
}