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 with printing json response object #1356

Closed
DragonOsman opened this issue Nov 14, 2018 · 1 comment
Closed

Problem with printing json response object #1356

DragonOsman opened this issue Nov 14, 2018 · 1 comment

Comments

@DragonOsman
Copy link

DragonOsman commented Nov 14, 2018

I need to print out my JSON response to see what the problem is that's causing me to get the exception

[json.exception.type_error.302] type must be number, but is null

My code is on GitHub here. I'm currently trying to make it completely asynchronous so I can port it to WebAssembly (though I'll also need to port the ASIO and Beast libraries).

The exception came from here:

// Perform currency conversion
double client_session::convert(const std::string &from_currency, const std::string &to_currency, const double money_amount, const char *accesskey)
{
	std::vector<std::string> currencies{
		"AED","AFN","ALL","AMD","ANG","AOA","ARS","AUD","AWG","AZN","BAM","BBD","BDT","BGN","BHD","BIF","BMD","BND","BOB","BRL","BSD","BTC","BTN","BWP",
		"BYN","BYR","BZD","CAD","CDF","CHF","CLF","CLP","CNY","COP","CRC","CUC","CUP","CVE","CZK","DJF","DKK","DOP","DZD","EGP","ERN","ETB","EUR","FJD",
		"FKP","GBP","GEL","GGP","GHS","GIP","GMD","GNF","GTQ","GYD","HKD","HNL","HRK","HTG","HUF","IDR","ILS","IMP","INR","IQD","IRR","ISK","JEP","JMD",
		"JOD","JPY","KES","KGS","KHR","KMF","KPW","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LTL","LVL","LYD","MAD","MDL","MGA","MKD","MMK",
		"MNT","MOP","MRO","MUR","MVR","MWK","MXN","MYR","MZN","NAD","NGN","NIO","NOK","NPR","NZD","OMR","PAB","PEN","PGK","PHP","PKR","PLN","PYG","QAR",
		"RON","RSD","RUB","RWF","SAR","SBD","SCR","SDG","SEK","SGD","SHP","SLL","SOS","SRD","STD","SVC","SYP","SZL","THB","TJS","TMT","TND","TOP","TRY",
		"TTD","TWD","TZS","UAH","UGX","USD","UYU","UZS","VEF","VND","VUV","WST","XAF","XAG","XAU","XCD","XDR","XOF","XPF","YER","ZAR","ZMK","ZMW","ZWL"
	};

	query_data = { std::make_pair("from_currency", from_currency), std::make_pair("to_currency", to_currency) };
	found = cache.get_cache().find(query_data);

	json j_res = cache.query(query_data, accesskey);

	for (const auto &v : query_data)
	{
		std::cout << v.first << ": " << v.second << '\n';
	}
	for (const auto &x : j_res)
	{
		std::cout << x << '\n';
	}

	double result = 0, rate = 0;

	try
	{
		for (const auto &x : j_res)
		{
			std::cout << x << '\n';
		}
		rate = j_res["quotes"][from_currency + to_currency].get<double>();
	}
	catch (const json::exception &e)
	{
		std::cerr << "Line 895: Error: " << e.what() << '\n';
	}

	if (std::find(currencies.begin(), currencies.end(), to_currency) != currencies.end() &&
		std::find(currencies.begin(), currencies.end(), from_currency) != currencies.end())
	{
		result = money_amount * rate;
	}

	return result;
}

The std::cerr << "Line 895: Error: " << e.what() << '\n'; line is where the output is from.

I tried to make the HTTP GET request I'm trying to make in the code manually and I got back the JSON response I expected:

{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1542208746,
  "source":"USD",
  "quotes":{
    "USDPKR":134.000093
  }
}

But for some reason, when I do it in this code, I get either an exception about the type being null or an exception about not being able to use operator[] with a string as the key with a number. And as you can see in my code, I also tried to print out the j_res object. Nothing from it gets printed, though. This is my output:

in cache_storage::query, query_data is:
from_currency: USD
to_currency: PKR
Line 929: target is: /api/live?access_key=6118470d67d40caf1f4db130ec67e1e8&currencies=PKR&format=1
hi from line 943 in cache_storage::query
from_currency: USD
to_currency: PKR
Line 895: Error: [json.exception.type_error.302] type must be number, but is null

And the problem is causing both the conversion rate and conversion result to come out to be 0. I'd really like some help here. Thanks in advance.

@DragonOsman
Copy link
Author

Okay, I managed to solve it by making cache_storage::get_cache return a reference. I've also replaced the code in currency_converter.cpp with the async version and made it the only .cpp file in the repository. currency_converter_async.cpp is no longer there.

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

1 participant