No commit activity in last 3 years
No release in over 3 years
An alternative JSON parser
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
>= 0
~> 10.0
>= 0
 Project Readme

json-expect-parser

An alternative JSON parser.

Synopsis

t.json

[
  {
    "id": 1,
    "name": "ksss",
    "admin": true
  },
  {
    "id": 2,
    "name": "foo",
    "admin": false
  },
  {
    "id": 3,
    "name": "bar",
    "admin": false
  }
]
require 'json/expect/parser'

File.open("t.json") do |io|
  expect = JSON::Expect::Parser.new(io)
  expect.array do
    expect.object do
      case expect.key
      when "id"
        p expect.integer #=> 1, 2, 3
      when "name"
        p expect.string #=> "ksss", "foo", "bar"
      when "admin"
        p expect.boolean #=> true, false, false
      end
    end
  end
end

Explicitly parse

json-expect-parser parse JSON explicitly.

expect = JSON::Expect::Parser.new(%([10, 20, 30]))
expect.array { p expect.integer } #=> 10, 20, 30

If get unexpected value, It failed.

expect = JSON::Expect::Parser.new(%([10, 20, 30]))
expect.array { p expect.string }
#=> JSON::Expect::ParseError: expected "\"" but was "1"

API

object

expect = JSON::Expect::Parser.new(%({"a": 10, "b": 20}))

expect.object
#=> #<Enumerator: #<JSON::Expect::Parser ...>>

expect.object do
  expect.key #=> "a", "b"
  expect.integer #=> 10, 20
end

array

expect = JSON::Expect::Parser.new(%(["foo", "bar", "baz"]))

expect.array
#=> #<Enumerator: #<JSON::Expect::Parser ...>>

expect.array do  
  expect.string #=> "foo", "bar", "baz"
end

expect.rewind

expect.array.map { expect.string }
#=> ["foo", "bar", "baz"]

integer

expect = JSON::Expect::Parser.new(%(100))
expect.integer #=> 100

number(alias float)

expect = JSON::Expect::Parser.new(%(1.1))
expect.float #=> 1.1

string

expect = JSON::Expect::Parser.new(%("foo"))
expect.string #=> "foo"

key

Use in object then get object key string

boolean

expect = JSON::Expect::Parser.new(%("true"))
expect.boolean #=> true

null

expect = JSON::Expect::Parser.new(%("null"))
expect.null #=> nil

object_or_null

expect = JSON::Expect::Parser.new(%([{"a": 1}, null]))
expect.array do
  expect.object_or_null do
    expect.key #=> "a"
    expect.integer #=> 1
  end
end

array_or_null

expect = JSON::Expect::Parser.new(%([[1, 2, 3], null]))
expect.array do
  expect.array_or_null do
    expect.integer #=> 1, 2, 3
  end
end

null_or

expect = JSON::Expect::Parser.new(%({"a": "foo", "b": null}))
expect.object do
  expect.key #=> "a", "b"
  expect.null_or { expect.string } #=> "foo", nil
end

value(alias parse)

expect = JSON::Expect::Parser.new(%([[true, false], null, 1, "foo"]))
expect.value
#=> [[true, false], nil, 1.0, "foo"]

rewind

expect = JSON::Expect::Parser.new(%("foo"))
expect.string #=> "foo"
expect.string #=> "foo"
#=> JSON::Expect::ParseError: expected "\"" but was nil
expect.rewind
expect.string #=> "foo"

Benchmark

$ bundle ex benchmark/comparison.rb
ruby v2.3.1
Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64
2109KB json string

=== JSON::Ext::Parser ===
time: 0.08309706707950681
memory: 30164.0KB

=== JSON::Pure::Parser ===
time: 0.4895962669979781
memory: 35684.0KB

=== JSON::Expect::Parser ===
time: 0.7294884130824357
memory: 23568.0KB