private JsonFactoryInfo GetJsonFactoryInfo(Type type) { JsonFactoryInfo jsonFactoryInfo = null; if (!JsonFactoryCache.ContainsKey(type)) { MethodInfo jsonFactory = type.GetMethods().FirstOrDefault(x => Attribute.IsDefined(x, typeof(JsonFactoryMethod), true)); if (jsonFactory != null && jsonFactory.GetParameters().Count() == 1) { jsonFactoryInfo = new JsonFactoryInfo() { Factory = jsonFactory, ArgumentType = jsonFactory.GetParameters()[0].ParameterType }; } JsonFactoryCache[type] = jsonFactoryInfo; } else { jsonFactoryInfo = JsonFactoryCache[type]; } return(jsonFactoryInfo); }
private object Read(Type expectedType, bool typeIsHint) { if (expectedType == typeof(Object) || (expectedType != null && expectedType.IsAbstract)) { expectedType = null; } if (expectedType != null) { JsonConstructorInfo jsonConstructorInfo = GetJsonConstructorInfo(expectedType); if (jsonConstructorInfo != null) { Type jsonType = jsonConstructorInfo.ArgumentType; ConstructorInfo jsonConstructor = jsonConstructorInfo.Constructor; object value = this.Read(jsonType, false); return(jsonConstructor.Invoke(new object[] { value })); } JsonFactoryInfo jsonFactoryInfo = GetJsonFactoryInfo(expectedType); if (jsonFactoryInfo != null) { Type jsonType = jsonFactoryInfo.ArgumentType; MethodInfo jsonFactory = jsonFactoryInfo.Factory; object value = this.Read(jsonType, false); return(jsonFactory.Invoke(null, new object[] { value })); } } JsonToken token = this.Tokenize(); switch (token) { case JsonToken.ObjectStart: { return(this.ReadObject(typeIsHint ? null : expectedType)); } case JsonToken.ArrayStart: { return(this.ReadArray(typeIsHint ? null : expectedType)); } case JsonToken.String: { return(this.ReadString(typeIsHint ? null : expectedType)); } case JsonToken.Number: { return(this.ReadNumber(typeIsHint ? null : expectedType)); } case JsonToken.False: { this.index += JsonReader.LiteralFalse.Length; return(false); } case JsonToken.True: { this.index += JsonReader.LiteralTrue.Length; return(true); } case JsonToken.Null: { this.index += JsonReader.LiteralNull.Length; return(null); } case JsonToken.NaN: { this.index += JsonReader.LiteralNotANumber.Length; return(Double.NaN); } case JsonToken.PositiveInfinity: { this.index += JsonReader.LiteralPositiveInfinity.Length; return(Double.PositiveInfinity); } case JsonToken.NegativeInfinity: { this.index += JsonReader.LiteralNegativeInfinity.Length; return(Double.NegativeInfinity); } case JsonToken.Undefined: { this.index += JsonReader.LiteralUndefined.Length; return(null); } case JsonToken.End: default: { return(null); } } }