private function parseJson($encoded, array $template = null)
{
// content is optional if template is provided
if (!$encoded && null !== $template) {
$decoded = array();
} else {
$decoded = json_decode($encoded, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf('Malformed JSON: "%s".', $encoded));
}
}
if (null !== $template) {
$decoded = array_merge($template, array_intersect_key($decoded, array_flip(array_keys($template))));
}
return $decoded;
}