JSON for Modern C++  2.1.1

◆ parse() [2/6]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename CharT , typename std::enable_if< std::is_pointer< CharT >::value and std::is_integral< typename std::remove_pointer< CharT >::type >::value and sizeof(typename std::remove_pointer< CharT >::type)==1, int >::type = 0>
static basic_json nlohmann::basic_json::parse ( const CharT  s,
const parser_callback_t  cb = nullptr 
)
inlinestatic
Template Parameters
CharTcharacter/literal type with size of 1 byte
Parameters
[in]sstring literal to read a serialized JSON value from
[in]cba parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional)
Returns
result of the deserialization
Exceptions
parse_error.101in case of an unexpected token
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails
Complexity
Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the parser callback function cb has a super-linear complexity.
Note
A UTF-8 byte order mark is silently ignored.
String containers like std::string or string_t can be parsed with parse(const ContiguousContainer&, const parser_callback_t)
Example
The example below demonstrates the parse() function with and without callback function.
1 #include "json.hpp"
2 #include <iomanip> // for std::setw
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // a JSON text
9  auto text = R"(
10  {
11  "Image": {
12  "Width": 800,
13  "Height": 600,
14  "Title": "View from 15th Floor",
15  "Thumbnail": {
16  "Url": "http://www.example.com/image/481989943",
17  "Height": 125,
18  "Width": 100
19  },
20  "Animated" : false,
21  "IDs": [116, 943, 234, 38793]
22  }
23  }
24  )";
25 
26  // parse and serialize JSON
27  json j_complete = json::parse(text);
28  std::cout << std::setw(4) << j_complete << "\n\n";
29 
30 
31  // define parser callback
32  json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed)
33  {
34  // skip object elements with key "Thumbnail"
35  if (event == json::parse_event_t::key and parsed == json("Thumbnail"))
36  {
37  return false;
38  }
39  else
40  {
41  return true;
42  }
43  };
44 
45  // parse (with callback) and serialize JSON
46  json j_filtered = json::parse(text, cb);
47  std::cout << std::setw(4) << j_filtered << '\n';
48 }
basic_json<> json
default JSON class
Definition: json.hpp:13933
static basic_json parse(T(&array)[N], const parser_callback_t cb=nullptr)
deserialize from an array
Definition: json.hpp:7289
std::function< bool(int depth, parse_event_t event, basic_json &parsed)> parser_callback_t
per-element parser callback type
Definition: json.hpp:2148
the parser read a key of a value in an object
parse_event_t
JSON callback events.
Definition: json.hpp:2078
Output (play with this example online):
{
    "Image": {
        "Animated": false,
        "Height": 600,
        "IDs": [
            116,
            943,
            234,
            38793
        ],
        "Thumbnail": {
            "Height": 125,
            "Url": "http://www.example.com/image/481989943",
            "Width": 100
        },
        "Title": "View from 15th Floor",
        "Width": 800
    }
}

{
    "Image": {
        "Animated": false,
        "Height": 600,
        "IDs": [
            116,
            943,
            234,
            38793
        ],
        "Title": "View from 15th Floor",
        "Width": 800
    }
}
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/parse__string__parser_callback_t.cpp -o parse__string__parser_callback_t 
See also
parse(std::istream&, const parser_callback_t) for a version that reads from an input stream
Since
version 1.0.0 (originally for string_t)

Definition at line 7331 of file json.hpp.