Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sax_parse(iterator, json_sax_t *) string callback clobbers spaces #3574

Closed
2 tasks
brhamon opened this issue Jul 11, 2022 · 2 comments
Closed
2 tasks

sax_parse(iterator, json_sax_t *) string callback clobbers spaces #3574

brhamon opened this issue Jul 11, 2022 · 2 comments
Labels
solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@brhamon
Copy link

brhamon commented Jul 11, 2022

Description

Implementing a sax_parse consumer. The string callback is called with a value that has all spaces removed.

Reproduction steps

Create a SAX consumer class derived from json_sax_t. Set a breakpoint in the string method so you can examine the output of the string_t variable the parser passes in. Parse a JSON file that contains a string with at least one interior space.

The value passed to the string method has the spaces removed.

Expected vs. actual results

Input JSON:

[ "The sax parser will delete all spaces between words." ]

Expected output:

The sax parser will delete all spaces between words.

Actual output:

Thesaxparserwilldeleteallspacesbetweenwords.

Minimal code example

#include <sstream>
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

class string_consumer : public json::json_sax_t
{
public:
    bool string(string_t& val) override
    {
        std::cout << val << std::endl;
        return true;
    }

    bool null() override { return true; }
    bool boolean(bool val) override { return true; }
    bool number_integer(number_integer_t val) override { return true; }
    bool number_unsigned(number_unsigned_t val) override { return true; }
    bool number_float(number_float_t val, const string_t& s) override { return true; }
    bool start_object(std::size_t elements) override { return true; }
    bool end_object() override { return true; }
    bool start_array(std::size_t elements) override { return true; }
    bool end_array() override { return true; }
    bool key(string_t& val) override { return true; }
    bool binary(json::binary_t& val) override { return true; }
    bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override {
        return false;
    }
};

int main(int , char* []) {
    std::stringstream jf("[ \"The sax parser will delete all spaces between words.\" ]");

    string_consumer sc;

    bool result = json::sax_parse(std::istream_iterator<char>(jf), std::istream_iterator<char>(), &sc);
    return result ? 0 : 1;
}

Error messages

No error messages.

Compiler and operating system

Microsoft Visual Studio Community 2019 (Version 16.11.16), Windows SDK 10.0.18362.0

Library version

3.10.5#3 from vcpkg (version 2022-06-17-9268e366206712e38102b28dbd1617697a99ff2e)

Validation

@nlohmann
Copy link
Owner

This is not an issue of the library, but standard behavior for streams. If you want to not skip whitespace do this:

std::noskipws(jf);
bool result = json::sax_parse(std::istream_iterator<char>(jf), std::istream_iterator<char>(), &sc);

or simpler:

bool result = json::sax_parse(jf, &sc);

@nlohmann nlohmann added solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Jul 11, 2022
@brhamon
Copy link
Author

brhamon commented Jul 11, 2022

Thanks Niels. Problem solved. Sorry to waste your time.

@brhamon brhamon closed this as completed Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants