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.
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores
* CA 94065 USA or visit www.oracle.com if you need additional information or
* have any questions.
*/
package com.codename1.io;
import com.codename1.processing.Result;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
*
Fast and dirty parser for JSON content on the web, it essentially returns
* a {@link java.util.Map} object containing the object fields mapped to their values. If the value is
* a nested object a nested {@link java.util.Map}/{@link java.util.List} is returned.
*
*
* The {@code JSONParser} returns a {@code Map} which is great if the root object is a {@code Map} but in
* some cases its a list of elements (as is the case above). In this case a special case {@code "root"} element is
* created to contain the actual list of elements. See the sample below for exact usage of this.
*
The sample code below fetches a page of data from the nestoria housing listing API as a list of Map elements.
* You can see instructions on how to display the data in the {@link com.codename1.components.InfiniteScrollAdapter}
* class.
*
*
* @author Shai Almog
*/
public class JSONParser implements JSONParseCallback {
/**
* Checks whether JSONParser instances will use longs to represent numeric values by default. This is just a
* global default setting. You should use {@link #isUseLongsInstance() } to check the status for a particular
* JSONParser object.
*
* @return the useLongsDefault
* @deprecated Use {@link #isUseLongsInstance() } to check whether the current JSONParser uses longs.
*/
public static boolean isUseLongs() {
return useLongsDefault;
}
/**
* Checks to see if this parser generates long objects and not just doubles for numeric values.
* @return
*/
public boolean isUseLongsInstance() {
return useLongs;
}
/**
* Indicates that the parser will generate long objects and not just doubles for numeric values.
*
*
Warning: This method will affect ALL JSONParser instances in the application. Prefer to use {@link #setUseLongsInstance(boolean) }
* to only affect the behaviour of the particular JSONParser instance.
* @param aUseLongsDefault the useLongsDefault to set
* @deprecated Use {@link #setUseLongsInstance(boolean) }
*/
public static void setUseLongs(boolean aUseLongsDefault) {
useLongsDefault = aUseLongsDefault;
}
/**
* Sets the current JSONParser instance to use longs instead of doubles for numeric values. Prefer this to the static {@link #setUseLongs(boolean) }
* so that it doesn't disrupt libraries that may depend on JSONParser.
* @param longs True to use
* @since 7.0
*/
public void setUseLongsInstance(boolean longs) {
useLongs = longs;
}
/**
* Checks the default setting for {@link #isIncludeNullsInstance() }.
* @return the includeNullsDefault The global default setting for {@link #isIncludeNullsInstance() }.
* @deprecated Use {@link #isIncludeNullsInstance() } instead.
*/
public static boolean isIncludeNulls() {
return includeNullsDefault;
}
/**
* Sets the global default settings for {@link #isIncludeNullsInstance() }.
* @param aIncludeNullsDefault the includeNullsDefault to set
* @deprecated Use {@link #setIncludeNullsInstance(boolean) } instead.
*/
public static void setIncludeNulls(boolean aIncludeNullsDefault) {
includeNullsDefault = aIncludeNullsDefault;
}
/**
* Sets whether to include null values in parsed content.
* @param include True to include null values in parsed content.
* @since 7.0
*/
public void setIncludeNullsInstance(boolean include) {
includeNulls = include;
}
/**
* Checks whether this parser will include null values in parsed content.
*
* @return True if null values are included in parsed content.
* @since 7.0
*/
public boolean isIncludeNullsInstance() {
return includeNulls;
}
/**
* Global default setting for {@link #isUseBooleanInstance() }.
* @return the useBooleanDefault
* @deprecated Use {@link #isUseBooleanInstance() } instead.
*/
public static boolean isUseBoolean() {
return useBooleanDefault;
}
/**
* Indicates that the parser will generate Boolean objects and not just Strings for boolean values
* @return True if the parser will generate Boolean objects and not just Strings for boolean values.
* @since 7.0
*/
public boolean isUseBooleanInstance() {
return useBoolean;
}
/**
* Indicates that the parser will generate Boolean objects and not just Strings for boolean values
* @param useBoolean True to generate Boolean objects and not just Strings for boolean values.
* @since 7.0
*/
public void setUseBooleanInstance(boolean useBoolean) {
this.useBoolean = useBoolean;
}
/**
* Sets the global default value for {@link #isUseBooleanInstance() }
* @param aUseBooleanDefault the useBooleanDefault to set
* @deprecated Use {@link #setUseBooleanInstance(boolean) } instead.
*/
public static void setUseBoolean(boolean aUseBooleanDefault) {
useBooleanDefault = aUseBooleanDefault;
}
static class ReaderClass {
char[] buffer;
int buffOffset;
int buffSize = -1;
int read(Reader is) throws IOException {
int c = -1;
if(buffer == null) {
buffer = new char[8192];
}
if(buffSize < 0 || buffOffset >= buffSize) {
buffSize = is.read(buffer, 0, buffer.length);
if(buffSize < 0) {
return -1;
}
buffOffset = 0;
}
c = buffer[buffOffset];
buffOffset ++;
return c;
}
}
private static boolean useLongsDefault;
private boolean useLongs = useLongsDefault;
private static boolean useLongs(JSONParseCallback callback) {
if (callback instanceof JSONParser) {
return ((JSONParser)callback).isUseLongsInstance();
}
return useLongsDefault;
}
/**
* Indicates that the parser will generate Boolean objects and not just Strings for boolean values
*/
private static boolean useBooleanDefault;
private boolean useBoolean = useBooleanDefault;
private static boolean useBoolean(JSONParseCallback callback) {
if (callback instanceof JSONParser) {
return ((JSONParser)callback).isUseBooleanInstance();
}
return useBooleanDefault;
}
private static boolean includeNullsDefault;
private boolean includeNulls = includeNullsDefault;
private boolean modern;
private Map state;
private java.util.List