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

Added bool and null for as_double() #532

Closed

Conversation

MonkeybreadSoftware
Copy link
Contributor

The code doing JSON diffs can't compare as it would run the "not a double" exception, if one side has a number and other a null value. So we fix this by returning 0 here for double value.

The code doing JSON diffs can't compare as it would run the "not a double" exception, if one side has a number and other a null value.
So we fix this by returning 0 here for double value.
@danielaparker
Copy link
Owner

The code doing JSON diffs can't compare as it would run the "not a double" exception, if one side has a number and other a null value. So we fix this by returning 0 here for double value.

Can you give an example of "The code doing JSON diffs" that fails? Thanks.

@MonkeybreadSoftware
Copy link
Contributor Author

Here you go:

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jmespath/jmespath.hpp>
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <jsoncons_ext/mergepatch/mergepatch.hpp>

using namespace jsoncons;

json_options options;


int main(int argc, const char * argv[]) {
	// insert code here...
	options.lossless_number(true);
	options.bigint_format(bigint_chars_format::number);
	options.byte_string_format(byte_string_chars_format::base64);

	std::cout << "Hello, World!\n";
	
	const char* json1 = "{\"hello\":123.4, \"hello2\":null}";
	const char* json2 = "{\"hello\":null,  \"hello2\":123.4 }";

	ojson j1 = ojson::parse(json1, options);
	ojson j2 = ojson::parse(json2, options);
	
	std::string s1;
	std::string s2;
	std::string s3;
	j1.dump(s1);
	j2.dump(s2);

	std::cout << "s1: " << s1 << "\n";
	std::cout << "s2: " << s2 << "\n";

	
	ojson patch = jsonpatch::from_diff(j1, j2);
	patch.dump(s3);

	std::cout << "patch: " << s3 << "\n";

	return 0;
}

this would raise the exception for comparing double to null here.

@danielaparker
Copy link
Owner

The issue really is with the basic_json compare function, not as_double(). For lossless numbers, the compare function assumes that the other side of the compare supports as_double(), that may not be the case. In should handle other types in the same way that it does for numbers represented as int64_t, uint64_t and double. I've fixed that on master, and your example should now work.

@MonkeybreadSoftware
Copy link
Contributor Author

Thank you.

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

Successfully merging this pull request may close these issues.

2 participants