Google APIs Client Library for C++
serializable_json.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 /*
00022  * @defgroup DataLayerJson Data Layer - JSON Support
00023  *
00024  * The JSON Support module provides helper classes for using
00025  * <a href='http://tools.ietf.org/html/rfc4627'>RFC 4627 JSON</a>
00026  * as an encoding type.
00027  *
00028  * As far as dependencies go, the JSON Support module lives in the Data Layer.
00029  */
00030 
00031 #ifndef APISERVING_CLIENTS_CPP_DATA_SERIALIZABLE_JSON_H_
00032 #define APISERVING_CLIENTS_CPP_DATA_SERIALIZABLE_JSON_H_
00033 #include <istream>  // NOLINT
00034 #include <ostream>  // NOLINT
00035 #include <string>
00036 using std::string;
00037 
00038 #include "googleapis/util/status.h"
00039 namespace googleapis {
00040 
00041 namespace client {
00042 class DataReader;
00043 
00044 /*
00045  * An abstract interface for data objects used to denote JSON compatability.
00046  * @ingroup DataLayerJson
00047  *
00048  * This is just an interface for a data object. At least at this time.
00049  * For a concrete class, see JsonCppData in jsoncpp_data.h.
00050  * The intent of making this an interface is to allow other underlying
00051  * implementations for experimentation without impacting much code that
00052  * just passes through data or converts to/from json wire protocol.
00053  */
00054 class SerializableJson {
00055  public:
00056   /*
00057    * Standard destructor
00058    */
00059   virtual ~SerializableJson();
00060 
00061   /*
00062    * Clear the instance data back to default state.
00063    */
00064   virtual void Clear() = 0;
00065 
00066   /*
00067    * Initialize instance from a reader.
00068    *
00069    * @param [in] reader JSON-encoded byte stream.
00070    */
00071   virtual util::Status LoadFromJsonReader(DataReader* reader) = 0;
00072 
00073   /*
00074    * Creates a reader that contains the serialized json for this object.
00075    *
00076    * @return A JSON-encoded byte stream with this object's state.
00077    *         If there is an error, the details will be in the reader status().
00078    */
00079   virtual DataReader* MakeJsonReader() const = 0;
00080 
00081   /*
00082    * Initializes this instance from a standard C++ istream.
00083    *
00084    * @param[in] stream JSON-encoded istream
00085    *
00086    * The default implementation creates an IstreamDataReader and calls
00087    * the Load() method.
00088    */
00089   virtual util::Status LoadFromJsonStream(std::istream* stream);
00090 
00091   /*
00092    * Serialize the instance as a JSON document to an ostream.
00093    *
00094    * @param[in] stream The output stream to write to.
00095    *
00096    * The default implementation calls MakeJsonReader() and writes the
00097    * byte-stream to the output stream.
00098    */
00099   virtual util::Status StoreToJsonStream(std::ostream* stream) const;
00100 };
00101 
00102 /*
00103  * Syntactic sugar for data.LoadFromStream(&stream).
00104  *
00105  * <pre>
00106  *   ostringstream output;
00107  *   output << data;
00108  * </pre>
00109  */
00110 // NOLINT
00111 inline std::istream& operator >>(
00112     std::istream &stream, SerializableJson& data) {
00113   data.LoadFromJsonStream(&stream).IgnoreError();
00114   return stream;
00115 }
00116 
00117 /*
00118  * Syntactic sugar for data.StoreToStream(&stream)
00119  *
00120  * <pre>
00121  *   string json = ...;
00122  *   istringstream input(json);
00123  *   data << input;
00124  * </pre>
00125  */
00126 inline std::ostream& operator <<(
00127     std::ostream &stream, const SerializableJson& data) {
00128   data.StoreToJsonStream(&stream).IgnoreError();
00129   return stream;
00130 }
00131 
00132 }  // namespace client
00133 
00134 } // namespace googleapis
00135 #endif  // APISERVING_CLIENTS_CPP_DATA_SERIALIZABLE_JSON_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines