Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
e282e1b6
Commit
e282e1b6
authored
Jun 29, 2018
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JSON annotations for encoding Data as base64 or hex.
parent
4311434a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
61 additions
and
2 deletions
+61
-2
json-test.c++
c++/src/capnp/compat/json-test.c++
+8
-1
json-test.capnp
c++/src/capnp/compat/json-test.capnp
+3
-0
json.c++
c++/src/capnp/compat/json.c++
+39
-0
json.capnp
c++/src/capnp/compat/json.capnp
+6
-0
json.h
c++/src/capnp/compat/json.h
+2
-0
annotated-json.binary
c++/src/capnp/testdata/annotated-json.binary
+0
-0
annotated.json
c++/src/capnp/testdata/annotated.json
+3
-1
No files found.
c++/src/capnp/compat/json-test.c++
View file @
e282e1b6
...
...
@@ -840,10 +840,14 @@ R"({ "names-can_contain!anything Really": "foo",
"simpleGroup": {"renamed-grault": "garply"},
"enums": ["qux", "renamed-bar", "foo", "renamed-baz"],
"innerJson": [123, "hello", {"object": true}],
"customFieldHandler": "add-prefix-waldo" })"
_kj
;
"customFieldHandler": "add-prefix-waldo",
"testBase64": "ZnJlZA==",
"testHex": "706c756768" })"
_kj
;
static
constexpr
kj
::
StringPtr
GOLDEN_ANNOTATED_REVERSE
=
R"({
"testHex": "706c756768",
"testBase64": "ZnJlZA==",
"customFieldHandler": "add-prefix-waldo",
"innerJson": [123, "hello", {"object": true}],
"enums": ["qux", "renamed-bar", "foo", "renamed-baz"],
...
...
@@ -927,6 +931,9 @@ KJ_TEST("rename fields") {
root
.
setCustomFieldHandler
(
"waldo"
);
root
.
setTestBase64
(
"fred"
_kj
.
asBytes
());
root
.
setTestHex
(
"plugh"
_kj
.
asBytes
());
auto
encoded
=
json
.
encode
(
root
.
asReader
());
KJ_EXPECT
(
encoded
==
GOLDEN_ANNOTATED
,
encoded
);
...
...
c++/src/capnp/compat/json-test.capnp
View file @
e282e1b6
...
...
@@ -74,6 +74,9 @@ struct TestJsonAnnotations {
innerJson @16 :Json.Value;
customFieldHandler @17 :Text;
testBase64 @18 :Data $Json.base64;
testHex @19 :Data $Json.hex;
}
struct TestJsonAnnotations2 {
...
...
c++/src/capnp/compat/json.c++
View file @
e282e1b6
...
...
@@ -31,6 +31,7 @@
#include <kj/function.h>
#include <kj/vector.h>
#include <kj/one-of.h>
#include <kj/encoding.h>
namespace
capnp
{
...
...
@@ -896,6 +897,32 @@ void JsonCodec::addFieldHandlerImpl(StructSchema::Field field, Type type, Handle
static
constexpr
uint64_t
JSON_NAME_ANNOTATION_ID
=
0xfa5b1fd61c2e7c3dull
;
static
constexpr
uint64_t
JSON_FLATTEN_ANNOTATION_ID
=
0x82d3e852af0336bfull
;
static
constexpr
uint64_t
JSON_DISCRIMINATOR_ANNOTATION_ID
=
0xcfa794e8d19a0162ull
;
static
constexpr
uint64_t
JSON_BASE64_ANNOTATION_ID
=
0xd7d879450a253e4bull
;
static
constexpr
uint64_t
JSON_HEX_ANNOTATION_ID
=
0xf061e22f0ae5c7b5ull
;
class
JsonCodec
::
Base64Handler
final
:
public
JsonCodec
::
Handler
<
capnp
::
Data
>
{
public
:
void
encode
(
const
JsonCodec
&
codec
,
capnp
::
Data
::
Reader
input
,
JsonValue
::
Builder
output
)
const
{
output
.
setString
(
kj
::
encodeBase64
(
input
));
}
Orphan
<
capnp
::
Data
>
decode
(
const
JsonCodec
&
codec
,
JsonValue
::
Reader
input
,
Orphanage
orphanage
)
const
{
return
orphanage
.
newOrphanCopy
(
capnp
::
Data
::
Reader
(
kj
::
decodeBase64
(
input
.
getString
())));
}
};
class
JsonCodec
::
HexHandler
final
:
public
JsonCodec
::
Handler
<
capnp
::
Data
>
{
public
:
void
encode
(
const
JsonCodec
&
codec
,
capnp
::
Data
::
Reader
input
,
JsonValue
::
Builder
output
)
const
{
output
.
setString
(
kj
::
encodeHex
(
input
));
}
Orphan
<
capnp
::
Data
>
decode
(
const
JsonCodec
&
codec
,
JsonValue
::
Reader
input
,
Orphanage
orphanage
)
const
{
return
orphanage
.
newOrphanCopy
(
capnp
::
Data
::
Reader
(
kj
::
decodeHex
(
input
.
getString
())));
}
};
class
JsonCodec
::
AnnotatedHandler
final
:
public
JsonCodec
::
Handler
<
DynamicStruct
>
{
public
:
...
...
@@ -962,6 +989,18 @@ public:
KJ_REQUIRE
(
fieldProto
.
isGroup
(),
"only unions can have discriminator"
);
subDiscriminator
=
anno
.
getValue
().
getText
();
break
;
case
JSON_BASE64_ANNOTATION_ID
:
{
KJ_REQUIRE
(
field
.
getType
().
isData
(),
"only Data can be marked for base64 encoding"
);
static
Base64Handler
handler
;
codec
.
addFieldHandler
(
field
,
handler
);
break
;
}
case
JSON_HEX_ANNOTATION_ID
:
{
KJ_REQUIRE
(
field
.
getType
().
isData
(),
"only Data can be marked for hex encoding"
);
static
HexHandler
handler
;
codec
.
addFieldHandler
(
field
,
handler
);
break
;
}
}
}
...
...
c++/src/capnp/compat/json.capnp
View file @
e282e1b6
...
...
@@ -92,3 +92,9 @@ annotation discriminator @0xcfa794e8d19a0162 (struct, union): Text;
# by a special discriminator field with the given name. The value of the discriminator field is
# a string naming which variant is active. This allows the members of the union to have the
# $jsonFlatten annotation.
annotation base64 @0xd7d879450a253e4b (field): Void;
# Place on a field of type `Data` to indicate that its JSON representation is a Base64 string.
annotation hex @0xf061e22f0ae5c7b5 (field): Void;
# Place on a field of type `Data` to indicate that its JSON representation is a hex string.
c++/src/capnp/compat/json.h
View file @
e282e1b6
...
...
@@ -231,6 +231,8 @@ private:
class
HandlerBase
;
class
AnnotatedHandler
;
class
AnnotatedEnumHandler
;
class
Base64Handler
;
class
HexHandler
;
class
JsonValueHandler
;
struct
Impl
;
...
...
c++/src/capnp/testdata/annotated-json.binary
View file @
e282e1b6
No preview for this file type
c++/src/capnp/testdata/annotated.json
View file @
e282e1b6
...
...
@@ -13,4 +13,6 @@
"dependency"
:
{
"renamed-foo"
:
"corge"
},
"simpleGroup"
:
{
"renamed-grault"
:
"garply"
},
"enums"
:
[
"qux"
,
"renamed-bar"
,
"foo"
,
"renamed-baz"
],
"innerJson"
:
[
123
,
"hello"
,
{
"object"
:
true
}]
}
"innerJson"
:
[
123
,
"hello"
,
{
"object"
:
true
}],
"testBase64"
:
"ZnJlZA=="
,
"testHex"
:
"706c756768"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment