Click here to Skip to main content
13,344,281 members (81,486 online)
Click here to Skip to main content


80 bookmarked
Posted 23 Nov 2011

Small LINQ to JSON Library

, 6 Dec 2011
A small LinqToJSON library in C#, and how it works
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Ranslant.JSON.Linq
    public class JNumber : IJValue
        private static string Culture = "en-US";
        private const string Pattern = @"^-?([1-9][0-9]*|0)(\.[0-9]+)?((e|E)(\+|-)?[0-9]+)?$";    // see

        private double _content;
        public double Content
                return this._content;

        public JNumber(double number)
            _content = number;

        /// <summary>
        /// The text representing the number to be stored is checked for validity.
        /// An exception will be thrown if an error is found.
        /// </summary>
        /// <param name="text">a string representing the number to be stored</param>
        public JNumber(string text)
            _content = Double.Parse(text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.GetCultureInfo(JNumber.Culture));

        private void IsValidJsonNumber(string text)
            if (!Regex.IsMatch(text, Pattern, RegexOptions.ExplicitCapture))
                throw new JsonException("invalid number: " + text);

        #region IJValue Members

        public string ToString(int indentLevel)
            return this.ToString();

        public new string ToString()
            return _content.ToString(System.Globalization.CultureInfo.GetCultureInfo(JNumber.Culture));


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.


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


About the Author

Guillaume Ranslant
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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 Dec 2011
Article Copyright 2011 by Guillaume Ranslant
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid