Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:03:12+00:00 2026-05-23T13:03:12+00:00

Following the resolved question Boost Spirit: Error C2664, Cannot convert 'const boost::phoenix::actor<Eval>' to 'char'

  • 0

Following the resolved question Boost Spirit: Error C2664, Cannot convert 'const boost::phoenix::actor<Eval>' to 'char' , I have another question:

Why using the code below for js_key and js_string, I cannot capture print the strings in the format “str” or ‘str’. Those always return blank!

For example:

Input: {"a":"aa", b:'c'}
Actual output:

{
str:
str:
str: b
str:
Successfully parsed the input as JSON!

Expected output:

{
str: a
str: aa
str: b
str: c
Successfully parsed the input as JSON!

Please kindly advise. Thank you!

#include <map>
#include <string>
#include <vector>
#include <iostream>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_container.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/fusion/include/adapt_assoc_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

void print_char(char c)
{
    std::cout << c << std::endl;
}

void print_str(std::string str)
{
    std::cout << "str: " << str << std::endl;
}

template <typename Iterator>
struct json_grammar : qi::grammar<Iterator, ascii::space_type>
{
    json_grammar() : json_grammar::base_type(start)
    {
        using ascii::alpha;
        using ascii::alnum;
        using qi::long_long;
        using qi::long_double;
        using qi::lit;
        using qi::char_;
        using qi::lexeme;

        // 
        start =
            char_('{')              [phoenix::bind(&print_char, qi::_1)]
            >> -(js_member % ',')
            >> char_('}')
        ;
        // 
        js_member =
            js_key                  [phoenix::bind(&print_str, qi::_1)]
            >> ':' >> js_value
        ;
        // 
        js_key = (alpha >> *alnum) | js_string
        ;
        // 
        js_string = js_single_quoted_str | js_double_quoted_str
        ;
        // 
        js_array = lit('[') >> -(js_value % ',') >> lit(']')
        ;
        // 
        js_bool = lit("true") | lit("false")
        ;
        // 
        js_null = lit("null")
        ;
        // 
        js_value = js_string        [phoenix::bind(&print_str, qi::_1)]
            | js_num | js_array | start | js_bool | js_null;
        // 
        js_single_quoted_str = (lexeme["'" >> *(char_ - "'") >> "'"]);
        // 
        js_double_quoted_str = (lexeme['"' >> *(char_ - '"') >> '"']);
        // 
        js_num = long_long | long_double;
    }

    qi::rule<Iterator, std::string(), ascii::space_type> js_key;
    qi::rule<Iterator, std::string(), ascii::space_type> js_string;

    qi::rule<Iterator, ascii::space_type> start;
    qi::rule<Iterator, ascii::space_type> js_member;
    qi::rule<Iterator, ascii::space_type> js_value;
    qi::rule<Iterator, ascii::space_type> js_single_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_double_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_array;
    qi::rule<Iterator, ascii::space_type> js_num;
    qi::rule<Iterator, ascii::space_type> js_bool;
    qi::rule<Iterator, ascii::space_type> js_null;
};

int main()
{
    std::string inputStr;
    json_grammar<std::string::const_iterator> jsonParser;
    bool parseOK = false;

    while(std::getline(std::cin, inputStr)) {
        if(inputStr.empty() || inputStr[0] == 'q' || inputStr[0] == 'Q')
            break;

        std::string::const_iterator iter = inputStr.begin();
        std::string::const_iterator iterEnd = inputStr.end();

        parseOK = qi::phrase_parse(iter, iterEnd, jsonParser, ascii::space);

        if(parseOK && iter == iterEnd) {
            std::cout << "Successfully parsed the input as JSON!" << std::endl;
        } else {
            std::cout << "Cannot parse the input as JSON!" << std::endl;
        }
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:03:13+00:00Added an answer on May 23, 2026 at 1:03 pm

    The answer is very simple: 2 structs js_single_quoted_str and js_double_quoted_str must be redefined as the following to make the parser rock:

    qi::rule<Iterator, std::string(), ascii::space_type> js_single_quoted_str;
    qi::rule<Iterator, std::string(), ascii::space_type> js_double_quoted_str;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As
I have asked a similar question previously but it was never resolved so here
Question resolved I have two tables, orders and customers, and I'd like to find
Following up on a previous question , I have a few issues to resolve
I have interface a question/confusion related to IoC purposes. Consider the following: ISite -
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
Suppose the following: I have a database set up on database.mywebsite.com , which resolves
Following on from my recent question on Large, Complex Objects as a Web Service
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following this question: Good crash reporting library in c# Is there any library like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.