コード例 #1
0
        public override void Write(object value, IJsonConsumer writer)
        {
            if (value == null)
            {
                writer.Null();
                return;
            }

            var objConsumer = writer.Object();

            foreach (var property in mPropList)
            {
                object propValue = property.Getter(value);
                if (propValue == null)
                {
                    if (!property.EmitNullValue)
                    {
                        continue;
                    }
                }
                else if (property.SuppressDefaultValue && Equals(propValue, property.DefaultValue))
                {
                    continue;
                }

                objConsumer.PropertyName(property.Name);
                property.Converter.Write(propValue, objConsumer);
            }

            objConsumer.Done();
        }
コード例 #2
0
        /// <summary>
        /// Writes data of the .NET object to the given <see cref="IJsonConsumer"/>
        /// in order to convert it to a JSON string or to any other representation of
        /// JSON data.
        /// </summary>
        /// <param name="value">The .NET object to write. The object type must match
        /// the type of the converter.</param>
        /// <param name="writer">The JSON consumer which will be used to write
        /// the data to according to the contents of the given value.</param>
        public sealed override void Write(object value, IJsonConsumer writer)
        {
            if (mNullable && value == null)
            {
                writer.Null();
                return;
            }

            InternalWrite((T)value, writer);
        }
コード例 #3
0
            public override void Write(object value, IJsonConsumer writer)
            {
                if (value == null)
                {
                    writer.Null();
                    return;
                }

                valueTypeConverter.Write(value, writer);
            }
コード例 #4
0
 /// <summary>
 /// Serializes the given object into the given consumer.
 /// </summary>
 /// <param name="model">The object to serialize. This may be null or a primitive.</param>
 /// <param name="consumer">An instance of IJsonConsumer which receives all data from
 /// the given object.</param>
 public static void Write(object model, IJsonConsumer consumer)
 {
     if (model == null)
     {
         consumer.Null();
     }
     else
     {
         ConverterRegistry.Get(model.GetType()).Write(model, consumer);
     }
 }
コード例 #5
0
 public override void Write(object value, IJsonConsumer writer)
 {
     if (value == null)
     {
         writer.Null();
     }
     else
     {
         writer.String(System.Convert.ToBase64String((byte[])value, System.Base64FormattingOptions.None));
     }
 }
コード例 #6
0
 /// <summary>
 /// Writes the string or null value to the <see cref="IJsonConsumer"/>.
 /// </summary>
 /// <param name="value">The string value or null.</param>
 /// <param name="writer">The <see cref="IJsonConsumer"/></param>
 public override void Write(object value, IJsonConsumer writer)
 {
     if (value == null)
     {
         writer.Null();
     }
     else
     {
         writer.String((string)value);
     }
 }
