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

com.javanut.pronghorn.util.parse.JSONVisitorCapture 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.pronghorn.util.parse;

import java.io.IOException;

import com.javanut.pronghorn.util.Appendables;
import com.javanut.pronghorn.util.ByteConsumer;

public class JSONVisitorCapture implements JSONVisitor {

	final A target;
	
	final StringBuilder builder = new StringBuilder();
	
	final ByteConsumer con = new ByteConsumer() {

		@Override
		public void consume(byte[] backing, int pos, int len, int mask) {
			Appendables.appendUTF8(builder, backing, pos, len, mask);	
		}

		@Override
		public void consume(byte value) {
			builder.append((char)value);
		}				
	};
	
	public JSONVisitorCapture(A target) {
		this.target = target;
	}

	@Override
	public ByteConsumer stringValue() {
		builder.setLength(0);
		return con;
	}

	@Override
	public void stringValueComplete() {
		try {
			target.append(builder);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public ByteConsumer stringName(int fieldIndex) {
		builder.setLength(0);
		
		if (fieldIndex>0) {
			builder.append(',');
		}
		
		return con;
	}

	@Override
	public void stringNameComplete() {
		try {
			target.append(builder).append(':');
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void arrayBegin() {
		try {
			target.append('[');
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void arrayEnd() {
		try {
			target.append(']');
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void arrayIndexBegin(int instance) {
		try {
			if (instance>0) {
				target.append(',');
			}
			
			//target.append("idx: ");
			//Appendables.appendValue(target, instance);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void numberValue(long m, byte e) {
		Appendables.appendDecimalValue(target, m, e);
	}

	@Override
	public void nullValue() {
		try {
			target.append("null");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void booleanValue(boolean b) {
		System.err.println("boolean "+b);
		
		try {
			target.append("boolean ");
			target.append(Boolean.toString(b));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public void objectEnd() {
		try {
			target.append('}');
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void objectBegin() {
		try {
			target.append('{');
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy