Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Small LINQ to JSON Library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (28 votes)
6 Dec 2011CPOL13 min read 80.9K   2K   79  
A small LinqToJSON library in C#, and how it works
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 JExtenssions
	{
        // extension method
        public static string ConsumeToken(this string thisString, string token, bool removeSpaceBeforeToken, bool removeSpaceAfterToken)
        {
            string result = thisString;

            char[] spaces = { ' ', '\n', '\t', '\r' };

            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;
        }

	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IPG
Germany Germany
since 2010: C# with WPF
since 2002: C++ (MFC / QT)
since 1995: C, Java, Pascal


"if a drummer can do it, anobody can" - Bruce Dickinson

Comments and Discussions