<commit_before>package com.localmatters.serializer.writer;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import com.localmatters.serializer.SerializationContext;
import com.localmatters.serializer.SerializationException;
import com.localmatters.serializer.serialization.DelegatingSerialization;
import com.localmatters.serializer.serialization.NameSerialization;
import com.localmatters.serializer.serialization.Serialization;
import com.localmatters.serializer.serialization.ValueSerialization;
import com.localmatters.serializer.util.ReflectionUtils;
import com.localmatters.util.CollectionUtils;
import com.localmatters.util.StringUtils;

/**
 * This class defines a serialization writer that outputs JSON.
 */
public class JSONWriter extends AbstractWriter {
	private static final byte[] NULL = "null".getBytes();
	private static final byte[] LEFT_CURLY = "{".getBytes();
	private static final byte[] RIGHT_CURLY = "}".getBytes();
	private static final byte[] LEFT_SQUARE = "[".getBytes();
	private static final byte[] RIGHT_SQUARE = "]".getBytes();
	private static final byte[] QUOTE = "\"".getBytes();
	private static final byte[] QUOTE_COLUMN = "\": ".getBytes();
	private static final byte[] QUOTE_COLUMN_LEFT_CURLY = "\": {".getBytes();
	private static final byte[] QUOTE_COLUMN_CLOSED_CURLY = "\": {}".getBytes();
	private static final byte[] QUOTE_COLUMN_LEFT_SQUARE = "\": [".getBytes();
	private static final byte[] QUOTE_COLUMN_CLOSED_SQUARE = "\": []".getBytes();
	private static final byte[] COMMA = ", ".getBytes();
	private static final String INDENTATION = "   ";
	private Map<Integer, String> prefixes = new HashMap<Integer, String>();

	/**
	 * @see com.localmatters.serializer.writer.Writer#writeRoot(com.localmatters.serializer.serialization.Serialization, java.lang.Object, com.localmatters.serializer.SerializationContext)
	 */
	public void writeRoot(Serialization ser, 
			Object root, 
			SerializationContext ctx) throws SerializationException {
		Serialization delegate = ser;
		while(delegate instanceof DelegatingSerialization) {
			delegate = ((DelegatingSerialization) ser).getDelegate();
		}
		if (delegate instanceof ValueSerialization) {
			write(ctx, LEFT_CURLY);
			ser.serialize(ser, null, root, ctx);
			write(ctx, getPrefix(ctx)).write(ctx, RIGHT_CURLY);
		} else if (ser instanceof NameSerialization) {
			delegate = ((NameSerialization) ser).getDelegate();
			delegate.serialize(delegate, null, root, ctx);
		} else {
			ser.serialize(ser, null, root, ctx);
			
		}
		
	}

	/**
	 * @see com.localmatters.serializer.writer.Writer#writeValue(com.localmatters.serializer.serialization.Serialization, java.lang.String, java.lang.Object, com.localmatters.serializer.SerializationContext)
	 */
	public void writeValue(Serialization ser, 
			String name,
			Object value, 
			SerializationContext ctx) throws SerializationException {
		ctx.nextLevel(StringUtils.defaultIfEmpty(name, "value"));

		String prefix = getPrefix(ctx);
		if (value != null) {
			write(ctx, prefix);
			if (StringUtils.isNotBlank(name)) {
				write(ctx, QUOTE).write(ctx, name).write(ctx, QUOTE_COLUMN);
			}<commit_msg>add support for list in map and improve JSON output<commit_after>package com.localmatters.serializer.writer;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import com.localmatters.serializer.SerializationContext;
import com.localmatters.serializer.SerializationException;
import com.localmatters.serializer.serialization.DelegatingSerialization;
import com.localmatters.serializer.serialization.NameSerialization;
import com.localmatters.serializer.serialization.Serialization;
import com.localmatters.serializer.serialization.ValueSerialization;
import com.localmatters.serializer.util.ReflectionUtils;
import com.localmatters.util.CollectionUtils;
import com.localmatters.util.StringUtils;

/**
 * This class defines a serialization writer that outputs JSON.
 */
public class JSONWriter extends AbstractWriter {
	private static final byte[] NULL = "null".getBytes();
	private static final byte[] LEFT_CURLY = "{".getBytes();
	private static final byte[] RIGHT_CURLY = "}".getBytes();
	private static final byte[] LEFT_SQUARE = "[".getBytes();
	private static final byte[] RIGHT_SQUARE = "]".getBytes();
	private static final byte[] QUOTE = "\"".getBytes();
	private static final byte[] QUOTE_COLUMN = "\": ".getBytes();
	private static final byte[] QUOTE_COLUMN_LEFT_CURLY = "\": {".getBytes();
	private static final byte[] QUOTE_COLUMN_CLOSED_CURLY = "\": {}".getBytes();
	private static final byte[] QUOTE_COLUMN_LEFT_SQUARE = "\": [".getBytes();
	private static final byte[] QUOTE_COLUMN_CLOSED_SQUARE = "\": []".getBytes();
	private static final byte[] COMMA = ", ".getBytes();
	private static final String INDENTATION = "   ";
	private Map<Integer, String> prefixes = new HashMap<Integer, String>();

	/**
	 * @see com.localmatters.serializer.writer.Writer#writeRoot(com.localmatters.serializer.serialization.Serialization, java.lang.Object, com.localmatters.serializer.SerializationContext)
	 */
	public void writeRoot(Serialization ser, 
			Object root, 
			SerializationContext ctx) throws SerializationException {
		Serialization delegate = ser;
		while(delegate instanceof DelegatingSerialization) {
			delegate = ((DelegatingSerialization) delegate).getDelegate();
		}
		if (delegate instanceof ValueSerialization) {
			write(ctx, LEFT_CURLY);
			ser.serialize(ser, null, root, ctx);
			write(ctx, getPrefix(ctx)).write(ctx, RIGHT_CURLY);
		} else if (ser instanceof NameSerialization) {
			delegate = ((NameSerialization) ser).getDelegate();
			delegate.serialize(delegate, null, root, ctx);
		} else {
			ser.serialize(ser, null, root, ctx);
			
		}
		
	}

	/**
	 * @see com.localmatters.serializer.writer.Writer#writeValue(com.localmatters.serializer.serialization.Serialization, java.lang.String, java.lang.Object, com.localmatters.serializer.SerializationContext)
	 */
	public void writeValue(Serialization ser, 
			String name,
			Object value, 
			SerializationContext ctx) throws SerializationException {
		ctx.nextLevel(StringUtils.defaultIfEmpty(name, "value"));

		String prefix = getPrefix(ctx);
		if (value != null) {
			write(ctx, prefix);
			if (StringUtils.isNotBlank(name)) {
				write(ctx, QUOTE).write(ctx, name).write(ctx, QUOTE_COLUMN);
			}
{'fraction_non_alphanumeric': 0.12226137582350237, 'fraction_numerical': 0.0, 'mean_word_length': 5.554216867469879, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2768821', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1952, 'n_words': 437}