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

Problem when exporting to CBOR #1171

Closed
arnaudbrejeon opened this issue Jul 21, 2018 · 3 comments
Closed

Problem when exporting to CBOR #1171

arnaudbrejeon opened this issue Jul 21, 2018 · 3 comments
Milestone

Comments

@arnaudbrejeon
Copy link

arnaudbrejeon commented Jul 21, 2018

  • What is the issue you have?
    When exporting to CBOR, in some cases the file is not properly formatted

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

    using json = nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float>;
    using json_input_adaper = nlohmann::detail::input_adapter;
    using json_output_adaper = nlohmann::detail::output_adapter<char>;

    auto jsNew = json();
    
    auto array = json::array();
    for (int i = 0; i < 1099; ++i) {
        array.push_back(0.0f);
    }
    jsNew["t"] = array;
    
    std::ofstream ofs("file.cbor", std::ios_base::binary);
    json::to_cbor(jsNew, json_output_adaper{ofs});
    ofs.close();
    
    std::ifstream ifs("file.cbor", std::ios_base::binary);
    const auto load = json::from_cbor(json_input_adaper{ifs});
  • What is the expected behavior?
    The cbor file should load properly

  • And what is the actual behavior instead?
    JSON_THROW(parse_error::create(110, chars_read, "unexpected end of input"));

  • Which compiler and operating system are you using? Is it a supported compiler?
    OSX, Xcode 9.4.1 clang

  • Did you use a released version of the library or the version from the develop branch?
    release

  • If you experience a compilation error: can you compile and run the unit tests?

I think the problem comes from the CBOR export. My floating point type is float, not double.
In function write_cbor, writing a floating point value forces the type to be 0xFB, even for floats. This works for double, but should be 0xFA for floats.

@nlohmann
Copy link
Owner

I cannot reproduce the issue: This code prints true:

#include <iostream>
#include <fstream>
#include "json.hpp"

int main()
{
    using json = nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float>;
    using json_input_adaper = nlohmann::detail::input_adapter;
    using json_output_adaper = nlohmann::detail::output_adapter<char>;
    
    auto jsNew = json();
    
    auto array = json::array();
    for (int i = 0; i < 1099; ++i) {
        array.push_back(0.0f);
    }
    jsNew["t"] = array;

    std::ofstream ofs("file.cbor", std::ios_base::binary);
    json::to_cbor(jsNew, json_output_adaper{ofs});
    ofs.close();
    
    std::ifstream ifs("file.cbor", std::ios_base::binary);
    const auto load = json::from_cbor(json_input_adaper{ifs});

    std::cout << std::boolalpha << (jsNew == load) << std::endl;
}

@arnaudbrejeon
Copy link
Author

Oh, I've just noticed the code is fixed on develop branch. I'm using release 3.1.2.
Sorry about that.

@nlohmann
Copy link
Owner

Indeed - it has been fixed with #1021. Thanks for checking back so fast!

@nlohmann nlohmann modified the milestone: Release 3.1.3 Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants