private JsonConstructorInfo GetJsonConstructorInfo(Type type) { JsonConstructorInfo jsonConstructorInfo = null; if (!JsonConstructorCache.ContainsKey(type)) { ConstructorInfo jsonConstructor = type.GetConstructors().FirstOrDefault(x => Attribute.IsDefined(x, typeof(JsonConstructor), true)); if (jsonConstructor != null && jsonConstructor.GetParameters().Count() == 1) { jsonConstructorInfo = new JsonConstructorInfo() { Constructor = jsonConstructor, ArgumentType = jsonConstructor.GetParameters()[0].ParameterType }; } JsonConstructorCache[type] = jsonConstructorInfo; } else { jsonConstructorInfo = JsonConstructorCache[type]; } return(jsonConstructorInfo); }
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); } } }