Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is part of the SimpleJSON Java library.
*
* It is based on Clifton Labs' version
* (https://github.com/cliftonlabs/json-simple/), which is a fork of
* the original by Yidong Fang (https://github.com/fangyidong/json-simple/).
* Other authors contributions remain the copyright of the respective
* owner, with the major ones of this derivative listed below.
*
* Copyright 2008-2009,2012-2014,2021 Yidong Fang
* Copyright 2008-2009,2012-2014,2016-2021 Clifton Labs
* Copyright 2021 BSPF Systems, LLC
*
* 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.
*/
package org.bspfsystems.simplejson.parser;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import org.bspfsystems.simplejson.JSONArray;
import org.bspfsystems.simplejson.JSONObject;
import org.bspfsystems.simplejson.JSONSerializable;
import org.bspfsystems.simplejson.SimpleJSONObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Provides JSON utilities for serializing and deserializing various objects.
*/
public final class JSONParser {
/**
* The possible states of the {@link JSONParser}.
*/
private enum ParserState {
/**
* Pre-parsing.
*/
INITIAL,
/**
* Actively parsing (the key of) a {@link JSONObject}.
*/
PARSING_OBJECT,
/**
* Actively parsing a {@link List}.
*/
PARSING_LIST,
/**
* Parsing a key-value pair inside a {@link JSONObject}.
*/
PARSING_ENTRY,
/**
* Error encountered while parsing, a {@link JSONException} should be
* thrown.
*/
PARSED_ERROR,
/**
* Post-parsing.
*/
DONE;
}
/**
* Constructs a new {@link JSONParser}.
*/
private JSONParser() {
// Utility class, do not allow instances to be created.
}
/**
* Deserializes the given {@link String} data into an {@link Object}.
*
* @param data The JSON data in {@link String} format.
* @return An {@link Object} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@NotNull
public static Object deserialize(@NotNull final String data) throws JSONException {
return JSONParser.deserialize(new StringReader(data));
}
/**
* Deserializes the data in the given {@link Reader} into an {@link Object}.
*
* @param reader The {@link Reader} containing the JSON data.
* @return An {@link Object} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@NotNull
public static Object deserialize(@NotNull final Reader reader) throws JSONException {
return JSONParser.performDeserialization(reader);
}
/**
* Deserializes the given {@link String} data into a {@link JSONArray}.
*
* @param data The JSON data in {@link String} format.
* @return A {@link JSONArray} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@Nullable
public static JSONArray deserializeArray(@NotNull final String data) throws JSONException {
return JSONParser.deserializeArray(new StringReader(data));
}
/**
* Deserializes the data in the given {@link Reader} into a
* {@link JSONArray}.
*
* @param reader The {@link Reader} containing the JSON data.
* @return A {@link JSONArray} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@Nullable
public static JSONArray deserializeArray(@NotNull final Reader reader) throws JSONException {
final Object value = JSONParser.deserialize(reader);
return value instanceof JSONArray ? (JSONArray) value : null;
}
/**
* Deserializes the given {@link String} data into a {@link JSONObject}.
*
* @param data The JSON data in {@link String} format.
* @return A {@link JSONObject} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@Nullable
public static JSONObject deserializeObject(@NotNull final String data) throws JSONException {
return JSONParser.deserializeObject(new StringReader(data));
}
/**
* Deserializes the data in the given {@link Reader} into a
* {@link JSONObject}.
*
* @param reader The {@link Reader} containing the JSON data.
* @return A {@link JSONObject} containing the deserialized JSON data.
* @throws JSONException If an error occurs while deserializing the data.
*/
@Nullable
public static JSONObject deserializeObject(@NotNull final Reader reader) throws JSONException {
final Object value = JSONParser.deserialize(reader);
return value instanceof JSONObject ? (JSONObject) value : null;
}
/**
* Deserializes the data in the given {@link Reader} according to the RFC
* 7159 JSON specification.
*
* @param reader The {@link Reader} with the data to deserialize.
* @return An {@link Object} containing the parsed data.
* @throws JSONException If an unexpected token is encountered in the data.
*/
@NotNull
private static Object performDeserialization(@NotNull final Reader reader) throws JSONException {
final JSONReader jsonReader = new JSONReader(reader);
JSONToken jsonToken;
JSONParser.ParserState currentState;
int returnCount = 1;
final Stack