On Feb 4, 11:01 am, Paolo Bonzini <bonz... / gnu.org> wrote:
> Here is my try using regexes.  I use the "copy-on-write trick" from
> the suffix tree quiz: the regex is always anchored to the beginning of
> the string using \A, and the matched text is discarded using
> post_match.  In some places where I don't want to discard I use (?
> =...).
>
> Using Eric's benchmark I get 36kb/sec, but I haven't benchmarked any
> other solution.
>
> http://pastie.caboo.se/147201

For what it's worth, I get 144kb/sec from tho_mica_l's solution, after
converting it to Ruby 1.8 like this:

 class JSONParser

    RXE = /
        \[|\]|
        \{|\}|
        (:)|
        (,\s*[}\]])|
        ,|
        ("(?>[^"\\]+|\\(?:u[0-9a-fA-F]{4}|[bfnrt"\/\\]))*")|
        -?(?=\d)(?>0|[1-9]\d*)(?>\.\d+)?(?>[Ee][+-]?\d+)?(?=\D|$)|
        true|
        false|
        (null)|
        (?>[[:space:][:cntrl:]]+)|
        ((?>.+))
        /xmu

    def parse(json)
        ruby = json.gsub(RXE) do |t|
            if !$5.nil?||!$2.nil?       then invalid($5.nil? ? $2 :
$5)
            elsif !$4.nil?              then 'nil'
            elsif !$1.nil?              then '=>'
            elsif !$3.nil?              then $3.gsub(/#/, '\\\\#')
            else
                t
            end
        end
        begin
            return eval(ruby)
        rescue Exception => e
            invalid(json)
        end
    end

    def invalid(string)
        raise RuntimeError, 'Invalid JSON: %s' % string
    end

end