/// <summary> /// Deserializes a json to a type /// </summary> /// <typeparam name="T">Type to deserialize into</typeparam> /// <param name="json">Json string to deserialize</param> /// <returns>deserialized type</returns> public T Deserialize <T>(string json) { try { var dataType = typeof(T); if (!TryGetValueOfTypeGo(dataType, out object typeGoInfo)) { typeGoInfo = BaseTypeGoInfo.Generate <T>(Options); } var reader = new JsonSpanReader(json.AsSpan()); JsonDeserializer deserializer = this; var result = FastDeserializerExtractor <T> .Extract(ref deserializer, (TypeGoInfo <T>) typeGoInfo, ref reader); return(result); } finally { //DeSerializedObjects.Clear(); } }
internal static void ExtractProperty(ref T instance, ref JsonDeserializer deserializer, BasePropertyGoInfo <T> basePropertyGo, ref JsonSpanReader json) { char character = json.Read(); if (character == JsonConstantsString.Quotes) { basePropertyGo.JsonDeserializeString(ref instance, ref json); //return typeGo.JsonDeserialize(ref extract); } else if (character == JsonConstantsString.OpenBraket) { basePropertyGo.JsonDeserializeObject(ref instance, ref deserializer, ref json); } else if (character == JsonConstantsString.OpenSquareBrackets) { basePropertyGo.JsonDeserializeArray(ref instance, ref deserializer, ref json); } else { basePropertyGo.JsonDeserializeValue(ref instance, ref json); //var value = json.ExtractValue(); //if (value[value.Length - 1] == JsonConstantsString.Comma) // value = value.Slice(0, value.Length - 1); //if (typeGo == null || typeGo.JsonDeserialize == null) //{ // return new string(value); //} //return typeGo.JsonDeserialize(ref value); } }
internal static T ExtractArray(JsonDeserializer deserializer, TypeGoInfo <T> typeGo, ref JsonSpanReader json) { //typeGo.JsonDeserialize(ref json); //var generic = typeGo.Generics.First(); while (true) { char character = json.Read(); if (character == JsonConstantsString.OpenBraket) { //var obj = ExtractOject(ref deserializer, ref typeGo, ref json); //typeGo.AddArrayValue(arrayInstance, obj); } else if (character == JsonConstantsString.OpenSquareBrackets) { //var obj = ExtractArray(deserializer, generic, ref json); //typeGo.AddArrayValue(arrayInstance, obj); } else if (character == JsonConstantsString.Quotes) { //var value = json.ExtractString(); //typeGo.AddArrayValue(arrayInstance, generic.JsonDeserialize(deserializer, value)); } else if (character == JsonConstantsString.Comma) { continue; } else if (character == JsonConstantsString.CloseSquareBrackets) { break; } else { //bool isClosed = false; //var value = json.ExtractValue(); //if (value[value.Length - 1] == JsonConstantsString.Comma) // value = value.Slice(0, value.Length - 1); //if (value[value.Length - 1] == JsonConstantsString.CloseSquareBrackets) //{ // value = value.Slice(0, value.Length - 1); // isClosed = true; //} //if (generic.JsonDeserialize != null) // typeGo.AddArrayValue(arrayInstance, generic.JsonDeserialize(deserializer, value)); //if (isClosed) // break; } } return(default);
/// <summary> /// Deserialize json /// </summary> /// <param name="deserializer"></param> /// <param name="typeGo"></param> /// <param name="json">json object to deserialize</param> /// <returns>The deserialized value</returns> internal static T Extract(ref JsonDeserializer deserializer, TypeGoInfo <T> typeGo, ref JsonSpanReader json) { char character = json.Read(); if (character == JsonConstantsString.Quotes) { ReadOnlySpan <char> extract = json.ExtractString(); return(typeGo.JsonDeserialize(ref extract)); } else if (character == JsonConstantsString.OpenBraket) { return(ExtractOject(ref deserializer, ref typeGo, ref json)); } else if (character == JsonConstantsString.OpenSquareBrackets) { return(ExtractArray(deserializer, typeGo, ref json)); } else { ReadOnlySpan <char> value = json.ExtractValue(); return(typeGo.JsonDeserialize(ref value)); } }