function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sam_Adminsam_Admin 

What does JSON object mean?

Hello,

     I have this class in my org and we are in process of cleaning up some stuff but i have no idea what exactly this code does, it's not related to any object, do any one have any idea on this? if i delete this does it cause any problem to my org? couldn't post the whole code bcz it's not letting me to exceed more than 20000 chrs

 


/*
* A JSONObject is an unordered collection of name/value pairs. Its
* external form is a string wrapped in curly braces with colons between the
* names and values, and commas between the values and names. The internal form
* is an object having <code>get</code> and <code>opt</code> methods for
* accessing the values by name, and <code>put</code> methods for adding or
* replacing values by name. The values can be any of these types:
* <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
* <code>Number</code>, <code>String</code>, or the <code>JSONObject.NULL</code>
* object. A JSONObject constructor can be used to convert an external form
* JSON text into an internal form whose values can be retrieved with the
* <code>get</code> and <code>opt</code> methods, or to convert values into a
* JSON text using the <code>put</code> and <code>toString</code> methods.
* A <code>get</code> method returns a value if one can be found, and throws an
* exception if one cannot be found. An <code>opt</code> method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
* <p>
* The generic <code>get()</code> and <code>opt()</code> methods return an
* object, which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and type
* coersion for you.
* <p>
* The <code>put</code> methods adds values to an object. For example, <pre>
* myString = new JSONObject().put('JSON', "Hello, World!").toString();</pre>
* produces the string <code>{"JSON": "Hello, World"}</code>.
* <p>
* The texts produced by the <code>toString</code> methods strictly conform to
* the JSON sysntax rules.
* The constructors are more forgiving in the texts they will accept:
* <ul>
* <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
* before the closing brace.</li>
* <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
* quote)</small>.</li>
* <li>Strings do not need to be quoted at all if they do not begin with a quote
* or single quote, and if they do not contain leading or trailing spaces,
* and if they do not contain any of these characters:
* <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
* and if they are not the reserved words <code>true</code>,
* <code>false</code>, or <code>null</code>.</li>
* <li>Keys can be followed by <code>=</code> or <code>=></code> as well as
* by <code>:</code>.</li>
* <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as
* well as by <code>,</code> <small>(comma)</small>.</li>
* <li>Numbers may have the <code>0-</code> <small>(octal)</small> or
* <code>0x-</code> <small>(hex)</small> prefix.</li>
* <li>Comments written in the slashshlash, slashstar, and hash conventions
* will be ignored.</li>
* </ul>
* @author JSON.org
* @version 3
*/

/*
* Sept 2008, rhess
* begin simple port of this code to Force.com Apex Code
* currently this code is only building Apex objects from JSON,
* not outputing JSON from Apex
* Oct add valueToString
*/
public class value {
/* apex object to store one of several different data types
* see : http://www.json.org/
*/
public value() { }
public value(boolean bb) { bool = bb;}
public value(integer ii) {num=ii;}
public value(Decimal ii) {dnum= ii;}
public value(string st) {str=st;}
public value( jsonobject oo) { obj = oo; }
public value( list<value> vary ) { values = vary; }

public string str {get;set;}
public integer num {get;set;}
public double dnum {get;set;}
public JSONObject obj {get;set;}
public list<value> values {get;set;} // array
public boolean bool {get;set;}
// if all members are null, value == null
// isNull()
/*
* value to string is used to output JSON for Google Visualization API
*/
public string valueToString() {
if ( this.bool != null ) return (this.bool?'true':'false');
if ( this.str != null ) return '"'+this.str +'"';
if ( this.num != null ) return String.valueof(this.num);
if ( this.dnum != null ) return String.valueof(this.dnum);
if ( this.values != null ) {
string ret = '[';
for ( value v:this.values) {
//system.debug( v );
ret += v.valueToString() + ',';
}
if ( this.values.size() > 0 ) {
// remove last comma
ret = ret.substring(0,ret.length()-1);
}
return ret + ']';
}
if ( this.obj != null ) {
return this.obj.valueToString();
}
return 'null';
}

} // end of class value

/* **
* The map where the JSONObject's properties are kept.
*/
private map<string,value> data = new map<string,value>();

public value getValue(string key) { return data.get(key); }

/* **
* Construct an empty JSONObject.
*/
public JSONObject() { }

