Boost logo

Boost Users :

Subject: Re: [Boost-users] [OT] Interested in good (fast, robust) boost-based JSON API
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-12-01 18:20:18


On Tue, Dec 1, 2009 at 4:03 PM, Dominique Devienne <ddevienne_at_[hidden]> wrote:
> On Tue, Dec 1, 2009 at 4:42 PM, OvermindDL1 <overminddl1_at_[hidden]> wrote:
>> On Tue, Dec 1, 2009 at 12:44 PM, Dominique Devienne <ddevienne_at_[hidden]> wrote:
>>> Would you mind sharing the grammar and parser in
>>> http://boost-spirit.com/repository/grammars/show_contents.php perhaps,
>>> with a Boost like license?
>>
>> I did post the old version (that worked on a much older Spirit trunk,
>> not the current release) on the Spirit list a long while ago.  I meant
>> to update it and give it a better interface and a karma output too
>> then give it to Spirit as an example, I just need to get motivated to
>> give the little amount of time that I have available, to it.  ;-)
>
> I'm still interested in just the Spirit 2.1 grammar for JSON if you
> don't mind, to implement just an event-based SAX-like parser for JSON.
> I'd happily use a full reading and writing impl, but I'd be happy with
> just the grammar for now.
>
>> (*hint* poke me to get it done, random emails, that helps a ton).
>
> Consider yourself poked ;) Thanks, --DD

Ew... I really did make this back when I was still learning Spirit, I
can make it better now for sure...

Just to show you the simple grammar for JSON parsing though, I was
trying to follow the Spec *to*the*letter*, and this is what I came up
with (which I definitely need to redesign for the modern Spirit2.1).
As stated, this is for the old trunk Spirit so it might not compile
with the current version...

                struct JSON_grammar : qi::grammar<typename
StringType::const_iterator, Value(),
boost::spirit::ODL1_JSON_Char_Class::space_type>
                {
                        JSON_grammar() : JSON_grammar::base_type(ValueRule)
                        {
                                using qi::on_error;
                                using qi::fail;
                                using qi::debug;
                                using qi::lit;
                                using qi::raw;
                                using spirit::double_;
                                using namespace boost::spirit::ODL1_JSON_Char_Class;

                                ValueRule
                                        =( lit("null") [spirit::_val = null_const()]
                                        | lit("false") [spirit::_val = false_const()]
                                        | lit("true") [spirit::_val = true_const()]
                                        | double_ [spirit::_val = spirit::_1]
                                        | StringRule [spirit::_val = spirit::_1]
                                        | ArrayRule [spirit::_val = spirit::_1]
                                        | ObjectRule [spirit::_val = spirit::_1]
                                        );

                                StringRule
                                        = char_('"') [spirit::_val = ""]
>> *(
                                                (char_-(char_("\"\\")|cntrl)) [spirit::_val += spirit::_1]
                                                |(lit('\\') >>
                                                        (lit('b') [spirit::_val += '\b']
                                                        |lit('f') [spirit::_val += '\f']
                                                        |lit('n') [spirit::_val += '\n']
                                                        |lit('r') [spirit::_val += '\r']
                                                        |lit('t') [spirit::_val += '\t']
                                                        | ( lit('u')
>> hex4 [spirit::_val += spirit::_1]
                                                                )
                                                        |char_ [spirit::_val += spirit::_1]
                                                        )
                                                )
                                        )
>> '"'
                                        ;

                                ArrayRule
                                        %= '['
>> ValueRule % ','
>> ']'
                                        ;

                                ObjectRule
                                        %= '{'
>> (StringRule >> ':' >> ValueRule) % ','
>> '}'
                                        ;

                                ValueRule.name("ValueRule");
                                StringRule.name("StringRule");
                                ArrayRule.name("ArrayRule");
                                ObjectRule.name("ObjectRule");

                                on_error<fail>
                                        (
                                        ValueRule
                                        , onerror()
                                        );

#ifdef ODL1_JSON_DEBUG
                                debug(ValueRule);
                                debug(StringRule);
                                debug(ArrayRule);
                                debug(ObjectRule);
#endif // ODL1_JSON_DEBUG
                        }

                        qi::rule<typename StringType::const_iterator, Value(),
boost::spirit::ODL1_JSON_Char_Class::space_type> ValueRule;
                        qi::rule<typename StringType::const_iterator, StringType()> StringRule;
                        qi::rule<typename StringType::const_iterator, ValueArray(),
boost::spirit::ODL1_JSON_Char_Class::space_type> ArrayRule;
                        qi::rule<typename StringType::const_iterator, ValueObject(),
boost::spirit::ODL1_JSON_Char_Class::space_type> ObjectRule;
                        qi::uint_parser<unsigned, 16, 4, 4> hex4;
                };

Yuck, that really is ugly... That should show you how it works, but
yeah, wait until I can actually redesign it. I do not have time right
this moment (keep poking me and I may have this odd hour here or
there), but should have some dedicated time late next week, just keep
on poking/emailing me.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net