// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #ifndef CAPNP_COMPAT_JSON_H_ #define CAPNP_COMPAT_JSON_H_ #include #include #include namespace capnp { class JsonCodec { // Flexible class for encoding Cap'n Proto types as JSON, and decoding JSON back to Cap'n Proto. // // Typical usage: // // JsonCodec json; // // // encode // kj::String encoded = json.encode(someStructReader); // // // decode // json.decode(encoded, someStructBuilder); // // Advanced users can do fancy things like override the way certain types or fields are // represented in JSON by registering handlers. See the unit test for an example. // // Notes: // - When encoding, all primitive fields are always encoded, even if default-valued. Pointer // fields are only encoded if they are non-null. // - 64-bit integers are encoded as strings, since JSON "numbers" are double-precision floating // points which cannot store a 64-bit integer without losing data. // - NaNs and infinite floating point numbers are not allowed by the JSON spec, and so are encoded // as null. This matches the behavior of `JSON.stringify` in at least Firefox and Chrome. // - Data is encoded as an array of numbers in the range [0,255]. You probably want to register // a handler that does something better, like maybe base64 encoding, but there are a zillion // different ways people do this. // - Encoding/decoding capabilities and AnyPointers requires registering a Handler, since there's // no obvious default behavior. // - When decoding, unrecognized field names are ignored. Note: This means that JSON is NOT a // good format for receiving input from a human. Consider `capnp eval` or the SchemaParser // library for human input. public: JsonCodec(); ~JsonCodec() noexcept(false); // --------------------------------------------------------------------------- // standard API void setPrettyPrint(bool enabled); // Enable to insert newlines, indentation, and other extra spacing into the output. The default // is to use minimal whitespace. void setMaxNestingDepth(size_t maxNestingDepth); // Set maximum nesting depth when decoding JSON to prevent highly nested input from overflowing // the call stack. The default is 64. template kj::String encode(T&& value); // Encode any Cap'n Proto value to JSON, including primitives and // Dynamic{Enum,Struct,List,Capability}, but not DynamicValue (see below). kj::String encode(DynamicValue::Reader value, Type type) const; // Encode a DynamicValue to JSON. `type` is needed because `DynamicValue` itself does // not distinguish between e.g. int32 and int64, which in JSON are handled differently. Most // of the time, though, you can use the single-argument templated version of `encode()` instead. void decode(kj::ArrayPtr input, DynamicStruct::Builder output) const; // Decode JSON text directly into a struct builder. This only works for structs since lists // need to be allocated with the correct size in advance. // // (Remember that any Cap'n Proto struct reader type can be implicitly cast to // DynamicStruct::Reader.) template Orphan decode(kj::ArrayPtr input, Orphanage orphanage) const; // Decode JSON text to any Cap'n Proto object (pointer value), allocated using the given // orphanage. T must be specified explicitly and cannot be dynamic, e.g.: // // Orphan orphan = json.decode(text, orphanage); template ReaderFor decode(kj::ArrayPtr input) const; // Decode JSON text into a primitive or capability value. T must be specified explicitly and // cannot be dynamic, e.g.: // // uint32_t n = json.decode(text); Orphan decode(kj::ArrayPtr input, Type type, Orphanage orphanage) const; Orphan decode( kj::ArrayPtr input, ListSchema type, Orphanage orphanage) const; Orphan decode( kj::ArrayPtr input, StructSchema type, Orphanage orphanage) const; DynamicCapability::Client decode(kj::ArrayPtr input, InterfaceSchema type) const; DynamicEnum decode(kj::ArrayPtr input, EnumSchema type) const; // Decode to a dynamic value, specifying the type schema. // --------------------------------------------------------------------------- // layered API // // You can separate text <-> JsonValue from JsonValue <-> T. These are particularly useful // for calling from Handler implementations. kj::String encodeRaw(JsonValue::Reader value) const; void decodeRaw(kj::ArrayPtr input, JsonValue::Builder output) const; // Translate JsonValue <-> text. template void encode(T&& value, JsonValue::Builder output); void encode(DynamicValue::Reader input, Type type, JsonValue::Builder output) const; void decode(JsonValue::Reader input, DynamicStruct::Builder output) const; template Orphan decode(JsonValue::Reader input, Orphanage orphanage) const; template ReaderFor decode(JsonValue::Reader input) const; Orphan decode(JsonValue::Reader input, Type type, Orphanage orphanage) const; Orphan decode(JsonValue::Reader input, ListSchema type, Orphanage orphanage) const; Orphan decode( JsonValue::Reader input, StructSchema type, Orphanage orphanage) const; DynamicCapability::Client decode(JsonValue::Reader input, InterfaceSchema type) const; DynamicEnum decode(JsonValue::Reader input, EnumSchema type) const; // --------------------------------------------------------------------------- // specializing particular types template ()> class Handler; // Implement this interface to specify a special encoding for a particular type or field. // // The templates are a bit ugly, but subclasses of this type essentially implement two methods, // one to encode values of this type and one to decode values of this type. `encode()` is simple: // // void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const; // // `decode()` is a bit trickier. When T is a struct (including DynamicStruct), it is: // // void decode(const JsonCodec& codec, JsonValue::Reader input, BuilderFor output) const; // // However, when T is a primitive, decode() is: // // T decode(const JsonCodec& codec, JsonValue::Reader input) const; // // Or when T is any non-struct object (list, blob), decode() is: // // Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const; // // Or when T is an interface: // // T::Client decode(const JsonCodec& codec, JsonValue::Reader input) const; // // Additionally, when T is a struct you can *optionally* also implement the orphan-returning form // of decode(), but it will only be called when the struct would be allocated as an individual // object, not as part of a list. This allows you to return "nullptr" in these cases to say that // the pointer value should be null. This does not apply to list elements because struct list // elements cannot ever be null (since Cap'n Proto encodes struct lists as a flat list rather // than list-of-pointers). template void addTypeHandler(Handler& handler); void addTypeHandler(Type type, Handler& handler); void addTypeHandler(EnumSchema type, Handler& handler); void addTypeHandler(StructSchema type, Handler& handler); void addTypeHandler(ListSchema type, Handler& handler); void addTypeHandler(InterfaceSchema type, Handler& handler); // Arrange that whenever the type T appears in the message, your handler will be used to // encode/decode it. // // Note that if you register a handler for a capability type, it will also apply to subtypes. // Thus Handler handles all capabilities. template void addFieldHandler(StructSchema::Field field, Handler& handler); // Matches only the specific field. T can be a dynamic type. T must match the field's type. private: class HandlerBase; struct Impl; kj::Own impl; void encodeField(StructSchema::Field field, DynamicValue::Reader input, JsonValue::Builder output) const; void decodeArray(List::Reader input, DynamicList::Builder output) const; void decodeObject(List::Reader input, DynamicStruct::Builder output) const; void addTypeHandlerImpl(Type type, HandlerBase& handler); void addFieldHandlerImpl(StructSchema::Field field, Type type, HandlerBase& handler); }; // ======================================================================================= // inline implementation details template kj::String JsonCodec::encode(T&& value) { typedef FromAny> Base; return encode(DynamicValue::Reader(ReaderFor(kj::fwd(value))), Type::from()); } template inline Orphan JsonCodec::decode(kj::ArrayPtr input, Orphanage orphanage) const { return decode(input, Type::from(), orphanage).template releaseAs(); } template inline ReaderFor JsonCodec::decode(kj::ArrayPtr input) const { static_assert(style() == Style::PRIMITIVE || style() == Style::CAPABILITY, "must specify an orphanage to decode an object type"); return decode(input, Type::from(), Orphanage()).getReader().template as(); } inline Orphan JsonCodec::decode( kj::ArrayPtr input, ListSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline Orphan JsonCodec::decode( kj::ArrayPtr input, StructSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline DynamicCapability::Client JsonCodec::decode( kj::ArrayPtr input, InterfaceSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } inline DynamicEnum JsonCodec::decode(kj::ArrayPtr input, EnumSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } // ----------------------------------------------------------------------------- template void JsonCodec::encode(T&& value, JsonValue::Builder output) { typedef FromAny> Base; encode(DynamicValue::Reader(ReaderFor(kj::fwd(value))), Type::from(), output); } template inline Orphan JsonCodec::decode(JsonValue::Reader input, Orphanage orphanage) const { return decode(input, Type::from(), orphanage).template releaseAs(); } template inline ReaderFor JsonCodec::decode(JsonValue::Reader input) const { static_assert(style() == Style::PRIMITIVE || style() == Style::CAPABILITY, "must specify an orphanage to decode an object type"); return decode(input, Type::from(), Orphanage()).getReader().template as(); } inline Orphan JsonCodec::decode( JsonValue::Reader input, ListSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline Orphan JsonCodec::decode( JsonValue::Reader input, StructSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline DynamicCapability::Client JsonCodec::decode( JsonValue::Reader input, InterfaceSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } inline DynamicEnum JsonCodec::decode(JsonValue::Reader input, EnumSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } // ----------------------------------------------------------------------------- class JsonCodec::HandlerBase { // Internal helper; ignore. public: virtual void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const = 0; virtual Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const; virtual void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, orphanage); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const = 0; virtual void decode(const JsonCodec& codec, JsonValue::Reader input, BuilderFor output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const { // If subclass does not override, fall back to regular version. auto result = orphanage.newOrphan(); decode(codec, input, result.get()); return result; } private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, orphanage); } void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override final { decode(codec, input, output.as()); } friend class JsonCodec; }; template <> class JsonCodec::Handler: private JsonCodec::HandlerBase { // Almost identical to Style::STRUCT except that we pass the struct type to decode(). public: virtual void encode(const JsonCodec& codec, DynamicStruct::Reader input, JsonValue::Builder output) const = 0; virtual void decode(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, StructSchema type, Orphanage orphanage) const { // If subclass does not override, fall back to regular version. auto result = orphanage.newOrphan(type); decode(codec, input, result.get()); return result; } private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, type.asStruct(), orphanage); } void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override final { decode(codec, input, output.as()); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, T input, JsonValue::Builder output) const = 0; virtual T decode(const JsonCodec& codec, JsonValue::Reader input) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, typename T::Client input, JsonValue::Builder output) const = 0; virtual typename T::Client decode(const JsonCodec& codec, JsonValue::Reader input) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return orphanage.newOrphanCopy(decode(codec, input)); } friend class JsonCodec; }; template inline void JsonCodec::addTypeHandler(Handler& handler) { addTypeHandlerImpl(Type::from(), handler); } inline void JsonCodec::addTypeHandler(Type type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(EnumSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(StructSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(ListSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(InterfaceSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } template inline void JsonCodec::addFieldHandler(StructSchema::Field field, Handler& handler) { addFieldHandlerImpl(field, Type::from(), handler); } template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); // TODO(someday): Implement support for registering handlers that cover thinsg like "all structs" // or "all lists". Currently you can only target a specific struct or list type. } // namespace capnp #endif // CAPNP_COMPAT_JSON_H_