Commit 7b92371a authored by Branislav Katreniak's avatar Branislav Katreniak

json: address review comments 2

parent 15453393
...@@ -323,7 +323,7 @@ KJ_TEST("decode test message") { ...@@ -323,7 +323,7 @@ KJ_TEST("decode test message") {
auto decodedRoot = decodedMessage.initRoot<TestAllTypes>(); auto decodedRoot = decodedMessage.initRoot<TestAllTypes>();
json.decode(encoded, decodedRoot); json.decode(encoded, decodedRoot);
//json encode serializes nan, inf and -inf as null. // json encode serializes nan, inf and -inf as null.
auto float32List = decodedRoot.getFloat32List(); auto float32List = decodedRoot.getFloat32List();
auto float64List = decodedRoot.getFloat64List(); auto float64List = decodedRoot.getFloat64List();
KJ_EXPECT(kj::isNaN(float32List[1])); KJ_EXPECT(kj::isNaN(float32List[1]));
......
...@@ -409,14 +409,16 @@ double parseFloat64(kj::StringPtr s) { ...@@ -409,14 +409,16 @@ double parseFloat64(kj::StringPtr s) {
template <typename SetFn, typename DecodeArrayFn, typename DecodeObjectFn> template <typename SetFn, typename DecodeArrayFn, typename DecodeObjectFn>
void decodeField(Type type, JsonValue::Reader value, SetFn setFn, DecodeArrayFn decodeArrayFn, void decodeField(Type type, JsonValue::Reader value, SetFn setFn, DecodeArrayFn decodeArrayFn,
DecodeObjectFn decodeObjectFn) { DecodeObjectFn decodeObjectFn) {
//This code relies on conversions in DynamicValue::Reader::as<T>. // This code relies on conversions in DynamicValue::Reader::as<T>.
switch(type.which()) { switch(type.which()) {
case schema::Type::VOID: case schema::Type::VOID:
break; break;
case schema::Type::BOOL: case schema::Type::BOOL:
if (value.isBoolean()) { switch (value.which()) {
case JsonValue::BOOLEAN:
setFn(value.getBoolean()); setFn(value.getBoolean());
} else { break;
default:
KJ_FAIL_REQUIRE("Expected boolean value"); KJ_FAIL_REQUIRE("Expected boolean value");
} }
break; break;
...@@ -424,88 +426,112 @@ void decodeField(Type type, JsonValue::Reader value, SetFn setFn, DecodeArrayFn ...@@ -424,88 +426,112 @@ void decodeField(Type type, JsonValue::Reader value, SetFn setFn, DecodeArrayFn
case schema::Type::INT16: case schema::Type::INT16:
case schema::Type::INT32: case schema::Type::INT32:
case schema::Type::INT64: case schema::Type::INT64:
//Rellies on range check in DynamicValue::Reader::as<IntType> // Relies on range check in DynamicValue::Reader::as<IntType>
if (value.isNumber()) { switch (value.which()) {
case JsonValue::NUMBER:
setFn(value.getNumber()); setFn(value.getNumber());
} else if (value.isString()) { break;
case JsonValue::STRING:
setFn(parseInt64(value.getString())); setFn(parseInt64(value.getString()));
} else { break;
KJ_FAIL_REQUIRE("Expected numeric value"); default:
KJ_FAIL_REQUIRE("Expected integer value");
} }
break; break;
case schema::Type::UINT8: case schema::Type::UINT8:
case schema::Type::UINT16: case schema::Type::UINT16:
case schema::Type::UINT32: case schema::Type::UINT32:
case schema::Type::UINT64: case schema::Type::UINT64:
//Rellies on range check in DynamicValue::Reader::as<IntType> // Relies on range check in DynamicValue::Reader::as<IntType>
if (value.isNumber()) { switch (value.which()) {
case JsonValue::NUMBER:
setFn(value.getNumber()); setFn(value.getNumber());
} else if (value.isString()) { break;
case JsonValue::STRING:
setFn(parseUInt64(value.getString())); setFn(parseUInt64(value.getString()));
} else { break;
KJ_FAIL_REQUIRE("Expected numeric value"); default:
KJ_FAIL_REQUIRE("Expected integer value");
} }
break; break;
case schema::Type::FLOAT32: case schema::Type::FLOAT32:
case schema::Type::FLOAT64: case schema::Type::FLOAT64:
if (value.isNull()) { switch (value.which()) {
case JsonValue::NULL_:
setFn(kj::nan()); setFn(kj::nan());
} else if (value.isNumber()) { break;
case JsonValue::NUMBER:
setFn(value.getNumber()); setFn(value.getNumber());
} else if (value.isString()) { break;
case JsonValue::STRING:
setFn(parseFloat64(value.getString())); setFn(parseFloat64(value.getString()));
} else { break;
KJ_FAIL_REQUIRE("Expected numeric value"); default:
KJ_FAIL_REQUIRE("Expected float value");
} }
break; break;
case schema::Type::TEXT: case schema::Type::TEXT:
if (value.isString()) { switch (value.which()) {
case JsonValue::STRING:
setFn(value.getString()); setFn(value.getString());
} else { break;
KJ_FAIL_REQUIRE("Expected string value"); default:
KJ_FAIL_REQUIRE("Expected text value");
} }
break; break;
case schema::Type::DATA: case schema::Type::DATA:
if (value.isArray()) { switch (value.which()) {
case JsonValue::ARRAY: {
auto array = value.getArray(); auto array = value.getArray();
kj::Vector<byte> data(array.size()); kj::Vector<byte> data(array.size());
for (auto arrayObject : array) { for (auto arrayObject : array) {
auto x = int(arrayObject.getNumber()); auto x = arrayObject.getNumber();
KJ_REQUIRE(x >= 0 && x <= 255, "Number in array of bytes out of range."); KJ_REQUIRE(byte(x) == x, "Number in array of bytes out of range.");
data.add(byte(x)); data.add(byte(x));
} }
setFn(Data::Reader(data.asPtr())); setFn(Data::Reader(data.asPtr()));
} else { break;
KJ_FAIL_REQUIRE("Expected string value"); }
default:
KJ_FAIL_REQUIRE("Expected data value");
} }
break; break;
case schema::Type::LIST: case schema::Type::LIST:
if (value.isNull()) { switch (value.which()) {
} else if (value.isArray()) { case JsonValue::NULL_:
// nothing to do
break;
case JsonValue::ARRAY:
decodeArrayFn(value.getArray()); decodeArrayFn(value.getArray());
} else { break;
KJ_FAIL_REQUIRE("Expected array value"); default:
KJ_FAIL_REQUIRE("Expected list value");
} }
break; break;
case schema::Type::ENUM: case schema::Type::ENUM:
if (value.isString()) { switch (value.which()) {
case JsonValue::STRING:
setFn(value.getString()); setFn(value.getString());
} else { break;
KJ_FAIL_REQUIRE("Expected enum as string value"); default:
KJ_FAIL_REQUIRE("Expected enum value");
} }
break; break;
case schema::Type::STRUCT: { case schema::Type::STRUCT:
if (value.isNull()) { switch (value.which()) {
} else if (value.isObject()) { case JsonValue::NULL_:
// nothing to do
break;
case JsonValue::OBJECT:
decodeObjectFn(value.getObject()); decodeObjectFn(value.getObject());
} else { break;
default:
KJ_FAIL_REQUIRE("Expected object value"); KJ_FAIL_REQUIRE("Expected object value");
} }
break; break;
} }
}
} }
} //namespace } // namespace
void JsonCodec::decodeArray(List<JsonValue>::Reader input, DynamicList::Builder output) const { void JsonCodec::decodeArray(List<JsonValue>::Reader input, DynamicList::Builder output) const {
KJ_ASSERT(input.size() == output.size(), "Builder was not initialized to input size"); KJ_ASSERT(input.size() == output.size(), "Builder was not initialized to input size");
...@@ -535,7 +561,7 @@ void JsonCodec::decodeObject(List<JsonValue::Field>::Reader input, DynamicStruct ...@@ -535,7 +561,7 @@ void JsonCodec::decodeObject(List<JsonValue::Field>::Reader input, DynamicStruct
decodeObject(object, output.init(*fieldSchema).as<DynamicStruct>()); decodeObject(object, output.init(*fieldSchema).as<DynamicStruct>());
}); });
} else { } else {
//Unknown json fields are ignored to allow schema evolution // Unknown json fields are ignored to allow schema evolution
} }
} }
} }
...@@ -553,7 +579,7 @@ void JsonCodec::decode(JsonValue::Reader input, DynamicStruct::Builder output) c ...@@ -553,7 +579,7 @@ void JsonCodec::decode(JsonValue::Reader input, DynamicStruct::Builder output) c
Orphan<DynamicValue> JsonCodec::decode( Orphan<DynamicValue> JsonCodec::decode(
JsonValue::Reader input, Type type, Orphanage orphanage) const { JsonValue::Reader input, Type type, Orphanage orphanage) const {
//TODO(soon) // TODO(soon)
KJ_FAIL_ASSERT("JSON decode into orphanage not implement yet. :("); KJ_FAIL_ASSERT("JSON decode into orphanage not implement yet. :(");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment