Example #1
0
        /// <summary>
        /// Encodes the specified object.
        /// </summary>
        /// <typeparam name="T">The type of the object to encode.</typeparam>
        /// <param name="encodable">The object to encode.</param>
        /// <returns>The encoded object as a JSON string.</returns>
        public static string Encode <T>(T encodable)
            where T : IEncodable <T>
        {
            var encoder = new JsonEncoder();

            encodable.Encode(encoder);
            return(encoder.JsonString);
        }
Example #2
0
            /// <summary>
            /// Adds a field that implements the
            /// <see cref="T:Dropbox.Api.Babel.IEncodable`1" /> interface to the current
            /// encoding context.
            /// </summary>
            /// <typeparam name="T">The type of the field that is being added.</typeparam>
            /// <param name="name">The field name.</param>
            /// <param name="value">The value of the field.</param>
            public void AddFieldObject <T>(string name, T value)
                where T : IEncodable <T>, new()
            {
                var encoder = new JsonEncoder();

                value.Encode(encoder);
                this.obj[name] = encoder.JsonObject;
            }
Example #3
0
            /// <summary>
            /// Adds a field that is a list of objects that implement the
            /// <see cref="T:Dropbox.Api.Babel.IEncodable`1" /> interface.
            /// </summary>
            /// <typeparam name="T">The element type of each list item.</typeparam>
            /// <param name="name">The field name.</param>
            /// <param name="value">The value of the field.</param>
            public void AddFieldObjectList <T>(string name, IEnumerable <T> value)
                where T : IEncodable <T>, new()
            {
                var array = new Json.JsonArray();

                foreach (var item in value)
                {
                    var encoder = new JsonEncoder();
                    item.Encode(encoder);
                    array.Add(encoder.JsonObject);
                }

                this.obj[name] = array;
            }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Context"/> class.
 /// </summary>
 /// <param name="parentEncoder">The parent.</param>
 public Context(JsonEncoder parentEncoder)
 {
     this.parent = parentEncoder;
     this.obj    = new Json.JsonObject();
 }