2011-03-17 01:47:30 -04:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "JsonUtil.h"
|
|
|
|
|
#include "RageFile.h"
|
|
|
|
|
#include "RageUtil.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "arch/Dialog/Dialog.h"
|
2022-02-19 12:42:13 +01:00
|
|
|
#include "json/json.h"
|
2011-03-17 01:47:30 -04:00
|
|
|
|
2023-04-20 19:02:13 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
2011-03-17 01:47:30 -04:00
|
|
|
bool JsonUtil::LoadFromString(Json::Value &root, RString sData, RString &sErrorOut)
|
|
|
|
|
{
|
|
|
|
|
Json::Reader reader;
|
|
|
|
|
bool parsingSuccessful = reader.parse(sData, root);
|
|
|
|
|
if (!parsingSuccessful)
|
|
|
|
|
{
|
2022-02-19 12:42:13 +01:00
|
|
|
RString err = reader.getFormattedErrorMessages();
|
2011-06-12 03:12:44 -04:00
|
|
|
LOG->Warn("JSON: LoadFromFileShowErrors failed: %s", err.c_str());
|
2011-03-17 01:47:30 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JsonUtil::LoadFromFileShowErrors(Json::Value &root, RageFileBasic &f)
|
|
|
|
|
{
|
|
|
|
|
// Optimization opportunity: read this streaming instead of at once
|
|
|
|
|
RString sData;
|
|
|
|
|
f.Read(sData, f.GetFileSize());
|
|
|
|
|
return LoadFromStringShowErrors(root, sData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JsonUtil::LoadFromFileShowErrors(Json::Value &root, const RString &sFile)
|
|
|
|
|
{
|
|
|
|
|
RageFile f;
|
|
|
|
|
if(!f.Open(sFile, RageFile::READ))
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Couldn't open %s for reading: %s", sFile.c_str(), f.GetError().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LoadFromFileShowErrors(root, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JsonUtil::LoadFromStringShowErrors(Json::Value &root, RString sData)
|
|
|
|
|
{
|
|
|
|
|
RString sError;
|
|
|
|
|
if(!LoadFromString(root, sData, sError))
|
|
|
|
|
{
|
|
|
|
|
Dialog::OK(sError, "JSON_PARSE_ERROR");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JsonUtil::WriteFile(const Json::Value &root, const RString &sFile, bool bMinified)
|
|
|
|
|
{
|
|
|
|
|
std::string s;
|
|
|
|
|
if(!bMinified)
|
|
|
|
|
{
|
|
|
|
|
Json::StyledWriter writer;
|
|
|
|
|
s = writer.write(root);
|
|
|
|
|
}
|
2023-04-20 19:02:13 +02:00
|
|
|
else
|
2011-03-17 01:47:30 -04:00
|
|
|
{
|
|
|
|
|
Json::FastWriter writer;
|
|
|
|
|
s = writer.write(root);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageFile f;
|
|
|
|
|
if(!f.Open(sFile, RageFile::WRITE))
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn("Couldn't open %s for reading: %s", sFile.c_str(), f.GetError().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
f.Write(s);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 20:56:40 +02:00
|
|
|
std::vector<RString> JsonUtil::DeserializeArrayStrings(const Json::Value &value)
|
|
|
|
|
{
|
|
|
|
|
std::vector<RString> values;
|
|
|
|
|
for(auto &&inner_value : value)
|
|
|
|
|
{
|
|
|
|
|
if(inner_value.isConvertibleTo(Json::stringValue))
|
|
|
|
|
{
|
|
|
|
|
values.push_back(inner_value.asString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-17 01:47:30 -04:00
|
|
|
/*
|
|
|
|
|
* (c) 2001-2004 Chris Danford
|
|
|
|
|
* All rights reserved.
|
2023-04-20 19:02:13 +02:00
|
|
|
*
|
2011-03-17 01:47:30 -04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
2023-04-20 19:02:13 +02:00
|
|
|
*
|
2011-03-17 01:47:30 -04:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|