// Edited to fix issues with null List<Guid> properties in response objects public static void RegisterElement <T, TElement>() { RegisterBuiltin <TElement>(); DeserializeDictionary <JsonTypeSerializer> .ParseDictionary <T, TElement>(null, null, null, null); DeserializeDictionary <JsonTypeSerializer> .ParseDictionary <TElement, T>(null, null, null, null); ToStringDictionaryMethods <T, TElement, JsonTypeSerializer> .WriteIDictionary(null, null, null, null); ToStringDictionaryMethods <TElement, T, JsonTypeSerializer> .WriteIDictionary(null, null, null, null); // Include List deserialisations from the Register<> method above. This solves issue where List<Guid> properties on responses deserialise to null. // No idea why this is happening because there is no visible exception raised. Suspect MonoTouch is swallowing an AOT exception somewhere. DeserializeArrayWithElements <TElement, JsonTypeSerializer> .ParseGenericArray(null, null); DeserializeListWithElements <TElement, JsonTypeSerializer> .ParseGenericList(null, null, null); // Cannot use the line below for some unknown reason - when trying to compile to run on device, mtouch bombs during native code compile. // Something about this line or its inner workings is offensive to mtouch. Luckily this was not needed for my List<Guide> issue. // DeserializeCollection<JsonTypeSerializer>.ParseCollection<TElement>(null, null, null); TranslateListWithElements <TElement> .LateBoundTranslateToGenericICollection(null, typeof(List <TElement>)); TranslateListWithConvertibleElements <TElement, TElement> .LateBoundTranslateToGenericICollection(null, typeof(List <TElement>)); }
/// <summary> /// Objects the type of the string to. /// </summary> /// <param name="strType">Type of the string.</param> /// <returns>System.Object.</returns> public static object ObjectStringToType(ReadOnlySpan <char> strType) { var type = ExtractType(strType); if (type != null) { var parseFn = Serializer.GetParseStringSpanFn(type); var propertyValue = parseFn(strType); return(propertyValue); } var config = JsConfig.GetConfig(); if (config.ConvertObjectTypesIntoStringDictionary && !strType.IsNullOrEmpty()) { var firstChar = strType[0]; var endChar = strType[strType.Length - 1]; switch (firstChar) { case JsWriter.MapStartChar when endChar == JsWriter.MapEndChar: { var dynamicMatch = DeserializeDictionary <TSerializer> .ParseDictionary <string, object>(strType, null, v => Serializer.UnescapeString(v).ToString(), v => Serializer.UnescapeString(v).ToString()); if (dynamicMatch is { Count : > 0 }) { return(dynamicMatch); } break; }
public static void RegisterElement <T, TElement>() { RegisterBuiltin <TElement>(); DeserializeDictionary <JsonTypeSerializer> .ParseDictionary <T, TElement>(null, null, null, null); DeserializeDictionary <JsonTypeSerializer> .ParseDictionary <TElement, T>(null, null, null, null); ToStringDictionaryMethods <T, TElement, JsonTypeSerializer> .WriteIDictionary(null, null, null, null); ToStringDictionaryMethods <TElement, T, JsonTypeSerializer> .WriteIDictionary(null, null, null, null); TranslateListWithElements <TElement> .LateBoundTranslateToGenericICollection(null, typeof(List <TElement>)); TranslateListWithConvertibleElements <TElement, TElement> .LateBoundTranslateToGenericICollection(null, typeof(List <TElement>)); }
/// <summary> /// Gets the core parse string span function. /// </summary> /// <typeparam name="T"></typeparam> /// <returns>ParseStringSpanDelegate.</returns> private static ParseStringSpanDelegate GetCoreParseStringSpanFn <T>() { var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); if (JsConfig <T> .HasDeserializeFn) { return(value => JsConfig <T> .ParseFn(Serializer, value.Value())); } if (type.IsEnum) { return(x => ParseUtils.TryParseEnum(type, Serializer.UnescapeSafeString(x).Value())); } if (type == typeof(string)) { return(Serializer.UnescapeStringAsObject); } if (type == typeof(object)) { return(DeserializeType <TSerializer> .ObjectStringToType); } var specialParseFn = ParseUtils.GetSpecialParseMethod(type); if (specialParseFn != null) { return(v => specialParseFn(v.Value())); } if (type.IsArray) { return(DeserializeArray <T, TSerializer> .ParseStringSpan); } var builtInMethod = DeserializeBuiltin <T> .ParseStringSpan; if (builtInMethod != null) { return(value => builtInMethod(Serializer.UnescapeSafeString(value))); } if (type.HasGenericType()) { if (type.IsOrHasGenericInterfaceTypeOf(typeof(IList <>))) { return(DeserializeList <T, TSerializer> .ParseStringSpan); } if (type.IsOrHasGenericInterfaceTypeOf(typeof(IDictionary <,>))) { return(DeserializeDictionary <TSerializer> .GetParseStringSpanMethod(type)); } if (type.IsOrHasGenericInterfaceTypeOf(typeof(ICollection <>))) { return(DeserializeCollection <TSerializer> .GetParseStringSpanMethod(type)); } if (type.HasAnyTypeDefinitionsOf(typeof(Queue <>)) || type.HasAnyTypeDefinitionsOf(typeof(Stack <>))) { return(DeserializeSpecializedCollections <T, TSerializer> .ParseStringSpan); } if (type.IsOrHasGenericInterfaceTypeOf(typeof(KeyValuePair <,>))) { return(DeserializeKeyValuePair <TSerializer> .GetParseStringSpanMethod(type)); } if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>))) { return(DeserializeEnumerable <T, TSerializer> .ParseStringSpan); } var customFn = DeserializeCustomGenericType <TSerializer> .GetParseStringSpanMethod(type); if (customFn != null) { return(customFn); } } var pclParseFn = PclExport.Instance.GetJsReaderParseStringSpanMethod <TSerializer>(typeof(T)); if (pclParseFn != null) { return(pclParseFn); } var isDictionary = typeof(T) != typeof(IEnumerable) && typeof(T) != typeof(ICollection) && (typeof(T).IsAssignableFrom(typeof(IDictionary)) || typeof(T).HasInterface(typeof(IDictionary))); if (isDictionary) { return(DeserializeDictionary <TSerializer> .GetParseStringSpanMethod(type)); } var isEnumerable = typeof(T).IsAssignableFrom(typeof(IEnumerable)) || typeof(T).HasInterface(typeof(IEnumerable)); if (isEnumerable) { var parseFn = DeserializeSpecializedCollections <T, TSerializer> .ParseStringSpan; if (parseFn != null) { return(parseFn); } } if (type.IsValueType) { //at first try to find more faster `ParseStringSpan` method var staticParseStringSpanMethod = StaticParseMethod <T> .ParseStringSpan; if (staticParseStringSpanMethod != null) { return(value => staticParseStringSpanMethod(Serializer.UnescapeSafeString(value))); } //then try to find `Parse` method var staticParseMethod = StaticParseMethod <T> .Parse; if (staticParseMethod != null) { return(value => staticParseMethod(Serializer.UnescapeSafeString(value).ToString())); } } else { var staticParseStringSpanMethod = StaticParseRefTypeMethod <TSerializer, T> .ParseStringSpan; if (staticParseStringSpanMethod != null) { return(value => staticParseStringSpanMethod(Serializer.UnescapeSafeString(value))); } var staticParseMethod = StaticParseRefTypeMethod <TSerializer, T> .Parse; if (staticParseMethod != null) { return(value => staticParseMethod(Serializer.UnescapeSafeString(value).ToString())); } } var typeConstructor = DeserializeType <TSerializer> .GetParseStringSpanMethod(TypeConfig <T> .GetState()); if (typeConstructor != null) { return(typeConstructor); } var stringConstructor = DeserializeTypeUtils.GetParseStringSpanMethod(type); return(stringConstructor ?? DeserializeType <TSerializer> .ParseAbstractType <T>); }