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 @@
@0xc9d405cf4333e4c9;
using Json = import "json.capnp".JsonAnnotations;
using JsonValue = import "json.capnp".JsonValue;
using Json = import "json.capnp";
$import "/capnp/c++.capnp".namespace("capnp");
......
......@@ -951,7 +951,7 @@ public:
case JSON_FLATTEN_ANNOTATION_ID:
KJ_REQUIRE(type.isStruct(), "only struct types can be flattened", fieldName, typeName);
flattened = true;
info.prefix = anno.getValue().getStruct().getAs<JsonFlattenFlags>().getPrefix();
info.prefix = anno.getValue().getStruct().getAs<json::FlattenOptions>().getPrefix();
break;
case JSON_DISCRIMINATOR_ANNOTATION_ID:
KJ_REQUIRE(fieldProto.isGroup(), "only unions can have discriminator");
......
......@@ -21,15 +21,15 @@
@0x8ef99297a43a5e34;
$import "/capnp/c++.capnp".namespace("capnp");
$import "/capnp/c++.capnp".namespace("capnp::json");
struct JsonValue {
struct Value {
union {
null @0 :Void;
boolean @1 :Bool;
number @2 :Float64;
string @3 :Text;
array @4 :List(JsonValue);
array @4 :List(Value);
object @5 :List(Field);
# Standard JSON values.
......@@ -48,45 +48,44 @@ struct JsonValue {
struct Field {
name @0 :Text;
value @1 :JsonValue;
value @1 :Value;
}
struct Call {
function @0 :Text;
params @1 :List(JsonValue);
params @1 :List(Value);
}
}
struct JsonFlattenFlags {
prefix @0 :Text = "";
# Optional: Adds the given prefix to flattened field names.
}
struct JsonAnnotations {
# These are wrapped in this struct for namespacing purposes. Typical usage:
#
# using Json = import "/capnp/compat/json.capnp".JsonAnnotations;
#
# And then later on:
#
# myField @0 :Text $Json.name("my_field");
# ========================================================================================
# Annotations to control parsing. Typical usage:
#
# using Json = import "/capnp/compat/json.capnp";
#
# And then later on:
#
# myField @0 :Text $Json.name("my_field");
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
# 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
# defining JSON-based APIs. But, when supporting a pre-existing API you may not have a choice.)
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
# 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
# defining JSON-based APIs. But, when supporting a pre-existing API you may not have a choice.)
annotation flatten @0x82d3e852af0336bf (field, group, union): JsonFlattenFlags;
# 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
# struct type) must have the $jsonDiscribinator annotation.
annotation flatten @0x82d3e852af0336bf (field, group, union): FlattenOptions;
# 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
# struct type) must have the $jsonDiscribinator 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.
struct FlattenOptions {
prefix @0 :Text = "";
# Optional: Adds the given prefix to flattened field names.
}
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 @@
namespace capnp {
typedef json::Value JsonValue;
// For backwards-compatibility.
//
// TODO(cleanup): Consider replacing all uses of JsonValue with json::Value?
class JsonCodec {
// 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