Commit 1ccd2dcb authored by Kenton Varda's avatar Kenton Varda

Honor JSON annotations when using `capnp convert`.

parent 0a2d1e0c
...@@ -38,6 +38,7 @@ else ...@@ -38,6 +38,7 @@ else
fi fi
SCHEMA=`dirname "$0"`/../test.capnp SCHEMA=`dirname "$0"`/../test.capnp
JSON_SCHEMA=`dirname "$0"`/../compat/json-test.capnp
TESTDATA=`dirname "$0"`/../testdata TESTDATA=`dirname "$0"`/../testdata
SUFFIX=${TESTDATA#*/src/} SUFFIX=${TESTDATA#*/src/}
...@@ -74,6 +75,9 @@ $CAPNP convert binary:json --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp ...@@ -74,6 +75,9 @@ $CAPNP convert binary:json --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp
$CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/pretty.json | cmp $TESTDATA/binary - || fail json to binary $CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/pretty.json | cmp $TESTDATA/binary - || fail json to binary
$CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/short.json | cmp $TESTDATA/binary - || fail short json to binary $CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/short.json | cmp $TESTDATA/binary - || fail short json to binary
$CAPNP convert json:binary $JSON_SCHEMA TestJsonAnnotations < $TESTDATA/annotated.json | cmp $TESTDATA/annotated-json.binary || fail annotated json to binary
$CAPNP convert binary:json $JSON_SCHEMA TestJsonAnnotations < $TESTDATA/annotated-json.binary | cmp $TESTDATA/annotated.json || fail annotated json to binary
# ======================================================================================== # ========================================================================================
# DEPRECATED encode/decode # DEPRECATED encode/decode
......
...@@ -714,6 +714,13 @@ public: ...@@ -714,6 +714,13 @@ public:
return kj::str("unknown format: ", to); return kj::str("unknown format: ", to);
} }
if (convertFrom == Format::JSON || convertTo == Format::JSON) {
// We need annotations to process JSON.
// TODO(someday): Find a way that we can process annotations from json.capnp without
// requiring other annotation-only imports like c++.capnp
annotationFlag = Compiler::COMPILE_ANNOTATIONS;
}
return true; return true;
} else { } else {
return "invalid conversion, format is: <from>:<to>"; return "invalid conversion, format is: <from>:<to>";
...@@ -1038,6 +1045,7 @@ private: ...@@ -1038,6 +1045,7 @@ private:
MallocMessageBuilder message; MallocMessageBuilder message;
JsonCodec codec; JsonCodec codec;
codec.setPrettyPrint(pretty); codec.setPrettyPrint(pretty);
codec.handleByAnnotation(rootType);
auto root = message.initRoot<DynamicStruct>(rootType); auto root = message.initRoot<DynamicStruct>(rootType);
codec.decode(text, root); codec.decode(text, root);
return writeConversion(root.asReader(), output); return writeConversion(root.asReader(), output);
...@@ -1096,6 +1104,7 @@ private: ...@@ -1096,6 +1104,7 @@ private:
case Format::JSON: { case Format::JSON: {
JsonCodec codec; JsonCodec codec;
codec.setPrettyPrint(pretty); codec.setPrettyPrint(pretty);
codec.handleByAnnotation(rootType);
auto text = codec.encode(reader.as<DynamicStruct>(rootType)); auto text = codec.encode(reader.as<DynamicStruct>(rootType));
output.write({text.asBytes(), kj::StringPtr("\n").asBytes()}); output.write({text.asBytes(), kj::StringPtr("\n").asBytes()});
return; return;
......
{ "names-can_contain!anything Really": "foo",
"flatFoo": 123,
"flatBar": "abc",
"renamed-flatBaz": {"hello": true},
"flatQux": "cba",
"pfx.foo": "this is a long string in order to force multi-line pretty printing",
"pfx.renamed-bar": 321,
"pfx.baz": {"hello": true},
"pfx.xfp.qux": "fed",
"union-type": "renamed-bar",
"barMember": 789,
"multiMember": "ghi",
"dependency": {"renamed-foo": "corge"},
"simpleGroup": {"renamed-grault": "garply"},
"enums": ["qux", "renamed-bar", "foo", "renamed-baz"],
"innerJson": [123, "hello", {"object": true}] }
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