/*
* Construct a JSONObject from a JSONTokener.
* @param x A JSONTokener object containing the source string.
* @ If there is a syntax error in the source string.
*/
public JSONObject(JSONTokener x) {
this();
string c;
String key;

if (x.nextClean() != '{') {
throw new jsonexception('A JSONObject text must begin with {');
}
for (;;) {
c = x.nextClean();
if ( c == null) {
throw new jsonexception('A JSONObject text must end with }');
} else if ( c== '}' ) {
return;
} else {
x.back();

key = (string)x.nextValue().str;
// system.debug( 'key is :'+key); }
}

/*
* The key is followed by ':'. We will also tolerate '=' or '=>'.
*/

c = x.nextClean();
if (c == '=') {
if (x.next() != '>') {
x.back();
}
} else if (c != ':') {
throw new jsonexception('Expected a : after a key');
}

putOpt(key, x.nextValue()); // load next value into data map

/*
* Pairs are separated by ','. We will also tolerate ';'.
*/

string nc = x.nextClean();
if ( nc == ';' || nc == ',' ) {
if (x.nextClean() == '}') {
return;
}
x.back();
} else if ( nc == '}' ) {
return;
} else {
throw new jsonexception('Expected a , or }');
}
}
}



/* **
* Construct a JSONObject from a source JSON text string.
* This is the most commonly used JSONObject constructor.
* @param source A string beginning
* with <code>{</code>&nbsp;<small>(left brace)</small> and ending
* with <code>}</code>&nbsp;<small>(right brace)</small>.
* @exception JSONException If there is a syntax error in the source string.
*/
public static JSONObject instance(String source) {
return new JSONObject( new JSONTokener(source) );
}
public JSONObject (String source) {
this( new JSONTokener(source) );
}


/* **
* Produce a string from a double. The string 'null' will be returned if
* the number is not finite.
* @param d A double.
* @return A String.

public static String doubleToString(double d) {
if (Double.isInfinite(d) || Double.isNaN(d)) {
return 'null';
}
// Shave off trailing zeros and decimal point, if possible.
String s = Double.toString(d);
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
while (s.endsWith('0')) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith('.')) {
s = s.substring(0, s.length() - 1);
}
}
return s;
}
*/

/* **
* Get the value object associated with a key.
*
* @param key A key string.
* @return The object associated with the key.
* @throws JSONException if the key is not found.
*/
public object get(string key) {
value ret = this.data.get(key);
if (ret == null) {
throw new JSONException('JSONObject[' + key + '] not found.');
}
if ( ret.str != null ) return ret.str;
if ( ret.num != null ) return ret.num;
if ( ret.bool != null ) return ret.bool;
system.assert( ret.obj == null , 'get cannot return nested json ojects');
// array?
return null;
}


/* **
* Get the boolean value associated with a key.
*
* @param key A key string.
* @return The truth.
* @throws JSONException
* if the value is not a Boolean or the String 'true' or 'false'.
*/
public boolean getBoolean(String key) {
Object o = this.get(key);
if ( o!=null) return (boolean) o;
throw new JSONException('JSONObject[' + key + '] is not a Boolean.');
}


/* **
* Get the double value associated with a key.
* @param key A key string.
* @return The numeric value.
* @ if the key is not found or
* if the value is not a Number object and cannot be converted to a number.

public double getDouble(String key) {
Object o = get(key);
try {
return o instanceof Number ?
((Number)o).doubleValue() :
Double.valueOf((String)o).doubleValue();
} catch (Exception e) {
throw new JSONException('JSONObject[' + quote(key) +
'] is not a number.');
}
}*/


/* **
* Get the int value associated with a key. If the number value is too
* large for an int, it will be clipped.
*
* @param key A key string.
* @return The integer value.
* @throws JSONException if the key is not found or if the value cannot
* be converted to an integer.

public int getInt(String key) {
Object o = get(key);
return o instanceof Number ?
((Number)o).intValue() : (int)getDouble(key);
} */


/*
* Get the JSONArray value associated with a key.
*
* @param key A key string.
* @return A JSONArray which is the value.
* @throws JSONException if the key is not found or
* if the value is not a JSONArray.

public JSONArray getJSONArray(String key) {
Object o = get(key);
if (o instanceof JSONArray) {
return (JSONArray)o;
}
throw new JSONException('JSONObject[' + quote(key) +
'] is not a JSONArray.');
}*/


/* **
* Get the JSONObject value associated with a key.
*
* @param key A key string.
* @return A JSONObject which is the value.
* @throws JSONException if the key is not found or
* if the value is not a JSONObject.

public JSONObject getJSONObject(String key) {
Object o = get(key);
if (o instanceof JSONObject) {
return (JSONObject)o;
}
throw new JSONException('JSONObject[' + quote(key) +
'] is not a JSONObject.');
}
*/

/* **
* Get the long value associated with a key. If the number value is too
* long for a long, it will be clipped.
*
* @param key A key string.
* @return The long value.
* @throws JSONException if the key is not found or if the value cannot
* be converted to a long.

public long getLong(String key) {
Object o = get(key);
return o instanceof Number ?
((Number)o).longValue() : (long)getDouble(key);
}
*/

/* **
* Get an array of field names from a JSONObject.
*
* @return An array of field names, or null if there are no names.

public static String[] getNames(JSONObject jo) {
int length = jo.length();
if (length == 0) {
return null;
}
Iterator i = jo.keys();
String[] names = new String[length];
int j = 0;
while (i.hasNext()) {
names[j] = (String)i.next();
j += 1;
}
return names;
}
*/

