CmdImport: JSON parse errors are assumed to imply old-style format
This commit is contained in:
@@ -68,7 +68,7 @@ int CmdImport::execute (std::string& output)
|
|||||||
std::string json;
|
std::string json;
|
||||||
std::string line;
|
std::string line;
|
||||||
while (std::getline (std::cin, line))
|
while (std::getline (std::cin, line))
|
||||||
json += line + " ";
|
json += line + "\n";
|
||||||
|
|
||||||
if (nontrivial (json))
|
if (nontrivial (json))
|
||||||
count = import (json);
|
count = import (json);
|
||||||
@@ -100,41 +100,69 @@ int CmdImport::execute (std::string& output)
|
|||||||
int CmdImport::import (const std::string& input)
|
int CmdImport::import (const std::string& input)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
json::value* root = json::parse (input);
|
try
|
||||||
|
|
||||||
// Single object parse. Input looks like:
|
|
||||||
// { ... }
|
|
||||||
if (root->type () == json::j_object)
|
|
||||||
{
|
{
|
||||||
// For each object element...
|
json::value* root = json::parse (input);
|
||||||
json::object* root_obj = (json::object*)root;
|
if (root)
|
||||||
if (root_obj)
|
|
||||||
{
|
{
|
||||||
importSingleTask (root_obj);
|
// Single object parse. Input looks like:
|
||||||
++count;
|
// { ... }
|
||||||
|
if (root->type () == json::j_object)
|
||||||
|
{
|
||||||
|
// For each object element...
|
||||||
|
json::object* root_obj = (json::object*)root;
|
||||||
|
importSingleTask (root_obj);
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiple object array. Input looks like:
|
||||||
|
// [ { ... } , { ... } ]
|
||||||
|
else if (root->type () == json::j_array)
|
||||||
|
{
|
||||||
|
json::array* root_arr = (json::array*)root;
|
||||||
|
|
||||||
|
// For each object element...
|
||||||
|
for (auto& element : root_arr->_data)
|
||||||
|
{
|
||||||
|
// For each object element...
|
||||||
|
json::object* root_obj = (json::object*)element;
|
||||||
|
importSingleTask (root_obj);
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiple object array. Input looks like:
|
// If an exception is caught, then it is because the free-form JSON
|
||||||
// [ { ... } , { ... } ]
|
// objects/array above failed to parse. This is an indication that the input
|
||||||
else if (root->type () == json::j_array)
|
// is an old-style line-by-line set of JSON objects, because both an array of
|
||||||
|
// objects, and a single object have failed to parse..
|
||||||
|
//
|
||||||
|
// Input looks like:
|
||||||
|
// { ... }
|
||||||
|
// { ... }
|
||||||
|
catch (std::string& e)
|
||||||
{
|
{
|
||||||
json::array* root_arr = (json::array*)root;
|
std::vector <std::string> lines;
|
||||||
|
split (lines, input, '\n');
|
||||||
|
|
||||||
// For each object element...
|
for (auto& line : lines)
|
||||||
for (auto& element : root_arr->_data)
|
|
||||||
{
|
{
|
||||||
// For each object element...
|
if (line.length ())
|
||||||
json::object* root_obj = (json::object*)element;
|
|
||||||
if (root_obj)
|
|
||||||
{
|
{
|
||||||
importSingleTask (root_obj);
|
json::value* root = json::parse (line);
|
||||||
++count;
|
if (root)
|
||||||
|
{
|
||||||
|
importSingleTask ((json::object*) root);
|
||||||
|
++count;
|
||||||
|
delete root;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete root;
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user