com.javanut.json.encode.JSONRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pronghorn-pipes Show documentation
Show all versions of pronghorn-pipes Show documentation
Ring buffer based queuing utility for applications that require high performance and/or a small
footprint. Well suited for embedded and stream based processing.
package com.javanut.json.encode;
import com.javanut.pronghorn.pipe.ChannelWriter;
import com.javanut.pronghorn.util.AppendableByteWriter;
public class JSONRenderer extends JSONRoot> {
private boolean locked = false;
public JSONRenderer() {
super(new JSONBuilder());
}
public JSONRenderer(JSONKeywords keywords) {
super(new JSONBuilder(keywords));
}
public boolean isLocked() {
return locked;
}
public void render(AppendableByteWriter> writer, T source) {
assert(locked) : "JSONRenderers can only be rendered once locked";
builder.render(writer, source);
}
@Override
JSONRenderer rootEnded() {
locked = true;
return this;
}
//render and prefix the field with a short length;
public void renderWithLengthPrefix(T source, ChannelWriter target) {
int startPos = target.absolutePosition();
target.writeShort(-1);//hold these 2 for later
int startTextPos = target.absolutePosition();
render(target, source);
int stopTextPos = target.absolutePosition();
int length = stopTextPos-startTextPos;
if (length>Short.MAX_VALUE) {
throw new ArrayIndexOutOfBoundsException();
}
target.absolutePosition(startPos);
target.writeShort(length); //set the actual length now that we know.
target.absolutePosition(stopTextPos);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy