Better JSON type names.

GitOrigin-RevId: 1d40b9f39a0850040820b90fcf874b42468edd2d
This commit is contained in:
levlam 2018-05-20 15:30:36 +03:00
parent 82df6d7625
commit 004e562d0f
3 changed files with 21 additions and 20 deletions

View File

@ -22,7 +22,7 @@ Result<Client::Request> 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);

View File

@ -65,7 +65,7 @@ void to_json(JsonValueScope &jv, const std::vector<T> &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<int32>(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<int64>(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 <class T>
Status from_json(std::vector<T> &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<T>(from.get_array().size());
size_t i = 0;
@ -157,7 +158,7 @@ std::enable_if_t<!std::is_constructible<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<!std::is_constructible<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<T> helper(constructor);
@ -194,7 +195,7 @@ std::enable_if_t<std::is_constructible<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<T>();
return from_json(*to, from.get_object());

View File

@ -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;