Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON serialized storage of properties table.
- Loading branch information
1 parent
2724ce3
commit 37abfb3
Showing
22 changed files
with
496 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//============================================================================ | ||
// | ||
// SSSS tt lll lll | ||
// SS SS tt ll ll | ||
// SS tttttt eeee ll ll aaaa | ||
// SSSS tt ee ee ll ll aa | ||
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" | ||
// SS SS tt ee ll ll aa aa | ||
// SSSS ttt eeeee llll llll aaaaa | ||
// | ||
// Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony | ||
// and the Stella Team | ||
// | ||
// See the file "License.txt" for information on usage and redistribution of | ||
// this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
//============================================================================ | ||
|
||
#include <sstream> | ||
|
||
#include "CompositeKVRJsonAdapter.hxx" | ||
#include "repository/KeyValueRepositoryJsonFile.hxx" | ||
|
||
namespace { | ||
class ProxyRepository : public KeyValueRepository { | ||
public: | ||
ProxyRepository(KeyValueRepositoryAtomic& kvr, const string& key) | ||
: myKvr(kvr), myKey(key) | ||
{} | ||
|
||
std::map<string, Variant> load() override { | ||
if (!myKvr.has(myKey)) return std::map<string, Variant>(); | ||
|
||
string serialized; | ||
myKvr.get(myKey, serialized); | ||
|
||
stringstream in{serialized}; | ||
|
||
return KeyValueRepositoryJsonFile::load(in); | ||
} | ||
|
||
bool save(const std::map<string, Variant>& values) override { | ||
stringstream out; | ||
|
||
if (!KeyValueRepositoryJsonFile::save(out, values)) return false; | ||
|
||
return myKvr.save(myKey, out.str()); | ||
} | ||
|
||
private: | ||
|
||
KeyValueRepositoryAtomic& myKvr; | ||
const string& myKey; | ||
}; | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
CompositeKVRJsonAdapter::CompositeKVRJsonAdapter(KeyValueRepositoryAtomic& kvr) | ||
: myKvr(kvr) | ||
{} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
shared_ptr<KeyValueRepository> CompositeKVRJsonAdapter::get(const string& key) | ||
{ | ||
return make_shared<ProxyRepository>(myKvr, key); | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
bool CompositeKVRJsonAdapter::has(const string& key) | ||
{ | ||
return myKvr.has(key); | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
void CompositeKVRJsonAdapter::remove(const string& key) | ||
{ | ||
return myKvr.remove(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//============================================================================ | ||
// | ||
// SSSS tt lll lll | ||
// SS SS tt ll ll | ||
// SS tttttt eeee ll ll aaaa | ||
// SSSS tt ee ee ll ll aa | ||
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" | ||
// SS SS tt ee ll ll aa aa | ||
// SSSS ttt eeeee llll llll aaaaa | ||
// | ||
// Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony | ||
// and the Stella Team | ||
// | ||
// See the file "License.txt" for information on usage and redistribution of | ||
// this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
//============================================================================ | ||
|
||
#ifndef COMPOSITE_KVR_JSON_ADAPTER_HXX | ||
#define COMPOSITE_KVR_JSON_ADAPTER_HXX | ||
|
||
#include "repository/CompositeKeyValueRepository.hxx" | ||
#include "repository/KeyValueRepository.hxx" | ||
#include "bspf.hxx" | ||
|
||
class CompositeKVRJsonAdapter : public CompositeKeyValueRepository { | ||
public: | ||
|
||
CompositeKVRJsonAdapter(KeyValueRepositoryAtomic& kvr); | ||
|
||
shared_ptr<KeyValueRepository> get(const string& key) override; | ||
|
||
bool has(const string& key) override; | ||
|
||
void remove(const string& key) override; | ||
|
||
private: | ||
|
||
KeyValueRepositoryAtomic& myKvr; | ||
}; | ||
|
||
#endif // COMPOSITE_KVR_JSON_ADAPTER_HXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//============================================================================ | ||
// | ||
// SSSS tt lll lll | ||
// SS SS tt ll ll | ||
// SS tttttt eeee ll ll aaaa | ||
// SSSS tt ee ee ll ll aa | ||
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" | ||
// SS SS tt ee ll ll aa aa | ||
// SSSS ttt eeeee llll llll aaaaa | ||
// | ||
// Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony | ||
// and the Stella Team | ||
// | ||
// See the file "License.txt" for information on usage and redistribution of | ||
// this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
//============================================================================ | ||
|
||
#ifndef KEY_VALUE_REPOSITORY_FILE_HXX | ||
#define KEY_VALUE_REPOSITORY_FILE_HXX | ||
|
||
#include <sstream> | ||
|
||
#include "KeyValueRepository.hxx" | ||
#include "Logger.hxx" | ||
#include "FSNode.hxx" | ||
#include "bspf.hxx" | ||
|
||
template<class T> | ||
class KeyValueRepositoryFile : public KeyValueRepository { | ||
public: | ||
KeyValueRepositoryFile(const FilesystemNode& node); | ||
|
||
std::map<string, Variant> load() override; | ||
|
||
bool save(const std::map<string, Variant>& values) override; | ||
|
||
protected: | ||
|
||
const FilesystemNode& myNode; | ||
}; | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
// IMPLEMEMNTATION | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
template<class T> | ||
KeyValueRepositoryFile<T>::KeyValueRepositoryFile(const FilesystemNode& node) | ||
: myNode(node) | ||
{} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
template<class T> | ||
std::map<string, Variant> KeyValueRepositoryFile<T>::load() | ||
{ | ||
if (!myNode.exists()) return std::map<string, Variant>(); | ||
|
||
stringstream in; | ||
|
||
try { | ||
myNode.read(in); | ||
return T::load(in); | ||
} | ||
catch (const runtime_error& err) { | ||
Logger::error(err.what()); | ||
|
||
return std::map<string, Variant>(); | ||
} | ||
catch (...) { | ||
return std::map<string, Variant>(); | ||
} | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
template<class T> | ||
bool KeyValueRepositoryFile<T>::save(const std::map<string, Variant>& values) | ||
{ | ||
if (values.size() == 0) return true; | ||
|
||
stringstream out; | ||
|
||
try { | ||
T::save(out, values); | ||
myNode.write(out); | ||
|
||
return true; | ||
} | ||
catch (const runtime_error& err) { | ||
Logger::error(err.what()); | ||
|
||
return false; | ||
} | ||
catch (...) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
#endif // KEY_VALUE_REPOSITORY_FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//============================================================================ | ||
// | ||
// SSSS tt lll lll | ||
// SS SS tt ll ll | ||
// SS tttttt eeee ll ll aaaa | ||
// SSSS tt ee ee ll ll aa | ||
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" | ||
// SS SS tt ee ll ll aa aa | ||
// SSSS ttt eeeee llll llll aaaaa | ||
// | ||
// Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony | ||
// and the Stella Team | ||
// | ||
// See the file "License.txt" for information on usage and redistribution of | ||
// this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
//============================================================================ | ||
|
||
#include "repository/KeyValueRepositoryJsonFile.hxx" | ||
#include "Logger.hxx" | ||
#include "json_lib.hxx" | ||
|
||
using nlohmann::json; | ||
|
||
namespace { | ||
json jsonIfValid(const string& s) { | ||
json parsed = json::parse(s, nullptr, false); | ||
|
||
return parsed.is_discarded() ? json(s) : parsed; | ||
} | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
KeyValueRepositoryJsonFile::KeyValueRepositoryJsonFile(const FilesystemNode& node) | ||
: KeyValueRepositoryFile<KeyValueRepositoryJsonFile>(node) | ||
{} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
std::map<string, Variant> KeyValueRepositoryJsonFile::load(istream& in) | ||
{ | ||
try { | ||
std::map<string, Variant> map; | ||
|
||
json deserialized = json::parse(in); | ||
|
||
if (!deserialized.is_object()) { | ||
Logger::error("KeyVallueRepositoryJsonFile: not an object"); | ||
|
||
return map; | ||
} | ||
|
||
for (const auto& [key, value] : deserialized.items()) | ||
map[key] = value.is_string() ? value.get<string>() : value.dump(); | ||
|
||
return map; | ||
} | ||
catch (const json::exception& err) { | ||
Logger::error("KeyValueRepositoryJsonFile: error during deserialization: " + string(err.what())); | ||
|
||
return std::map<string, Variant>(); | ||
} | ||
} | ||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
bool KeyValueRepositoryJsonFile::save(ostream& out, const std::map<string, Variant>& values) | ||
{ | ||
try { | ||
json serializedJson = json::object(); | ||
|
||
for (const auto& [key, value] : values) | ||
serializedJson[key] = jsonIfValid(value.toString()); | ||
|
||
out << serializedJson.dump(2); | ||
|
||
return true; | ||
} | ||
catch (const json::exception& err) { | ||
Logger::error("KeyValueRepositoryJsonFile: error during serialization: " + string(err.what())); | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.