未验证 提交 28c767dd 编写于 作者: Z Zhilkin Serg 提交者: GitHub

Merge pull request #54402 from akrieger/de_json_deserializer_serializer

Remove JsonSerializer/Deserializer, replace with JsonValue where appropriate.
......@@ -66,9 +66,8 @@
* and @ref JsonSerializer, which would grant them functions to serialize/deserialize. The Json
* classes will automatically use those if available, but they prefer the `io` function.
*
* Classes that only use the (templated) `io` function (and which do not inherit from
* JsonDeserializer and JsonSerializer), must announce that. This allows the Archive classes to
* avoid calling Json functions, which would not work.
* Classes that only use the (templated) `io` function must announce that.
* This allows the Archive classes to avoid calling Json functions, which would not work.
*
* If a class only uses the `io` function defined like this:
*
......@@ -127,7 +126,7 @@ struct enable_if_type {
/**
* Implementation for classes that don't have an archive_type_tag defined. They use the
* normal JsonSerializer / JsonDeserializer interface, which is handled directly by the Json
* normal json read/write interface, which is handled directly by the Json
* classes. Therefore the functions here simply forward to those.
*/
template<class T, class E = void>
......
......@@ -437,10 +437,12 @@ bool read_from_file_json( const std::string &path, const std::function<void( Jso
} );
}
bool read_from_file( const std::string &path, JsonDeserializer &reader )
bool read_from_file_json( const std::string &path,
const std::function<void( const JsonValue & )> &reader )
{
return read_from_file_json( path, [&reader]( JsonIn & jsin ) {
reader.deserialize( jsin );
return read_from_file( path, [&]( std::istream & fin ) {
JsonIn jsin( fin, path );
reader( jsin.get_value() );
} );
}
......@@ -462,10 +464,12 @@ bool read_from_file_optional_json( const std::string &path,
} );
}
bool read_from_file_optional( const std::string &path, JsonDeserializer &reader )
bool read_from_file_optional_json( const std::string &path,
const std::function<void( const JsonValue & )> &reader )
{
return read_from_file_optional_json( path, [&reader]( JsonIn & jsin ) {
reader.deserialize( jsin );
return read_from_file_optional( path, [&]( std::istream & fin ) {
JsonIn jsin( fin, path );
reader( jsin.get_value() );
} );
}
......
......@@ -19,6 +19,7 @@
class JsonIn;
class JsonOut;
class JsonValue;
class translation;
/**
......@@ -316,8 +317,6 @@ bool write_to_file( const std::string &path, const std::function<void( std::ostr
void write_to_file( const std::string &path, const std::function<void( std::ostream & )> &writer );
///@}
class JsonDeserializer;
/**
* Try to open and read from given file using the given callback.
*
......@@ -327,9 +326,8 @@ class JsonDeserializer;
* If the stream is in a fail state (other than EOF) after the callback returns, it is handled as
* error as well.
*
* The callback can either be a generic `std::istream`, a @ref JsonIn stream (which has been
* initialized from the `std::istream`) or a @ref JsonDeserializer object (in case of the later,
* it's `JsonDeserializer::deserialize` method will be invoked).
* The callback can either be a generic `std::istream` or a @ref JsonIn stream (which has been
* initialized from the `std::istream`) or a @ref JsonValue object.
*
* The functions with the "_optional" prefix do not show a debug message when the file does not
* exist. They simply ignore the call and return `false` immediately (without calling the callback).
......@@ -340,13 +338,15 @@ class JsonDeserializer;
/**@{*/
bool read_from_file( const std::string &path, const std::function<void( std::istream & )> &reader );
bool read_from_file_json( const std::string &path, const std::function<void( JsonIn & )> &reader );
bool read_from_file( const std::string &path, JsonDeserializer &reader );
bool read_from_file_json( const std::string &path,
const std::function<void( const JsonValue & )> &reader );
bool read_from_file_optional( const std::string &path,
const std::function<void( std::istream & )> &reader );
bool read_from_file_optional_json( const std::string &path,
const std::function<void( JsonIn & )> &reader );
bool read_from_file_optional( const std::string &path, JsonDeserializer &reader );
bool read_from_file_optional_json( const std::string &path,
const std::function<void( const JsonValue & )> &reader );
/**@}*/
std::istream &safe_getline( std::istream &ins, std::string &str );
......
......@@ -1707,22 +1707,6 @@ bool JsonIn::read( std::bitset<N> &b, bool throw_on_error )
return true;
}
bool JsonIn::read( JsonDeserializer &j, bool throw_on_error )
{
// can't know what type of json object it will deserialize from,
// so just try to deserialize, catching any error.
// TODO: non-verbose flag for JsonIn errors so try/catch is faster here
try {
j.deserialize( *this );
return true;
} catch( const JsonError & ) {
if( throw_on_error ) {
throw;
}
return false;
}
}
/**
* Get the normal form of a relative path. Does not work on absolute paths.
* Slash and backslash are both treated as path separators and replaced with
......@@ -2241,15 +2225,6 @@ void JsonOut::write( const std::bitset<N> &b )
need_separator = true;
}
void JsonOut::write( const JsonSerializer &thing )
{
if( need_separator ) {
write_separator();
}
thing.serialize( *this );
need_separator = true;
}
void JsonOut::member( const std::string &name )
{
write( name );
......
......@@ -25,22 +25,18 @@
/* Cataclysm-DDA homegrown JSON tools
* copyright CC-BY-SA-3.0 2013 CleverRaven
*
* Consists of six JSON manipulation tools:
* Consists of four JSON manipulation tools:
* JsonIn - for low-level parsing of an input JSON stream
* JsonOut - for outputting JSON
* JsonObject - convenience-wrapper for reading JSON objects from a JsonIn
* JsonArray - convenience-wrapper for reading JSON arrays from a JsonIn
* JsonSerializer - inheritable interface for custom datatype serialization
* JsonDeserializer - inheritable interface for custom datatype deserialization
*
* Further documentation can be found below.
*/
class JsonArray;
class JsonDeserializer;
class JsonIn;
class JsonObject;
class JsonSerializer;
class JsonValue;
class item;
......@@ -368,7 +364,6 @@ class JsonIn
bool read( std::string &s, bool throw_on_error = false );
template<size_t N>
bool read( std::bitset<N> &b, bool throw_on_error = false );
bool read( JsonDeserializer &j, bool throw_on_error = false );
template <typename T>
auto read( string_id<T> &thing, bool throw_on_error = false ) -> bool {
......@@ -731,7 +726,6 @@ class JsonIn
* which inserts newlines and whitespace liberally, if turned on.
*
* Basic containers such as maps, sets and vectors,
* as well as anything inheriting the JsonSerializer interface,
* can be serialized automatically by write() and member().
*/
class JsonOut
......@@ -820,8 +814,6 @@ class JsonOut
template<size_t N>
void write( const std::bitset<N> &b );
void write( const JsonSerializer &thing );
template <typename T>
auto write( const string_id<T> &thing ) {
write( thing.str() );
......@@ -1005,8 +997,8 @@ class JsonOut
* and for member existence with has_member(name).
*
* They can also read directly into compatible data structures,
* including sets, vectors, maps, and any class inheriting JsonDeserializer,
* using read(name, value).
* including sets, vectors, maps, and any class with a compatible
* deserialize() routine.
*
* read() returns true on success, false on failure.
*
......@@ -1224,12 +1216,12 @@ class JsonObject
* -------------------------
*
* Elements can also be automatically read into compatible containers,
* such as maps, sets, vectors, and classes implementing JsonDeserializer,
* such as maps, sets, vectors, and classes using a deserialize routine,
* using the read_next() and read() methods.
*
* JsonArray ja = jo.get_array("custom_datatype_array");
* while (ja.has_more()) {
* MyDataType mydata; // MyDataType implementing JsonDeserializer
* MyDataType mydata;
* ja.read_next(mydata);
* process(mydata);
* }
......@@ -1596,72 +1588,6 @@ Res JsonObject::get_tags( const std::string &name ) const
*/
void add_array_to_set( std::set<std::string> &, const JsonObject &json, const std::string &name );
/* JsonSerializer
* ==============
*
* JsonSerializer is an inheritable interface,
* allowing classes to define how they are to be serialized,
* and then treated as a basic type for serialization purposes.
*
* All a class must to do satisfy this interface,
* is define a `void serialize(JsonOut&) const` method,
* which should use the provided JsonOut to write its data as JSON.
*
* class point : public JsonSerializer {
* int x, y;
* void serialize(JsonOut &jsout) const {
* jsout.start_array();
* jsout.write(x);
* jsout.write(y);
* jsout.end_array();
* }
* }
*/
class JsonSerializer
{
public:
virtual ~JsonSerializer() = default;
virtual void serialize( JsonOut &jsout ) const = 0;
JsonSerializer() = default;
JsonSerializer( JsonSerializer && ) = default;
JsonSerializer( const JsonSerializer & ) = default;
JsonSerializer &operator=( JsonSerializer && ) = default;
JsonSerializer &operator=( const JsonSerializer & ) = default;
};
/* JsonDeserializer
* ==============
*
* JsonDeserializer is an inheritable interface,
* allowing classes to define how they are to be deserialized,
* and then treated as a basic type for deserialization purposes.
*
* All a class must to do satisfy this interface,
* is define a `void deserialize(JsonIn&)` method,
* which should read its data from the provided JsonIn,
* assuming it to be in the correct form.
*
* class point : public JsonDeserializer {
* int x, y;
* void deserialize(JsonIn &jsin) {
* JsonArray ja = jsin.get_array();
* x = ja.get_int(0);
* y = ja.get_int(1);
* }
* }
*/
class JsonDeserializer
{
public:
virtual ~JsonDeserializer() = default;
virtual void deserialize( JsonIn &jsin ) = 0;
JsonDeserializer() = default;
JsonDeserializer( JsonDeserializer && ) = default;
JsonDeserializer( const JsonDeserializer & ) = default;
JsonDeserializer &operator=( JsonDeserializer && ) = default;
JsonDeserializer &operator=( const JsonDeserializer & ) = default;
};
std::ostream &operator<<( std::ostream &stream, const JsonError &err );
template<typename T>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册