/* **
* Get an array of field names from an Object.
*
* @return An array of field names, or null if there are no names.

public static String[] getNames(Object object) {
if (object == null) {
return null;
}
Class klass = object.getClass();
Field[] fields = klass.getFields();
int length = fields.length;
if (length == 0) {
return null;
}
String[] names = new String[length];
for (int i = 0; i < length; i += 1) {
names[i] = fields[i].getName();
}
return names;
} */



/* **
* Get the string associated with a key.
*
* @param key A key string.
* @return A string which is the value.
* @throws JSONException if the key is not found.
*/
public String getString(String key) {
return this.data.get(key).str;
}


/* **
* Determine if the JSONObject contains a specific key.
* @param key A key string.
* @return true if the key exists in the JSONObject.
*/
public boolean has(String key) {
return this.data.containsKey(key);
}


/* **
* Determine if the value associated with the key is null or if there is
* no value.
* @param key A key string.
* @return true if there is no value associated with the key or if
* the value is the JSONObject.NULL object.

public boolean isNull(String key) {
return JSONObject.NULL.equals(opt(key));
}
*/

/* **
* Get an enumeration of the keys of the JSONObject.
*
* @return A set of the keys.
*/
public set<string> keys() {
return this.data.keySet();
}


/* **
* Get the number of keys stored in the JSONObject.
*
* @return The number of keys in the JSONObject.
*/
public integer length() {
return this.data.keySet().size();
}


/* **
* Produce a JSONArray containing the names of the elements of this
* JSONObject.
* @return A JSONArray containing the key strings, or null if the JSONObject
* is empty.

public JSONArray names() {
JSONArray ja = new JSONArray();
Iterator keys = keys();
while (keys.hasNext()) {
ja.put(keys.next());
}
return ja.length() == 0 ? null : ja;
}

/* **
* Produce a string from a Number.
* @param n A Number
* @return A String.
* @ If n is a non-finite number.

public static String numberToString(Number n)
{
if (n == null) {
throw new JSONException('Null pointer');
}
testValidity(n);

// Shave off trailing zeros and decimal point, if possible.

String s = n.toString();
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
while (s.endsWith('0')) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith('.')) {
s = s.substring(0, s.length() - 1);
}
}
return s;
} */


/* **
* Get an optional value associated with a key.
* @param key A key string.
* @return An object which is the value, or null if there is no value.
*/
public Object opt(String key) {
return key == null ? null : this.get(key);
}


/* **
* Get an optional boolean associated with a key.
* It returns false if there is no such key, or if the value is not
* true or the String 'true'.
*
* @param key A key string.
* @return The truth.
*/
public boolean optBoolean(String key) {
return optBoolean(key, false);
}


/* **
* Get an optional boolean associated with a key.
* It returns the defaultValue if there is no such key, or if it is not
* a Boolean or the String 'true' or 'false' (case insensitive).
*
* @param key A key string.
* @param defaultValue The default.
* @return The truth.
*/
public boolean optBoolean(String key, boolean defaultValue) {
try {
return getBoolean(key);
} catch (Exception e) {
return defaultValue;
}
}


/* **
* Put a key/value pair in the JSONObject, where the value will be a
* JSONArray which is produced from a Collection.
* @param key A key string.
* @param value A Collection value.
* @return this.
* @

public JSONObject put(String key, Collection value) {
put(key, new JSONArray(value));
return this;
}*/


/* **
* Get an optional double associated with a key,
* or NaN if there is no such key or if its value is not a number.
* If the value is a string, an attempt will be made to evaluate it as
* a number.
*
* @param key A string which is the key.
* @return An object which is the value.

public double optDouble(String key) {
return optDouble(key, Double.NaN);
} */


/* **
* Get an optional double associated with a key, or the
* defaultValue if there is no such key or if its value is not a number.
* If the value is a string, an attempt will be made to evaluate it as
* a number.
*
* @param key A key string.
* @param defaultValue The default.
* @return An object which is the value.

public double optDouble(String key, double defaultValue) {
try {
Object o = opt(key);
return o instanceof Number ? ((Number)o).doubleValue() :
new Double((String)o).doubleValue();
} catch (Exception e) {
return defaultValue;
}
} */


/* **
* Get an optional int value associated with a key,
* or zero if there is no such key or if the value is not a number.
* If the value is a string, an attempt will be made to evaluate it as
* a number.
*
* @param key A key string.
* @return An object which is the value.


thedabblerthedabbler

That class I believe is a part of opensource apex-lib.

Here is the link:

 

http://code.google.com/p/apex-library/source/browse/trunk/JSONObject/src/unpackaged/classes/JSONObject.cls?r=14

 

unless none of your other code in the org is using it you can remove that.

I don't think that's a part of standard salesforce.