/*
Copyright (C) 2008 Patrick van Bergen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace GreenshotPlugin.Core {
///
/// This parses a JSON response, a modified version of the code found at:
/// See: http://techblog.procurios.nl/k/n618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
///
/// This file is under the MIT License, which is GPL Compatible and according to: http://en.wikipedia.org/wiki/MIT_License
/// can be used under the GPL "umbrella".
///
/// TODO: code should be replaced when upgrading to .NET 3.5 or higher!!
///
public class JSONHelper {
public const int TOKEN_NONE = 0;
public const int TOKEN_CURLY_OPEN = 1;
public const int TOKEN_CURLY_CLOSE = 2;
public const int TOKEN_SQUARED_OPEN = 3;
public const int TOKEN_SQUARED_CLOSE = 4;
public const int TOKEN_COLON = 5;
public const int TOKEN_COMMA = 6;
public const int TOKEN_STRING = 7;
public const int TOKEN_NUMBER = 8;
public const int TOKEN_TRUE = 9;
public const int TOKEN_FALSE = 10;
public const int TOKEN_NULL = 11;
private const int BUILDER_CAPACITY = 2000;
///
/// Parses the string json into a value
///
/// A JSON string.
/// An ArrayList, a Hashtable, a double, a string, null, true, or false
public static IDictionary JsonDecode(string json) {
bool success = true;
return JsonDecode(json, ref success);
}
///
/// Parses the string json into a value; and fills 'success' with the successfullness of the parse.
///
/// A JSON string.
/// Successful parse?
/// An ArrayList, a Hashtable, a double, a string, null, true, or false
public static IDictionary JsonDecode(string json, ref bool success) {
success = true;
if (json != null) {
char[] charArray = json.ToCharArray();
int index = 0;
IDictionary value = ParseValue(charArray, ref index, ref success) as IDictionary;
return value;
} else {
return null;
}
}
protected static IDictionary ParseObject(char[] json, ref int index, ref bool success) {
IDictionary table = new Dictionary();
int token;
// {
NextToken(json, ref index);
bool done = false;
while (!done) {
token = LookAhead(json, index);
if (token == TOKEN_NONE) {
success = false;
return null;
} else if (token == TOKEN_COMMA) {
NextToken(json, ref index);
} else if (token == TOKEN_CURLY_CLOSE) {
NextToken(json, ref index);
return table;
} else {
// name
string name = ParseString(json, ref index, ref success);
if (!success) {
success = false;
return null;
}
// :
token = NextToken(json, ref index);
if (token != TOKEN_COLON) {
success = false;
return null;
}
// value
object value = ParseValue(json, ref index, ref success);
if (!success) {
success = false;
return null;
}
table.Add(name, value);
}
}
return table;
}
protected static IList