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

net.minidev.json.parser.JSONParser Maven / Gradle / Ivy

Go to download

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

There is a newer version: 2.5.1
Show newest version
package net.minidev.json.parser;

/*
 *    Copyright 2011 JSON-SMART authors
 *
 * Licensed 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.
 */
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

public class JSONParser {
	/**
	 * allow simple quote as String quoting char
	 */
	public final static int ACCEPT_SIMPLE_QUOTE = 1;
	/**
	 * allow non quoted test
	 */
	public final static int ACCEPT_NON_QUOTE = 2;
	/**
	 * Parse NaN as Float.NaN
	 */
	public final static int ACCEPT_NAN = 4;
	/**
	 * Ignore control char in input text.
	 */
	public final static int IGNORE_CONTROL_CHAR = 8;
	/**
	 * Use int datatype to store number when it's possible.
	 * 
	 * @since 1.0.7
	 */
	public final static int USE_INTEGER_STORAGE = 16;
	/**
	 * Throws exception on excessive 0 leading in digits
	 * 
	 * @since 1.0.7
	 */
	public final static int ACCEPT_LEADING_ZERO = 32;
	/**
	 * Throws exception on useless comma in object and array
	 * 
	 * @since 1.0.8
	 */
	public final static int ACCEPT_USELESS_COMMA = 64;
	/**
	 * Allow Json-smart to use Double or BigDecimal to store floating point
	 * value
	 * 
	 * You may need to disable HI_PRECISION_FLOAT feature on 32bit to improve
	 * parsing performances.
	 * 
	 * @since 1.0.9
	 */
	public final static int USE_HI_PRECISION_FLOAT = 128;
	/**
	 * If enabled json-smart will throws exception if datas are present after
	 * the end of the Json data.
	 * 
	 * @since 1.0.9-2
	 */
	public final static int ACCEPT_TAILLING_DATA = 256;
	/**
	 * smart mode, fastest parsing mode. accept lots of non standard json syntax
	 * 
	 * @since 1.3.1
	 */
	public final static int ACCEPT_TAILLING_SPACE = 512;
	/**
	 * smart mode, fastest parsing mode. accept lots of non standard json syntax
	 * 
	 * @since 1.0.6
	 */
	public final static int MODE_PERMISSIVE = -1;
	/**
	 * strict RFC4627 mode.
	 * 
	 * slower than PERMISIF MODE.
	 * 
	 * @since 1.0.6
	 */
	public final static int MODE_RFC4627 = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_DATA;
	/**
	 * Parse Object like json-simple
	 * 
	 * Best for an iso-bug json-simple API port.
	 * 
	 * @since 1.0.7
	 */
	public final static int MODE_JSON_SIMPLE = ACCEPT_USELESS_COMMA | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_DATA | ACCEPT_TAILLING_SPACE;
	/**
	 * Strictest parsing mode
	 * 
	 * @since 1.0.9-2
	 */
	public final static int MODE_STRICTEST = USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT;
	/**
	 * Default json-smart processing mode
	 */
	public static int DEFAULT_PERMISSIVE_MODE = (System.getProperty("JSON_SMART_SIMPLE") != null) ? MODE_JSON_SIMPLE
			: MODE_PERMISSIVE;

	/*
	 * internal fields
	 */
	private int mode;
	private JSONParserReader pStream;
	private JSONParserInputStream pSBintream;
	private JSONParserString pString;
	private JSONParserByteArray pBytes;

	/**
	 * @deprecated prefer usage of new JSONParser(JSONParser.MODE_*)
	 */
	public JSONParser() {
		this.mode = DEFAULT_PERMISSIVE_MODE;
	}

	public JSONParser(int permissifMode) {
		this.mode = permissifMode;
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(String in) throws ParseException {
		if (pString == null)
			pString = new JSONParserString(mode);
		return pString.parse(in);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(String in, ContainerFactory containerFactory) throws ParseException {
		if (pString == null)
			pString = new JSONParserString(mode);
		return pString.parse(in, containerFactory);
	}

	public Object parse(String in, ContainerFactory containerFactory, ContentHandler handler) throws ParseException {
		if (pString == null)
			pString = new JSONParserString(mode);
		return pString.parse(in, containerFactory, handler);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(byte[] in) throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(byte[] in, ContainerFactory containerFactory) throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in, containerFactory);
	}

	public Object parse(byte[] in, ContainerFactory containerFactory, ContentHandler handler) throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in, containerFactory, handler);
	}

	public Object parse(byte[] in, int offset, int length) throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in, offset, length, ContainerFactory.FACTORY_SIMPLE, ContentHandlerDumy.HANDLER);
	}

	public Object parse(byte[] in, int offset, int length, ContainerFactory containerFactory) throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in, offset, length, containerFactory, ContentHandlerDumy.HANDLER);
	}
	
	public Object parse(byte[] in, int offset, int length, ContainerFactory containerFactory, ContentHandler handler)
			throws ParseException {
		if (pBytes == null)
			pBytes = new JSONParserByteArray(mode);
		return pBytes.parse(in, offset, length, containerFactory, handler);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(Reader in) throws ParseException {
		if (pStream == null)
			pStream = new JSONParserReader(mode);
		return pStream.parse(in);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(Reader in, ContainerFactory containerFactory) throws ParseException {
		if (pStream == null)
			pStream = new JSONParserReader(mode);
		return pStream.parse(in, containerFactory);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(Reader in, ContainerFactory containerFactory, ContentHandler handler) throws ParseException {
		if (pStream == null)
			pStream = new JSONParserReader(mode);
		return pStream.parse(in, containerFactory, handler);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(InputStream in) throws ParseException, UnsupportedEncodingException  {
		if (pSBintream == null)
			pSBintream = new JSONParserInputStream(mode);
		return pSBintream.parse(in);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException, UnsupportedEncodingException  {
		if (pSBintream == null)
			pSBintream = new JSONParserInputStream(mode);
		return pSBintream.parse(in, containerFactory);
	}

	/**
	 * use to return Primitive Type, or String, Or JsonObject or JsonArray
	 * generated by a ContainerFactory
	 */
	public Object parse(InputStream in, ContainerFactory containerFactory, ContentHandler handler)
			throws ParseException, UnsupportedEncodingException  {
		if (pSBintream == null)
			pSBintream = new JSONParserInputStream(mode);
		return pSBintream.parse(in, containerFactory, handler);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy