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

Better json::null handling for loops #27

Closed
ludekvodicka opened this issue Mar 17, 2018 · 1 comment
Closed

Better json::null handling for loops #27

ludekvodicka opened this issue Mar 17, 2018 · 1 comment

Comments

@ludekvodicka
Copy link
Contributor

Currently, when json element is empty and we're trying to iterate it application throws the error.

I believe that in this situations loop should be skipped but not stopped.

Fix is pretty easy, it's necessary to test for null in eval_expression function

	template<typename T = json>
  T eval_expression(const Parsed::ElementExpression& element, const json& data) {
		const json var = eval_function(element, data);
		if (var.is_null())            // << test if element is empty
			return T();           // << return empty

		try {
			return var.get<T>();
		} catch (json::type_error& e) {
			inja_throw("json_error", e.what());
			throw;
		}
	}

The use case is that I'm filling database data to the json context and generating some stats from such data. But when array of data is empty the generator throw error instead of generating empty data.

@pantor
Copy link
Owner

pantor commented Mar 19, 2018

Fixed with the latest commits.

@pantor pantor closed this as completed Mar 19, 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