コード例 #7
0
            public static void ParseValue(Tokenizer tokenizer, IJsonConsumer consumer)
            {
                if (tokenizer.CurrentToken.Type == Tokenizer.Token.None)
                {
                    throw new ParserException($"Unexpected end of stream reached in line {tokenizer.CurrentToken.LineNo} at position {tokenizer.CurrentToken.Position}.", tokenizer.CurrentToken.LineNo, tokenizer.CurrentToken.Position);
                }

                if (tokenizer.CurrentToken.Type == Tokenizer.Token.CurlyOpen)
                {
                    ParseObject(tokenizer, consumer.Object());
                    return;
                }
                if (tokenizer.CurrentToken.Type == Tokenizer.Token.SquaredOpen)
                {
                    ParseArray(tokenizer, consumer.Array());
                    return;
                }

                if (tokenizer.CurrentToken.Type == Tokenizer.Token.String)
                {
                    consumer.String(tokenizer.CurrentToken.StringValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.Boolean)
                {
                    consumer.Boolean(tokenizer.CurrentToken.BooleanValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberInteger)
                {
                    consumer.Number(tokenizer.CurrentToken.IntegerValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberUnsignedInteger)
                {
                    consumer.Number(tokenizer.CurrentToken.UnsignedIntegerValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberFloat)
                {
                    consumer.Number(tokenizer.CurrentToken.FloatValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.Null)
                {
                    consumer.Null();
                }
                else
                {
                    throw new ParserException($"Expected value in line {tokenizer.CurrentToken.LineNo} at position {tokenizer.CurrentToken.Position}, but found '{tokenizer.CurrentToken}'.", tokenizer.CurrentToken.LineNo, tokenizer.CurrentToken.Position);
                }

                tokenizer.MoveNext(); // skip value literal
            }
コード例 #8
0
        /// <summary>
        /// Writes data of the .NET object to the given <see cref="IJsonConsumer"/>
        /// in order to convert it to a JSON string or to any other representation of
        /// JSON data. This implementation expects <paramref name="value"/> to implement
        /// <see cref="IEnumerable"/> and creates a JSON array from it. The <paramref name="value"/>
        /// may be null, though.
        /// </summary>
        /// <param name="value">The .NET object to write. It may be null or an instance of
        /// <see cref="IEnumerable"/>.</param>
        /// <param name="writer">The JSON consumer which will be used to write the array to.</param>
        public override void Write(object value, IJsonConsumer writer)
        {
            if (value == null)
            {
                writer.Null();
                return;
            }

            var arrayConsumer = writer.Array();

            foreach (object item in (IEnumerable)value)
            {
                ElementConverter.Write(item, arrayConsumer);
            }
            arrayConsumer.Done();
        }
コード例 #9
0
        public override void Write(object value, IJsonConsumer writer)
        {
            if (value == null)
            {
                writer.Null();
                return;
            }

            IJsonObjectConsumer objectConsumer = writer.Object();

            foreach (KeyValuePair <string, T> kvp in (Dictionary <string, T>)value)
            {
                objectConsumer.PropertyName(kvp.Key);
                ElementConverter.Write(kvp.Value, objectConsumer);
            }
            objectConsumer.Done();
        }
コード例 #10
0
        /// <summary>
        /// Writes data of the .NET object to the given <see cref="IJsonConsumer"/>
        /// in order to convert it to a JSON string or to any other representation of
        /// JSON data.
        /// </summary>
        /// <param name="value">The .NET object to write. Implementors may assume
        /// that the type of the incoming value can be cast to the target type
        /// of this converter, however, it may be null.</param>
        /// <param name="writer">The JSON consumer which must be used to write
        /// the data to according to the contents of the given value.</param>
        public override void Write(object value, IJsonConsumer writer)
        {
            if (value == null)
            {
                writer.Null();
                return;
            }

            Type type = value.GetType();

            if (!mTypeNameResolver.TryGetTypeName(type, out string typeName) && type != mTypeNameResolver.DefaultType)
            {
                throw new Exception($"Type '{type}' is unknown.");
            }

            IConverter converter = GetConverterForType(type);

            converter.Write(value, new WritingConsumer(typeName, mTypeProperty, type, writer));
        }
コード例 #11
0
 public override void Write(object value, IJsonConsumer writer)
 {
     if (value == null)
     {
         writer.Null();
     }
     else
     {
         Type type = value.GetType();
         if (type == typeof(object)) // prevent recursion
         {
             writer.Object().Done();
         }
         else
         {
             IConverter converter = ConverterRegistry.Get(type);
             converter.Write(value, writer);
         }
     }
 }
コード例 #12
0
ファイル: JsonNull.cs プロジェクト: pupsette/CompactJson
 /// <summary>
 /// Writes null to a <see cref="IJsonConsumer"/>.
 /// This is also used internally by <see cref="JsonValue.ToModel(System.Type)"/> and
 /// <see cref="JsonValue.ToModel{T}"/> in order to convert this generic object
 /// model to a JSON string or another .NET object.
 /// </summary>
 /// <param name="consumer">The consumer.</param>
 public override void Write(IJsonConsumer consumer)
 {
     consumer.Null();
 }
コード例 #13
0
 public void Null()
 {
     mWrappedConsumer.Null();
 }