diff --git a/src/indexer.h b/src/indexer.h index d1478f71..f7e3ab35 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -154,7 +154,8 @@ inline bool operator!=(const IndexFuncRef& a, const IndexFuncRef& b) { } inline void Reflect(Reader& visitor, IndexFuncRef& value) { - const char* str_value = visitor.GetCString(); + std::string s = visitor.GetString(); + const char* str_value = s.c_str(); if (str_value[0] == '~') { value.is_implicit = true; ++str_value; diff --git a/src/position.cc b/src/position.cc index 71099336..ee0f9e46 100644 --- a/src/position.cc +++ b/src/position.cc @@ -148,8 +148,10 @@ bool Range::operator<(const Range& that) const { void Reflect(Reader& visitor, Position& value) { if (!visitor.IsString()) value = Position(); - else - value = Position(visitor.GetCString()); + else { + std::string s = visitor.GetString(); + value = Position(s.c_str()); + } } void Reflect(Writer& visitor, Position& value) { std::string output = value.ToString(); @@ -160,8 +162,10 @@ void Reflect(Writer& visitor, Position& value) { void Reflect(Reader& visitor, Range& value) { if (!visitor.IsString()) value = Range(); - else - value = Range(visitor.GetCString()); + else { + std::string s = visitor.GetString(); + value = Range(s.c_str()); + } } void Reflect(Writer& visitor, Range& value) { std::string output = value.ToString(); diff --git a/src/serializer.cc b/src/serializer.cc index 37fe8831..d684c6fc 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -6,6 +6,7 @@ #include "indexer.h" #include +#include namespace { bool gTestOutputMode = false; @@ -252,7 +253,8 @@ std::unique_ptr Deserialize(SerializeFormat format, Reflect(reader, *file); if (file->version != expected_version) return nullptr; - } catch (msgpack::insufficient_bytes&) { + } catch (msgpack::unpack_error& ex) { + LOG_S(ERROR) << "msgpack::unpack_err for '" << path << "' " << ex.what(); return nullptr; } break; diff --git a/src/serializer.h b/src/serializer.h index 95592872..b8543c60 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -27,8 +27,7 @@ class Reader { virtual int GetInt() = 0; virtual int64_t GetInt64() = 0; virtual uint64_t GetUint64() = 0; - virtual const char* GetCString() = 0; - virtual std::string GetString() { return GetCString(); } + virtual std::string GetString() = 0; virtual bool HasMember(const char* x) = 0; virtual std::unique_ptr operator[](const char* x) = 0; @@ -163,8 +162,12 @@ void Reflect(Reader& visitor, optional& value) { } template void Reflect(Writer& visitor, optional& value) { + // We omit optional fields for JsonWriter to reduce output. + // But don't omit them for other serialization formats. if (value) - Reflect(visitor, value.value()); + Reflect(visitor, *value); + else if (visitor.Format() != SerializeFormat::Json) + visitor.Null(); } // std::variant (Writer only) @@ -179,8 +182,6 @@ void Reflect(Writer& visitor, std::variant& value) { // std::vector template void Reflect(Reader& visitor, std::vector& values) { - if (!visitor.IsArray()) - return; visitor.IterArray([&](Reader& entry) { T entry_value; Reflect(entry, entry_value); @@ -224,10 +225,10 @@ void ReflectMember(Writer& visitor, const char* name, std::vector& values) { } template void ReflectMember(Writer& visitor, const char* name, optional& value) { - if (!value) - return; - visitor.Key(name); - Reflect(visitor, value); + if (value || visitor.Format() != SerializeFormat::Json) { + visitor.Key(name); + Reflect(visitor, value); + } } void ReflectMember(Writer& visitor, const char* name, std::string& value); diff --git a/src/serializers/json.h b/src/serializers/json.h index 6707322a..8cb92bc8 100644 --- a/src/serializers/json.h +++ b/src/serializers/json.h @@ -24,7 +24,7 @@ class JsonReader : public Reader { int GetInt() override { return m_->GetInt(); } int64_t GetInt64() override { return m_->GetInt64(); } uint64_t GetUint64() override { return m_->GetUint64(); } - const char* GetCString() override { return m_->GetString(); } + std::string GetString() override { return m_->GetString(); } bool HasMember(const char* x) override { return m_->HasMember(x); } std::unique_ptr operator[](const char* x) override { diff --git a/src/serializers/msgpack.h b/src/serializers/msgpack.h index 3f05f71f..c2468e8a 100644 --- a/src/serializers/msgpack.h +++ b/src/serializers/msgpack.h @@ -25,7 +25,7 @@ class MessagePackReader : public Reader { int GetInt() override { return o_.as(); } int64_t GetInt64() override { return o_.as(); } uint64_t GetUint64() override { return o_.as(); } - const char* GetCString() override { return o_.as(); } + std::string GetString() override { return o_.as(); } bool HasMember(const char* x) override { return true; } std::unique_ptr operator[](const char* x) override {