open-source-parsers / open-source-parsers/jsoncpp

I have encountered some problems with floating point conversion

Open
#1,389 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

I have encountered some problems with floating point conversion

Json::Value ReadFromString(std::string& strJson)
{
	try {
		const auto rawJsonLength = static_cast<int>(strJson.length());

		JSONCPP_STRING err;
		Json::Value root;

		Json::CharReaderBuilder builder;
		const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
		if (!reader->parse(strJson.c_str(), strJson.c_str() + rawJsonLength, &root, &err))
		{
			std::cout << "error" << std::endl;
			return false;
		}

		return root;
	}
	catch (std::exception& ec)
	{
		return false;
	}
}

std::string WriteToString(Json::Value jsRoot)
{
	std::string ret;
	try {
		Json::StreamWriterBuilder builder;
		builder["indentation"] = "";
		ret = Json::writeString(builder, jsRoot);
		return ret;
	}
	catch (std::exception& ec)
	{
		return ret;
	}
}
int main()
{
	try {
		std::string str = R"({"snr":9.2})";
		Json::Value jsSrc= ReadFromString(str);

		Json::Value jsDes;
		jsDes["snr"] = jsSrc["snr"];
		std::cout << WriteToString(jsDes) << std::endl;		
	}
	catch (std::exception& ec)
	{
		std::cout << ec.what() << std::endl;
	}
}

the output is {"snr":9.1999999999999993}
How to solve this problem

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied C++ reproduction, focusing on Json::CharReaderBuilder, Json::StreamWriterBuilder, and Json::writeString. Check the existing floating-point parsing and serialization behavior and project guidance. Done is unclear because the issue asks how to solve the output but names no expected formatting, file, or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
data
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.