121 char[] charArray = json.ToCharArray();
131 Debug.Log(
"object value =");
157 StringBuilder builder =
new StringBuilder(BUILDER_CAPACITY);
159 return (success ? builder.ToString() : null);
192 if (startIndex < 0) {
205 Hashtable table =
new Hashtable();
237 object value =
ParseValue(json, ref index, ref success);
251 ArrayList array =
new ArrayList();
268 object value =
ParseValue(json, ref index, ref success);
280 protected object ParseValue(
char[] json, ref
int index, ref
bool success)
298 return Boolean.Parse(
"TRUE");
302 return Boolean.Parse(
"FALSE");
326 bool complete =
false;
329 if (index == json.Length) {
337 }
else if (c ==
'\\') {
339 if (index == json.Length) {
345 }
else if (c ==
'\\') {
347 }
else if (c ==
'/') {
349 }
else if (c ==
'b') {
351 }
else if (c ==
'f') {
353 }
else if (c ==
'n') {
355 }
else if (c ==
'r') {
357 }
else if (c ==
't') {
359 }
else if (c ==
'u') {
360 int remainingLength = json.Length - index;
361 if (remainingLength >= 4) {
362 char[] unicodeCharArray =
new char[4];
363 Array.Copy(json, index, unicodeCharArray, 0, 4);
366 s +=
"&#x" +
new string(unicodeCharArray) +
";";
398 int charLength = (lastIndex - index) + 1;
399 char[] numberCharArray =
new char[charLength];
401 Array.Copy(json, index, numberCharArray, 0, charLength);
402 index = lastIndex + 1;
403 return Double.Parse(
new string(numberCharArray));
409 for (lastIndex = index; lastIndex < json.Length; lastIndex++) {
410 if (
"0123456789+-.eE".IndexOf(json[lastIndex]) == -1) {
414 return lastIndex - 1;
419 for (; index < json.Length; index++) {
420 if (
" \t\n\r".IndexOf(json[index]) == -1) {
428 int saveIndex = index;
436 if (index == json.Length) {
440 char c = json[index];
455 case '0':
case '1':
case '2':
case '3':
case '4':
456 case '5':
case '6':
case '7':
case '8':
case '9':
464 int remainingLength = json.Length - index;
467 if (remainingLength >= 5) {
468 if (json[index] ==
'f' &&
469 json[index + 1] ==
'a' &&
470 json[index + 2] ==
'l' &&
471 json[index + 3] ==
's' &&
472 json[index + 4] ==
'e') {
479 if (remainingLength >= 4) {
480 if (json[index] ==
't' &&
481 json[index + 1] ==
'r' &&
482 json[index + 2] ==
'u' &&
483 json[index + 3] ==
'e') {
490 if (remainingLength >= 4) {
491 if (json[index] ==
'n' &&
492 json[index + 1] ==
'u' &&
493 json[index + 2] ==
'l' &&
494 json[index + 3] ==
'l') {
505 if (objectOrArray is Hashtable) {
507 }
else if (objectOrArray is ArrayList) {
518 IDictionaryEnumerator e = anObject.GetEnumerator();
520 while (e.MoveNext()) {
521 string key = e.Key.ToString();
522 object value = e.Value;
525 builder.Append(
", ");
546 for (
int i = 0; i < anArray.Count; i++) {
547 object value = anArray[i];
550 builder.Append(
", ");
571 if (value.GetType().IsArray) {
573 }
else if (value is
string) {
575 }
else if (value is Char) {
577 }
else if (value is Hashtable) {
579 }
else if (value is ArrayList) {
582 else if ((value is Boolean) && ((Boolean)value ==
true)) {
583 builder.Append(
"true");
584 }
else if ((value is Boolean) && ((Boolean)value ==
false)) {
585 builder.Append(
"false");
587 else if (value.GetType().IsPrimitive) {
589 }
else if (value == null) {
590 builder.Append(
"null");
599 builder.Append(
"\"");
601 char[] charArray = aString.ToCharArray();
602 for (
int i = 0; i < charArray.Length; i++) {
603 char c = charArray[i];
605 builder.Append(
"\\\"");
606 }
else if (c ==
'\\') {
607 builder.Append(
"\\\\");
608 }
else if (c ==
'\b') {
609 builder.Append(
"\\b");
610 }
else if (c ==
'\f') {
611 builder.Append(
"\\f");
612 }
else if (c ==
'\n') {
613 builder.Append(
"\\n");
614 }
else if (c ==
'\r') {
615 builder.Append(
"\\r");
616 }
else if (c ==
'\t') {
617 builder.Append(
"\\t");
619 int codepoint =
System.Convert.ToInt32(c);
620 if ((codepoint >= 32) && (codepoint <= 126)) {
623 builder.Append(
"\\u" +
System.Convert.ToString(codepoint, 16).PadLeft(4,
'0'));
628 builder.Append(
"\"");
633 builder.Append(
System.Convert.ToString(number));
static string JsonEncode(object json)
Converts a Hashtable / ArrayList object into a JSON string
double ParseNumber(char[] json, ref int index)
string ParseString(char[] json, ref int index)
ArrayList ParseArray(char[] json, ref int index)
const int TOKEN_SQUARED_CLOSE
int NextToken(char[] json, ref int index)
void SerializeNumber(double number, StringBuilder builder)
const int TOKEN_CURLY_CLOSE
Hashtable ParseObject(char[] json, ref int index)
static string GetLastErrorSnippet()
If a decoding error occurred, this function returns a piece of the JSON string at which the error too...
bool SerializeObject(Hashtable anObject, StringBuilder builder)
This class encodes and decodes JSON strings.
const int TOKEN_SQUARED_OPEN
bool SerializeValue(object value, StringBuilder builder)
static bool LastDecodeSuccessful()
On decoding, this function returns the position at which the parse failed (-1 = no error)...
static object JsonDecode(string json)
Parses the string json into a value
void SerializeString(string aString, StringBuilder builder)
const int BUILDER_CAPACITY
int LookAhead(char[] json, int index)
static int GetLastErrorIndex()
On decoding, this function returns the position at which the parse failed (-1 = no error)...
bool SerializeArray(ArrayList anArray, StringBuilder builder)
bool SerializeObjectOrArray(object objectOrArray, StringBuilder builder)
const int TOKEN_CURLY_OPEN
object ParseValue(char[] json, ref int index, ref bool success)
int lastErrorIndex
On decoding, this value holds the position at which the parse failed (-1 = no error).
void EatWhitespace(char[] json, ref int index)
int GetLastIndexOfNumber(char[] json, int index)