From 004e562d0f418c6252f9eeb017dd6879dbd3e95c Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 20 May 2018 15:30:36 +0300 Subject: [PATCH] Better JSON type names. GitOrigin-RevId: 1d40b9f39a0850040820b90fcf874b42468edd2d --- td/telegram/ClientJson.cpp | 2 +- td/tl/tl_json.h | 21 +++++++++++---------- tdutils/td/utils/JsonBuilder.h | 18 +++++++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/td/telegram/ClientJson.cpp b/td/telegram/ClientJson.cpp index 13dd81c4..9e3cbc22 100644 --- a/td/telegram/ClientJson.cpp +++ b/td/telegram/ClientJson.cpp @@ -22,7 +22,7 @@ Result ClientJson::to_request(Slice request) { auto request_str = request.str(); TRY_RESULT(json_value, json_decode(request_str)); if (json_value.type() != JsonValue::Type::Object) { - return Status::Error("Expected an object"); + return Status::Error("Expected an Object"); } TRY_RESULT(extra_field, get_json_object_field(json_value.get_object(), "@extra", JsonValue::Type::Null, true)); std::uint64_t extra_id = extra_id_.fetch_add(1, std::memory_order_relaxed); diff --git a/td/tl/tl_json.h b/td/tl/tl_json.h index 54d9b7fe..a28e8046 100644 --- a/td/tl/tl_json.h +++ b/td/tl/tl_json.h @@ -65,7 +65,7 @@ void to_json(JsonValueScope &jv, const std::vector &v) { inline Status from_json(int32 &to, JsonValue &from) { if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) { - return Status::Error(PSLICE() << "Expected number, got " << from.type()); + return Status::Error(PSLICE() << "Expected Number, got " << from.type()); } Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); TRY_RESULT(res, to_integer_safe(number)); @@ -81,7 +81,7 @@ inline Status from_json(bool &to, JsonValue &from) { to = x != 0; return Status::OK(); } - return Status::Error(PSLICE() << "Expected bool, got " << from.type()); + return Status::Error(PSLICE() << "Expected Boolean, got " << from.type()); } to = from.get_boolean(); return Status::OK(); @@ -89,16 +89,17 @@ inline Status from_json(bool &to, JsonValue &from) { inline Status from_json(int64 &to, JsonValue &from) { if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) { - return Status::Error(PSLICE() << "Expected number, got " << from.type()); + return Status::Error(PSLICE() << "Expected String or Number, got " << from.type()); } Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); TRY_RESULT(res, to_integer_safe(number)); to = res; return Status::OK(); } + inline Status from_json(double &to, JsonValue &from) { if (from.type() != JsonValue::Type::Number) { - return Status::Error(PSLICE() << "Expected number, got " << from.type()); + return Status::Error(PSLICE() << "Expected Number, got " << from.type()); } to = to_double(from.get_number()); return Status::OK(); @@ -106,7 +107,7 @@ inline Status from_json(double &to, JsonValue &from) { inline Status from_json(string &to, JsonValue &from) { if (from.type() != JsonValue::Type::String) { - return Status::Error(PSLICE() << "Expected string, got " << from.type()); + return Status::Error(PSLICE() << "Expected String, got " << from.type()); } to = from.get_string().str(); return Status::OK(); @@ -114,7 +115,7 @@ inline Status from_json(string &to, JsonValue &from) { inline Status from_json_bytes(string &to, JsonValue &from) { if (from.type() != JsonValue::Type::String) { - return Status::Error(PSLICE() << "Expected string, got " << from.type()); + return Status::Error(PSLICE() << "Expected String, got " << from.type()); } TRY_RESULT(decoded, base64_decode(from.get_string())); to = std::move(decoded); @@ -124,7 +125,7 @@ inline Status from_json_bytes(string &to, JsonValue &from) { template Status from_json(std::vector &to, JsonValue &from) { if (from.type() != JsonValue::Type::Array) { - return Status::Error(PSLICE() << "Expected array, got " << from.type()); + return Status::Error(PSLICE() << "Expected Array, got " << from.type()); } to = std::vector(from.get_array().size()); size_t i = 0; @@ -157,7 +158,7 @@ std::enable_if_t::value, Status> from_json(tl_object_p to = nullptr; return Status::OK(); } - return Status::Error(PSLICE() << "Expected object, got " << from.type()); + return Status::Error(PSLICE() << "Expected Object, got " << from.type()); } auto &object = from.get_object(); @@ -169,7 +170,7 @@ std::enable_if_t::value, Status> from_json(tl_object_p TRY_RESULT(t_constructor, tl_constructor_from_string(to.get(), constructor_value.get_string().str())); constructor = t_constructor; } else { - return Status::Error(PSLICE() << "Expected string or int, got " << constructor_value.type()); + return Status::Error(PSLICE() << "Expected String or Integer, got " << constructor_value.type()); } DowncastHelper helper(constructor); @@ -194,7 +195,7 @@ std::enable_if_t::value, Status> from_json(tl_object_pt to = nullptr; return Status::OK(); } - return Status::Error(PSLICE() << "Expected object, got " << from.type()); + return Status::Error(PSLICE() << "Expected Object, got " << from.type()); } to = make_tl_object(); return from_json(*to, from.get_object()); diff --git a/tdutils/td/utils/JsonBuilder.h b/tdutils/td/utils/JsonBuilder.h index dde78dff..b6eb12c9 100644 --- a/tdutils/td/utils/JsonBuilder.h +++ b/tdutils/td/utils/JsonBuilder.h @@ -653,18 +653,18 @@ class JsonValue : public Jsonable { inline StringBuilder &operator<<(StringBuilder &sb, JsonValue::Type type) { switch (type) { - case JsonValue::Type::Object: - return sb << "JsonObject"; - case JsonValue::Type::Boolean: - return sb << "JsonBoolean"; case JsonValue::Type::Null: - return sb << "JsonNull"; + return sb << "Null"; case JsonValue::Type::Number: - return sb << "JsonNumber"; - case JsonValue::Type::Array: - return sb << "JsonArray"; + return sb << "Number"; + case JsonValue::Type::Boolean: + return sb << "Boolean"; case JsonValue::Type::String: - return sb << "JsonString"; + return sb << "String"; + case JsonValue::Type::Array: + return sb << "Array"; + case JsonValue::Type::Object: + return sb << "Object"; default: UNREACHABLE(); return sb;