Skip to content

Commit

Permalink
jsonToCsv added.
Browse files Browse the repository at this point in the history
commit for second workspace environment management
  • Loading branch information
Ubuntu committed Jul 17, 2022
1 parent 86e95a3 commit 5de54e8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion data/datadefine/example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
[
"column_list": [
{
"name": "id",
"type": "int32",
Expand Down
Empty file added data/datagenerate/example.txt
Empty file.
Binary file removed source/generatecsv
Binary file not shown.
36 changes: 29 additions & 7 deletions source/generatecsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,34 @@ using namespace std;

bool jsonToCsv(shared_ptr<Json::Value> jsonInput, const char* input, const char* output)
{
Json::Reader jsonReader;
ifstream ifs(input);

// input file path validation check
if(ifs.is_open())
{
istream& istream = ifs;
if(false == jsonReader.parse(istream, *jsonInput))
{
cout << "[jsonToCsv Fail] jsonReader parse failed" << endl;
ifs.close();
return false;
}
}
else
{
cout << "[jsonToCsv Fail] no input file path : " << input << endl;
return false;
}

// close file io streams
ifs.close();
return true;
}

objects_t jsonToDicts(shared_ptr<Json::Value> jsonInput)
{
auto objects = objects_t();
shred_ptr<objects_t> objects = make_shared<objects_t>();
return objects;
}

Expand All @@ -23,25 +45,25 @@ shared_ptr<lines_t> dictsToCsv(objects_t o)

int main(int argc, char *argv[])
{
if(argc != 2)
if(argc != 3)
{
cout << "2 arguments required";
cout << "2 arguments required. argument count : " << argc << endl;
exit(1);
}

// json input file pointer
auto jsonInput = make_shared<Json::Value>();
shared_ptr<Json::Value> jsonInput = make_shared<Json::Value>();

auto ok = jsonToCsv(jsonInput, argv[1], argv[2]);
bool ok = jsonToCsv(jsonInput, argv[1], argv[2]);
if(!ok)
{
cout << "json to csv failed";
exit(1);
}

auto objects = jsonToDicts(jsonInput);
objects_t objects = jsonToDicts(jsonInput);

auto csv = dictsToCsv(objects);
shared_ptr<lines_t> csv = dictsToCsv(objects);

ofstream ofs;
ofs.open(argv[2]);
Expand Down
4 changes: 3 additions & 1 deletion source/generatecsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

using namespace std;

// type definition for json types
typedef std::vector<std::pair<string, string>> json_column;

typedef shared_ptr<json_column> object_t;
typedef shared_ptr<std::vector<object_t>> objects_t;
typedef vector<unique_ptr<string>> line_t;
typedef vector<shared_ptr<line_t>> lines_t;


// json convert functions
bool jsonToCsv(shared_ptr<Json::Value> jsonInput, const char* input, const char* output);
objects_t jsonToDicts(shared_ptr<Json::Value> jsonInput);
shared_ptr<lines_t> dictsToCsv(objects_t o);

0 comments on commit 5de54e8

Please sign in to comment.