|
using System;
namespace Ranslant.JSON.Linq
{
// use only for invalid JSON text (see specs: http://www.json.org/ )
public class JsonException: Exception
{
public JsonException(string s) : base("JSON: " + s)
{ }
}
public class JsonParserException : Exception
{
public JsonParserException(string s) : base(s)
{ }
}
internal static class JExtensions
{
internal static char[] spaces = { ' ', '\n', '\t', '\r' };
// extension method
// at first, I had:
// public static string ConsumeToken(this string thisString, string token, bool removeSpaceBeforeToken, bool removeSpaceAfterToken)
// but since removeSpaceBeforeToken and removeSpaceAfterToken were always set to true, I removed them.
public static string ConsumeToken(this string thisString, string token)
{
string result = thisString;
//if (removeSpaceBeforeToken)
result = result.TrimStart(spaces);
if (result.StartsWith(token))
result = result.Remove(0, token.Length);
else
{
throw new JsonParserException("token '" + token + "' not found");
}
//if (removeSpaceAfterToken)
result = result.TrimStart(spaces);
return result;
}
public static bool StartsWith(this byte[] thisArray, byte[] array)
{
int count = array.Length;
if (count > thisArray.Length)
return false;
while (--count >= 0)
{
if (thisArray[count] != array[count])
return false;
}
return true;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.