<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.yuanheng.cookjson;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.json.spi.JsonProvider;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParserFactory;

import org.apache.commons.cli.*;

/**
 * An extremely simple utility that dumps a BSON file to JSON file.
 *
 * @author	<NAME>
 */
public class ConvertJson
{
	private static void usage (Options options)
	{
		PrintWriter pw = new PrintWriter (System.out);
		HelpFormatter formatter = new HelpFormatter ();
		formatter.printHelp (pw, 78, "cookjson-utils.jar [options]", null, options, 2, HelpFormatter.DEFAULT_DESC_PAD, null);
		pw.flush ();
	}

	public static void main (String[] args)
	{
		Options options = new Options ();

		options.addOption ("f", "from", true, "from file");
		options.addOption ("t", "to", true, "to file");
		options.addOption ("p", "pretty", false, "pretty output for text format.");
		options.addOption ("d", "double", false, "use double for BSON to store BigDecimal / BigInteger.");
		options.addOption ("h", "help", false, "print this message.");

		if (args.length == 0)
		{
			usage (options);
			System.exit (1);
		}

		String src = null;
		String dst = null;
		boolean pretty = false;
		boolean useDouble = false;

		CommandLine cmdLine = null;
		try
		{
			cmdLine = new DefaultParser ().parse (options, args);
		}
		catch (ParseException ex)
		{
			System.out.println (ex.getMessage ());
			System.exit (1);
		}

		for (Option opt : cmdLine.getOptions ())
		{
			switch (opt.getOpt ().charAt (0))
			{
				case 'h':
					usage (options);
					System.exit (0);
					break;
				case 'd':
					useDouble = true;
					break;
				case 'p':
					pretty = true;
					break;
				case 'f':
					src = opt.getValue ();
					break;
				case 't':
					dst = opt.getValue ();
					break;
			}
		}

		if (src == null)
		{
			System.out.println ("missing source file.");
			System.exit (1);
		}

		if (dst == null)
		{
			System.out.println ("missing target file.");
			System.exit (1);
		}

		boolean srcBson = false;
		boolean dstBson = false;

		if (src.endsWith (".bson"))
			srcBson = true;
		if (dst.endsWith (".bson"))
			dstBson = true;

		try
		{
			FileInputStream is = new FileInputStream (src);
			JsonParser p;
			JsonProvider provider = JsonProvider.provider ();
			HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
			bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
			if (useDouble)
			{
				bsonConfig.put (CookJsonProvider.USE_DOUBLE, Boolean.TRUE);
			}

			HashMap<String, Object> textConfig = new HashMap<String, Object> ();
			if (pretty)
			{
				textConfig.put (JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
			}
<commit_msg>Add an option to treat the root as array for bson.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.yuanheng.cookjson;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.json.spi.JsonProvider;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParserFactory;

import org.apache.commons.cli.*;

/**
 * An extremely simple utility that dumps a BSON file to JSON file.
 *
 * @author	<NAME>
 */
public class ConvertJson
{
	private static void usage (Options options)
	{
		PrintWriter pw = new PrintWriter (System.out);
		HelpFormatter formatter = new HelpFormatter ();
		formatter.printHelp (pw, 78, "cookjson-utils.jar [options]", null, options, 2, HelpFormatter.DEFAULT_DESC_PAD, null);
		pw.flush ();
	}

	public static void main (String[] args)
	{
		Options options = new Options ();

		options.addOption ("f", "from", true, "from file");
		options.addOption ("t", "to", true, "to file");
		options.addOption ("p", "pretty", false, "pretty output for text format.");
		options.addOption ("d", "double", false, "use double for BSON to store BigDecimal / BigInteger.");
		options.addOption ("a", "array", false, "treat BSON root document as array.");
		options.addOption ("h", "help", false, "print this message.");

		if (args.length == 0)
		{
			usage (options);
			System.exit (1);
		}

		String src = null;
		String dst = null;
		boolean pretty = false;
		boolean useDouble = false;
		boolean rootAsArray = false;

		CommandLine cmdLine = null;
		try
		{
			cmdLine = new DefaultParser ().parse (options, args);
		}
		catch (ParseException ex)
		{
			System.out.println (ex.getMessage ());
			System.exit (1);
		}

		for (Option opt : cmdLine.getOptions ())
		{
			switch (opt.getOpt ().charAt (0))
			{
				case 'a':
					rootAsArray = true;
					break;
				case 'h':
					usage (options);
					System.exit (0);
					break;
				case 'd':
					useDouble = true;
					break;
				case 'p':
					pretty = true;
					break;
				case 'f':
					src = opt.getValue ();
					break;
				case 't':
					dst = opt.getValue ();
					break;
			}
		}

		if (src == null)
		{
			System.out.println ("missing source file.");
			System.exit (1);
		}

		if (dst == null)
		{
			System.out.println ("missing target file.");
			System.exit (1);
		}

		boolean srcBson = false;
		boolean dstBson = false;

		if (src.endsWith (".bson"))
			srcBson = true;
		if (dst.endsWith (".bson"))
			dstBson = true;

		try
		{
			JsonProvider provider = JsonProvider.provider ();

			HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
			bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
			if (useDouble)
			{
				bsonConfig.put (CookJsonProvider.USE_DOUBLE, Boolean.TRUE);
			}
			if (rootAsArray)
			{
				bsonConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);
			}

			HashMap<String, Object> textConfig = new HashMap<String, Object> ();
			if (pretty)
			{
				textConfig.put (JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
			}

			FileInputStream is = new FileInputStream (src);
			JsonParser p;
{'fraction_non_alphanumeric': 0.12083333333333333, 'fraction_numerical': 0.0036458333333333334, 'mean_word_length': 3.3891428571428572, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6440377', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2332, 'n_words': 771}