예제 #1
0
static void __print_container (string & result
        , json::value const & value
        , print_spec const & pspec
        , int indent)
{
    if (value.size() == 0) { 
        __print_open_brace(result, value, pspec);
        __print_close_brace(result, value, pspec);
        return;
    }
    
    json::value::const_iterator it_begin = value.cbegin();
    json::value::const_iterator it_end   = value.cend();
    json::value::const_iterator it       = it_begin;
    json::value::const_iterator it_next  = it_begin;
        
    ++it_next;
    
    indent += pspec.base_indent;
    
    __print_open_brace(result, value, pspec);
    result.append(pspec.new_line); // container content always begin after new line
    
    for (; it != it_end; ++it, ++it_next) {
        if (it == it_begin) {
            __print_indent(result, pspec, indent);
            
            if (pspec.comma_position == json::comma_next_line) {
                __print_indent(result, pspec, pspec.first_item_indent);
            }
        }
        
        if (value.is_object()) {
            __print_value(result, it.key(), *it, pspec, indent);
        } else {
            __print_value(result, *it, pspec, indent);
        }
        
        if (it_next != it_end) {
            __print_comma(result, pspec, indent);
        }
    }
    
    indent -= pspec.base_indent;    
    
    result.append(pspec.new_line);
    __print_indent(result, pspec, indent);
    __print_close_brace(result, value, pspec);
}
예제 #2
0
void GoogleSearchClient::flattenJsonInnerLoop(json::value const & val, FlatJsonObject & json_map) {
  // input : Body of the google search response as json::value
  // This recursive function parse the Json raw value to a C++ multimap
  if (!val.is_null() && val.is_object()) {
    for (auto const &iter : val.as_object()) {
      //iter on each element for Json object type. Return a vect<string_t,json::value>
      const auto &mapKey = iter.first;
      const auto &content = iter.second;
      json_map.insert(std::pair<utility::string_t, utility::string_t>(mapKey, content.serialize()));
      flattenJsonInnerLoop(content, json_map);
    }
  } else if (val.is_array()) {
    for (auto const &content : val.as_array()) {
      //iter on each element for Json array type. Return a json::value
      flattenJsonInnerLoop(content, json_map);
    }
  }
}
예제 #3
0
http::http_request http_client_impl::_build_request(http::method method, http::uri_builder request_uri, const ::utility::string_t& accept, json::value object) const
{
    http::http_request msg(method);

    if (!accept.empty())
        msg.headers().add(U("Accept"), accept);

    msg.set_request_uri(request_uri.to_uri());

    if (method == http::methods::POST || method == http::methods::PUT || method == http::methods::PATCH)
    {
        if(object.is_null() || !object.is_object())
            throw std::invalid_argument("POST, PUT, and PATCH requests require a payload");

        msg.set_body(object);
    }

    return msg;
}
예제 #4
0
파일: json-test.cpp 프로젝트: sgorsten/json
    REQUIRE( json::value{'A'}.get_contents() == "65" );

    // Constructing with a json::array should produce array values
    json::value array_value = json::array{2,3.14f,"foo",false,nullptr};
    REQUIRE( array_value.is_array() );
    REQUIRE( array_value.get_array().size() == 5 );
    REQUIRE( array_value[0] == 2 );
    REQUIRE( array_value[1] == 3.14f );
    REQUIRE( array_value[2] == "foo" );
    REQUIRE( array_value[3] == false );
    REQUIRE( array_value[4] == nullptr );
    REQUIRE( array_value.get_kind() == json::kind::array );

    // Constructing with a json::object should produce object values
    json::value object_value = json::object{{"a",2},{"b",3.14f},{"c","foo"},{"d",false},{"e",nullptr}};
    REQUIRE( object_value.is_object() );
    REQUIRE( object_value.get_object().size() == 5 );
    REQUIRE( object_value["a"] == 2 );
    REQUIRE( object_value["b"] == 3.14f );
    REQUIRE( object_value["c"] == "foo" );
    REQUIRE( object_value["d"] == false );
    REQUIRE( object_value["e"] == nullptr );
    REQUIRE( object_value.get_kind() == json::kind::object );
}

std::string packed_print(const json::value & v) { std::ostringstream ss; ss << v; return ss.str(); }
std::string pretty_print(const json::value & v) { std::ostringstream ss; ss << tabbed(v, 4); return ss.str(); }

TEST_CASE( "printing" )
{
    REQUIRE( packed_print(5) == "5" );