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

com.javanut.json.encode.JSONRenderer 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 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