Commit d5892261 authored by Kenton Varda's avatar Kenton Varda

Refactor: Put json.capnp into its own namespace.

This avoids the need for a weird struct-as-namespace pattern inside json.capnp.
parent c10a1b08
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
@0xc9d405cf4333e4c9; @0xc9d405cf4333e4c9;
using Json = import "json.capnp".JsonAnnotations; using Json = import "json.capnp";
using JsonValue = import "json.capnp".JsonValue;
$import "/capnp/c++.capnp".namespace("capnp"); $import "/capnp/c++.capnp".namespace("capnp");
......
...@@ -951,7 +951,7 @@ public: ...@@ -951,7 +951,7 @@ public:
case JSON_FLATTEN_ANNOTATION_ID: case JSON_FLATTEN_ANNOTATION_ID:
KJ_REQUIRE(type.isStruct(), "only struct types can be flattened", fieldName, typeName); KJ_REQUIRE(type.isStruct(), "only struct types can be flattened", fieldName, typeName);
flattened = true; flattened = true;
info.prefix = anno.getValue().getStruct().getAs<JsonFlattenFlags>().getPrefix(); info.prefix = anno.getValue().getStruct().getAs<json::FlattenOptions>().getPrefix();
break; break;
case JSON_DISCRIMINATOR_ANNOTATION_ID: case JSON_DISCRIMINATOR_ANNOTATION_ID:
KJ_REQUIRE(fieldProto.isGroup(), "only unions can have discriminator"); KJ_REQUIRE(fieldProto.isGroup(), "only unions can have discriminator");
......
...@@ -21,15 +21,15 @@ ...@@ -21,15 +21,15 @@
@0x8ef99297a43a5e34; @0x8ef99297a43a5e34;
$import "/capnp/c++.capnp".namespace("capnp"); $import "/capnp/c++.capnp".namespace("capnp::json");
struct JsonValue { struct Value {
union { union {
null @0 :Void; null @0 :Void;
boolean @1 :Bool; boolean @1 :Bool;
number @2 :Float64; number @2 :Float64;
string @3 :Text; string @3 :Text;
array @4 :List(JsonValue); array @4 :List(Value);
object @5 :List(Field); object @5 :List(Field);
# Standard JSON values. # Standard JSON values.
...@@ -48,45 +48,44 @@ struct JsonValue { ...@@ -48,45 +48,44 @@ struct JsonValue {
struct Field { struct Field {
name @0 :Text; name @0 :Text;
value @1 :JsonValue; value @1 :Value;
} }
struct Call { struct Call {
function @0 :Text; function @0 :Text;
params @1 :List(JsonValue); params @1 :List(Value);
} }
} }
struct JsonFlattenFlags { # ========================================================================================
prefix @0 :Text = ""; # Annotations to control parsing. Typical usage:
# Optional: Adds the given prefix to flattened field names. #
} # using Json = import "/capnp/compat/json.capnp";
#
struct JsonAnnotations { # And then later on:
# These are wrapped in this struct for namespacing purposes. Typical usage: #
# # myField @0 :Text $Json.name("my_field");
# using Json = import "/capnp/compat/json.capnp".JsonAnnotations;
#
# And then later on:
#
# myField @0 :Text $Json.name("my_field");
annotation name @0xfa5b1fd61c2e7c3d (field, enumerant, method, group, union): Text; annotation name @0xfa5b1fd61c2e7c3d (field, enumerant, method, group, union): Text;
# Define an alternative name to use when encoding the given item in JSON. This can be used, for # Define an alternative name to use when encoding the given item in JSON. This can be used, for
# example, to use snake_case names where needed, even though Cap'n Proto uses strictly camelCase. # example, to use snake_case names where needed, even though Cap'n Proto uses strictly camelCase.
# #
# (However, because JSON is derived from JavaScript, you *should* use camelCase names when # (However, because JSON is derived from JavaScript, you *should* use camelCase names when
# defining JSON-based APIs. But, when supporting a pre-existing API you may not have a choice.) # defining JSON-based APIs. But, when supporting a pre-existing API you may not have a choice.)
annotation flatten @0x82d3e852af0336bf (field, group, union): JsonFlattenFlags; annotation flatten @0x82d3e852af0336bf (field, group, union): FlattenOptions;
# Specifies that an aggregate field should be flattened into its parent. # Specifies that an aggregate field should be flattened into its parent.
# #
# In order to flatten a member of a union, the union (or, for an anonymous union, the parent # In order to flatten a member of a union, the union (or, for an anonymous union, the parent
# struct type) must have the $jsonDiscribinator annotation. # struct type) must have the $jsonDiscribinator annotation.
annotation discriminator @0xcfa794e8d19a0162 (struct, union): Text; struct FlattenOptions {
# Specifies that a union's variant will be decided not by which fields are present, but instead prefix @0 :Text = "";
# by a special discriminator field with the given name. The value of the discriminator field is # Optional: Adds the given prefix to flattened field names.
# a string naming which variant is active. This allows the members of the union to have the
# $jsonFlatten annotation.
} }
annotation discriminator @0xcfa794e8d19a0162 (struct, union): Text;
# Specifies that a union's variant will be decided not by which fields are present, but instead
# 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.
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
namespace capnp { namespace capnp {
typedef json::Value JsonValue;
// For backwards-compatibility.
//
// TODO(cleanup): Consider replacing all uses of JsonValue with json::Value?
class JsonCodec { class JsonCodec {
// Flexible class for encoding Cap'n Proto types as JSON, and decoding JSON back to Cap'n Proto. // Flexible class for encoding Cap'n Proto types as JSON, and decoding JSON back to Cap'n Proto.
// //
......
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