コード例 #1
0
        /// <summary>
        /// Parses JSON read from the given text reader and converts it into
        /// an object of the given type. The resulting output can be cast
        /// safely to that given type.
        /// </summary>
        /// <param name="reader">The TextReader to read JSON from.</param>
        /// <param name="modelType">The type of the result object.</param>
        /// <returns>The result object. For class types, this can be null.</returns>
        public static object Parse(TextReader reader, Type modelType)
        {
            var consumer = new ModelJsonConsumer(ConverterRegistry.Get(modelType));

            Parse(reader, consumer);
            return(consumer.Result);
        }
コード例 #2
0
        /// <summary>
        /// Converts this generic object model to another .NET object of the given <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The type of the .NET object to convert to.</param>
        /// <returns>An instance of the given <paramref name="type"/>.</returns>
        public object ToModel(Type type)
        {
            var consumer = new ModelJsonConsumer(ConverterRegistry.Get(type));

            Write(consumer);
            return(consumer.Result);
        }
コード例 #3
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);
     }
 }
コード例 #4
0
        /// <summary>
        /// Converts the given .NET object to the generic JSON object model represented
        /// by <see cref="JsonValue"/> and deriving classes.
        /// </summary>
        /// <param name="model">The .NET object to read from.</param>
        /// <returns>The resulting <see cref="JsonValue"/>.</returns>
        public static JsonValue FromModel(object model)
        {
            if (model == null)
            {
                return(new JsonNull());
            }

            var        consumer  = new JsonValueJsonConsumer();
            IConverter converter = ConverterRegistry.Get(model.GetType());

            converter.Write(model, consumer);
            return(consumer.Result);
        }
コード例 #5
0
        private IConverter GetConverterForType(Type type)
        {
            if (type == this.Type)
            {
                return(mBaseClassConverter);
            }
            else
            {
                var converter = ConverterRegistry.Get(type);
                if (!(converter is TypedConverter typedConverter))
                {
                    throw new Exception($"Expected converter registry to return an instance of {nameof(TypedConverter)} for type '{type}'.");
                }

                return(typedConverter.mBaseClassConverter);
            }
        }
コード例 #6
0
        private static IConverter GetConverter(IConverterFactory factory, Type type, object[] converterParameters)
        {
            if (factory != null)
            {
                if (!factory.CanConvert(type))
                {
                    throw new Exception($"Converter factory {factory.GetType()} is not able to convert type '{type}'.");
                }

                IConverter converter = factory.Create(type, converterParameters);
                if (converter == null)
                {
                    throw new Exception($"{nameof(IConverterFactory.Create)} implementation of {factory.GetType()} did not return a converter for type {type}.");
                }

                return(converter);
            }

            return(ConverterRegistry.Get(type, converterParameters));
        }
コード例 #7
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);
         }
     }
 }
コード例 #8
0
 public Converter(Type nullableType)
     : base(nullableType)
 {
     valueTypeConverter = ConverterRegistry.Get(nullableType.GenericTypeArguments[0]);
 }