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

com.github.bloodshura.ignitium.cfg.json.handling.JsonComposer Maven / Gradle / Ivy

Go to download

A collection of configuration and serialization readers and writers, like JSON, internationalization (I18n), and CSV.

There is a newer version: 1.0.1
Show newest version
package com.github.bloodshura.ignitium.cfg.json.handling;

import com.github.bloodshura.ignitium.cfg.json.JsonArray;
import com.github.bloodshura.ignitium.cfg.json.JsonObject;
import com.github.bloodshura.ignitium.charset.TextBuilder;
import com.github.bloodshura.ignitium.collection.list.XListIterator;
import com.github.bloodshura.ignitium.collection.tuple.Pair;
import com.github.bloodshura.ignitium.io.stream.TextWriter;
import com.github.bloodshura.ignitium.resource.OutputResource;
import com.github.bloodshura.ignitium.worker.StringWorker;

import javax.annotation.Nonnull;
import java.io.IOException;

public class JsonComposer {
	public static void compose(@Nonnull JsonArray array, @Nonnull OutputResource resource, boolean pretty) throws IOException {
		try (TextWriter writer = new TextWriter(resource)) {
			arrayCompose(array, writer, new TextBuilder(), pretty);
		}
	}

	public static void compose(@Nonnull JsonObject object, @Nonnull OutputResource resource, boolean pretty) throws IOException {
		try (TextWriter writer = new TextWriter(resource)) {
			objectCompose(object, writer, new TextBuilder(), pretty);
		}
	}

	protected static void arrayCompose(@Nonnull JsonArray array, @Nonnull TextWriter writer, @Nonnull TextBuilder prefix, boolean pretty) throws IOException {
		writer.write('[');

		if (pretty) {
			writer.newLine();
			prefix.append('\t');
		}

		XListIterator iterator = array.iterator();

		while (iterator.hasNext()) {
			Object object = iterator.next();

			if (iterator.hasPrevious()) {
				writer.write(',');

				if (pretty) {
					writer.newLine();
				}
			}

			if (pretty) {
				writer.write(prefix);
			}

			writeJsonFormatted(object, writer, prefix, pretty);
		}

		if (pretty) {
			writer.newLine();
			writer.write(prefix.back(1));
		}

		writer.write(']');
	}

	protected static void objectCompose(@Nonnull JsonObject object, @Nonnull TextWriter writer, @Nonnull TextBuilder prefix, boolean pretty) throws IOException {
		writer.write('{');

		if (pretty) {
			writer.newLine();
			prefix.append('\t');
		}

		XListIterator> iterator = object.iterator();

		while (iterator.hasNext()) {
			Pair pair = iterator.next();

			if (iterator.hasPrevious()) {
				writer.write(',');

				if (pretty) {
					writer.newLine();
				}
			}

			if (pretty) {
				writer.write(prefix);
			}

			writer.write('"');
			writer.write(StringWorker.escape(pair.getLeft()));
			writer.write('"');
			writer.write(':');

			if (pretty) {
				writer.write(' ');
			}

			writeJsonFormatted(pair.getRight(), writer, prefix, pretty);
		}

		if (pretty) {
			writer.newLine();
			writer.write(prefix.back(1));
		}

		writer.write('}');
	}

	protected static void writeJsonFormatted(@Nonnull Object value, @Nonnull TextWriter writer, @Nonnull TextBuilder prefix, boolean pretty) throws IOException {
		if (value instanceof String) {
			writer.write('"' + StringWorker.escape((String) value) + '"');
		} else if (value instanceof JsonArray) {
			arrayCompose((JsonArray) value, writer, prefix, pretty);
		} else if (value instanceof JsonObject) {
			objectCompose((JsonObject) value, writer, prefix, pretty);
		} else {
			writer.write(value);
		}
	}
}