public static void WriteIListString(ITypeSerializer serializer, TextWriter writer, IList <string> list) { writer.Write(JsWriter.ListStartChar); var ranOnce = false; var listLength = list.Count; for (var i = 0; i < listLength; i++) { JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); serializer.WriteString(writer, list[i]); } writer.Write(JsWriter.ListEndChar); }
public static void WriteStringArray(ITypeSerializer serializer, TextWriter writer, object oList) { writer.Write(JsWriter.ListStartChar); var list = (string[])oList; var ranOnce = false; var listLength = list.Length; for (var i = 0; i < listLength; i++) { JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); serializer.WriteString(writer, list[i]); } writer.Write(JsWriter.ListEndChar); }
public static void WriteListString(ITypeSerializer serializer, TextWriter writer, List <string> list) { if (list == null) { writer.Write(JsonUtils.Null); return; } writer.Write(JsWriter.ListStartChar); var ranOnce = false; list.ForEach(x => { JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); serializer.WriteString(writer, x); }); writer.Write(JsWriter.ListEndChar); }
public static void WriteListString(ITypeSerializer serializer, TextWriter writer, List <string> list) { if (!JsState.QueryStringMode) { writer.Write(JsWriter.ListStartChar); } var ranOnce = false; foreach (var x in list) { JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); serializer.WriteString(writer, x); } if (!JsState.QueryStringMode) { writer.Write(JsWriter.ListEndChar); } }
// TODO: optimize ExtractType public static Type ExtractType(StringSegment strType) { if (!strType.HasValue || strType.Length <= 1) { return(null); } var hasWhitespace = Json.JsonUtils.WhiteSpaceChars.Contains(strType.GetChar(1)); if (hasWhitespace) { var pos = strType.IndexOf('"'); if (pos >= 0) { strType = new StringSegment($"{{{strType.Substring(pos, strType.Length - pos)}"); } } var typeAttrInObject = Serializer.TypeAttrInObject; if (strType.Length > typeAttrInObject.Length && strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject) { var propIndex = typeAttrInObject.Length; var typeName = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex)).Value; var type = JsConfig.TypeFinder(typeName); JsWriter.AssertAllowedRuntimeType(type); if (type == null) { Tracer.Instance.WriteWarning($"Could not find type: {typeName}"); return(null); } return(PclExport.Instance.UseType(type)); } return(null); }
//TODO: optimize ExtractType public static Type ExtractType(ReadOnlySpan <char> strType) { if (strType.IsEmpty || strType.Length <= 1) { return(null); } var hasWhitespace = Json.JsonUtils.WhiteSpaceChars.Contains(strType[1]); if (hasWhitespace) { var pos = strType.IndexOf('"'); if (pos >= 0) { strType = ("{" + strType.Substring(pos, strType.Length - pos)).AsSpan(); } } var typeAttrInObject = Serializer.TypeAttrInObject; if (strType.Length > typeAttrInObject.Length && strType.Slice(0, typeAttrInObject.Length).EqualsOrdinal(typeAttrInObject)) { var propIndex = typeAttrInObject.Length; var typeName = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex)).ToString(); var type = JsConfig.TypeFinder(typeName); JsWriter.AssertAllowedRuntimeType(type); if (type == null) { Tracer.Instance.WriteWarning("Could not find type: " + typeName); return(null); } return(ReflectionOptimizer.Instance.UseType(type)); } return(null); }
public static void WriteIDictionary(TextWriter writer, object oMap) { WriteObjectDelegate writeKeyFn = null; WriteObjectDelegate writeValueFn = null; writer.Write(JsWriter.MapStartChar); var map = (IDictionary)oMap; var ranOnce = false; foreach (var key in map.Keys) { var dictionaryValue = map[key]; if (writeKeyFn == null) { writeKeyFn = Serializer.GetWriteFn(key.GetType()); } if (writeValueFn == null) { writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType()); } JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); JsState.WritingKeyCount++; JsState.IsWritingValue = false; writeKeyFn(writer, key); JsState.WritingKeyCount--; writer.Write(JsWriter.MapKeySeperator); JsState.IsWritingValue = true; writeValueFn(writer, dictionaryValue ?? JsWriter.MapNullValue); JsState.IsWritingValue = false; } writer.Write(JsWriter.MapEndChar); }
public static void WriteIEnumerable(TextWriter writer, object oValueCollection) { WriteObjectDelegate toStringFn = null; writer.Write(JsWriter.ListStartChar); var valueCollection = (IEnumerable)oValueCollection; var ranOnce = false; foreach (var valueItem in valueCollection) { if (toStringFn == null) { toStringFn = Serializer.GetWriteFn(valueItem.GetType()); } JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); toStringFn(writer, valueItem); } writer.Write(JsWriter.ListEndChar); }
public static void WriteIEnumerable(TextWriter writer, object oValueCollection) { WriteObjectDelegate toStringFn = null; writer.Write(JsWriter.ListStartChar); var valueCollection = (IEnumerable)oValueCollection; var ranOnce = false; Type lastType = null; foreach (var valueItem in valueCollection) { if ((toStringFn == null) || (valueItem != null && valueItem.GetType() != lastType)) { if (valueItem != null) { if (valueItem.GetType() != lastType) { lastType = valueItem.GetType(); toStringFn = Serializer.GetWriteFn(lastType); } } else { // this can happen if the first item in the collection was null lastType = typeof(object); toStringFn = Serializer.GetWriteFn(lastType); } } JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); toStringFn(writer, valueItem); } writer.Write(JsWriter.ListEndChar); }
public static void WriteGenericIDictionary( TextWriter writer, IDictionary <TKey, TValue> map, WriteObjectDelegate writeKeyFn, WriteObjectDelegate writeValueFn) { writer.Write(JsWriter.MapStartChar); var ranOnce = false; foreach (var kvp in map) { JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); JsState.WritingKeyCount++; JsState.IsWritingValue = false; if (typeof(TKey) != typeof(string)) { JsState.IsWritingKey = true; } writeKeyFn(writer, kvp.Key); if (typeof(TKey) != typeof(string)) { JsState.IsWritingKey = false; } JsState.WritingKeyCount--; writer.Write(JsWriter.MapKeySeperator); JsState.IsWritingValue = true; writeValueFn(writer, kvp.Value); JsState.IsWritingValue = false; } writer.Write(JsWriter.MapEndChar); }
public static void WriteGenericIDictionary( TextWriter writer, IDictionary <TKey, TValue> map, WriteObjectDelegate writeKeyFn, WriteObjectDelegate writeValueFn) { if (map == null) { writer.Write(JsonUtils.Null); return; } writer.Write(JsWriter.MapStartChar); var encodeMapKey = Serializer.GetTypeInfo(typeof(TKey)).EncodeMapKey; var ranOnce = false; foreach (var kvp in map) { var isNull = (kvp.Value == null); if (isNull && !JsConfig.IncludeNullValues) { continue; } JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); JsState.WritingKeyCount++; try { JsState.IsWritingValue = false; if (encodeMapKey) { JsState.IsWritingValue = true; //prevent ""null"" try { writer.Write(JsWriter.QuoteChar); writeKeyFn(writer, kvp.Key); writer.Write(JsWriter.QuoteChar); } finally { JsState.IsWritingValue = false; } } else { writeKeyFn(writer, kvp.Key); } } finally { JsState.WritingKeyCount--; } writer.Write(JsWriter.MapKeySeperator); if (isNull) { writer.Write(JsonUtils.Null); } else { JsState.IsWritingValue = true; try { writeValueFn(writer, kvp.Value); } finally { JsState.IsWritingValue = false; } } } writer.Write(JsWriter.MapEndChar); }
public static void WriteIDictionary(TextWriter writer, object oMap) { WriteObjectDelegate writeKeyFn = null; Type writeKeyFnType = null; WriteObjectDelegate writeValueFn = null; writer.Write(JsWriter.MapStartChar); var encodeMapKey = false; var map = (IDictionary)oMap; var ranOnce = false; foreach (var key in map.Keys) { var dictionaryValue = map[key]; var isNull = (dictionaryValue == null); if (isNull && !JsConfig.IncludeNullValues) { continue; } var keyType = key.GetType(); if (writeKeyFn == null || keyType != writeKeyFnType) { writeKeyFn = Serializer.GetWriteFn(keyType); writeKeyFnType = keyType; encodeMapKey = Serializer.GetTypeInfo(keyType).EncodeMapKey; } if (!isNull) { writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType()); } JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce); JsState.WritingKeyCount++; try { JsState.IsWritingValue = false; if (encodeMapKey) { JsState.IsWritingValue = true; //prevent ""null"" try { writer.Write(JsWriter.QuoteChar); writeKeyFn(writer, key); writer.Write(JsWriter.QuoteChar); } finally { JsState.IsWritingValue = false; } } else { writeKeyFn(writer, key); } } finally { JsState.WritingKeyCount--; } writer.Write(JsWriter.MapKeySeperator); if (isNull) { writer.Write(JsonUtils.Null); } else { JsState.IsWritingValue = true; try { writeValueFn(writer, dictionaryValue); } finally { JsState.IsWritingValue = false; } } } writer.Write(JsWriter.MapEndChar); }
internal static object StringToType( TypeConfig typeConfig, StringSegment strType, EmptyCtorDelegate ctorFn, Dictionary <HashedStringSegment, TypeAccessor> typeAccessorMap) { var index = 0; var type = typeConfig.Type; if (!strType.HasValue) { return(null); } var buffer = strType.Buffer; var offset = strType.Offset; var strTypeLength = strType.Length; //if (!Serializer.EatMapStartChar(strType, ref index)) for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline if (buffer[offset + index] != JsWriter.MapStartChar) { throw DeserializeTypeRef.CreateSerializationError(type, strType.Value); } index++; if (JsonTypeSerializer.IsEmptyMap(strType, index)) { return(ctorFn()); } object instance = null; var propertyResolver = JsConfig.PropertyConvention == PropertyConvention.Lenient ? ParseUtils.LenientPropertyNameResolver : ParseUtils.DefaultPropertyNameResolver; while (index < strTypeLength) { var propertyName = JsonTypeSerializer.ParseJsonString(strType, ref index); //Serializer.EatMapKeySeperator(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline if (strTypeLength != index) { index++; } var propertyValueStr = Serializer.EatValue(strType, ref index); var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1; //if we already have an instance don't check type info, because then we will have a half deserialized object //we could throw here or just use the existing instance. if (instance == null && possibleTypeInfo && propertyName == typeAttr) { var explicitTypeName = Serializer.ParseString(propertyValueStr); var explicitType = JsConfig.TypeFinder(explicitTypeName); if (explicitType == null || explicitType.IsInterface() || explicitType.IsAbstract()) { Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr); } else if (!type.IsAssignableFromType(explicitType)) { Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr); } else { JsWriter.AssertAllowedRuntimeType(explicitType); instance = explicitType.CreateInstance(); } if (instance != null) { //If __type info doesn't match, ignore it. if (!type.InstanceOfType(instance)) { instance = null; } else { var derivedType = instance.GetType(); if (derivedType != type) { var derivedTypeConfig = new TypeConfig(derivedType); var map = DeserializeTypeRef.GetTypeAccessorMap(derivedTypeConfig, Serializer); if (map != null) { typeAccessorMap = map; } } } } Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); continue; } if (instance == null) { instance = ctorFn(); } var typeAccessor = propertyResolver.GetTypeAccessorForProperty(propertyName, typeAccessorMap); var propType = possibleTypeInfo && propertyValueStr.GetChar(0) == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null; if (propType != null) { try { if (typeAccessor != null) { //var parseFn = Serializer.GetParseFn(propType); var parseFn = JsonReader.GetParseStringSegmentFn(propType); var propertyValue = parseFn(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.Value, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline if (index != strTypeLength) { var success = buffer[offset + index] == JsWriter.ItemSeperator || buffer[offset + index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline } } continue; } catch (Exception e) { if (JsConfig.OnDeserializationError != null) { JsConfig.OnDeserializationError(instance, propType, propertyName.Value, propertyValueStr.Value, e); } if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName.Value, propertyValueStr.Value, propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr.Value); } } } if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null) { try { var propertyValue = typeAccessor.GetProperty(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.Value, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } catch (NotSupportedException) { throw; } catch (Exception e) { if (JsConfig.OnDeserializationError != null) { JsConfig.OnDeserializationError(instance, propType ?? typeAccessor.PropertyType, propertyName.Value, propertyValueStr.Value, e); } if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName.Value, propertyValueStr.Value, typeAccessor.PropertyType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr.Value); } } } else { // the property is not known by the DTO typeConfig.OnDeserializing?.Invoke(instance, propertyName.Value, propertyValueStr.Value); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline if (index != strType.Length) { var success = buffer[offset + index] == JsWriter.ItemSeperator || buffer[offset + index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[offset + index])) { break; } } //Whitespace inline } } } return(instance); }
internal static object StringToType(ReadOnlySpan <char> strType, TypeConfig typeConfig, EmptyCtorDelegate ctorFn, KeyValuePair <string, TypeAccessor>[] typeAccessors) { var index = 0; var type = typeConfig.Type; if (strType.IsEmpty) { return(null); } //if (!Serializer.EatMapStartChar(strType, ref index)) if (strType[index++] != JsWriter.MapStartChar) { throw DeserializeTypeRef.CreateSerializationError(type, strType.ToString()); } if (JsonTypeSerializer.IsEmptyMap(strType)) { return(ctorFn()); } var config = JsConfig.GetConfig(); object instance = null; var lenient = config.PropertyConvention == PropertyConvention.Lenient; var strTypeLength = strType.Length; while (index < strTypeLength) { var propertyName = Serializer.EatMapKey(strType, ref index).Trim(); //Serializer.EatMapKeySeperator(strType, ref index); index++; var propertyValueStr = Serializer.EatValue(strType, ref index); var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1; if (possibleTypeInfo && propertyName.Equals(typeAttr.Span, StringComparison.OrdinalIgnoreCase)) { var explicitTypeName = Serializer.ParseString(propertyValueStr); var explicitType = config.TypeFinder(explicitTypeName); if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract) { Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr.ToString()); } else if (!type.IsAssignableFrom(explicitType)) { Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr.ToString()); } else { JsWriter.AssertAllowedRuntimeType(explicitType); instance = ActivatorUtils.FastCreateInstance(explicitType); } if (instance != null) { //If __type info doesn't match, ignore it. if (!type.IsInstanceOfType(instance)) { instance = null; } else { var derivedType = instance.GetType(); if (derivedType != type) { var map = DeserializeTypeRef.GetCachedTypeAccessors(derivedType, Serializer); if (map != null) { typeAccessors = map; } } } } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } continue; } if (instance == null) { instance = ctorFn(); } var typeAccessor = typeAccessors.Get(propertyName, lenient); var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null; if (propType != null) { try { if (typeAccessor != null) { var parseFn = Serializer.GetParseStringSpanFn(propType); var propertyValue = parseFn(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } continue; } catch (Exception e) { config.OnDeserializationError?.Invoke(instance, propType, propertyName.ToString(), propertyValueStr.ToString(), e); if (config.ThrowOnError) { throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString()); } } } if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null) { try { var propertyValue = typeAccessor.GetProperty(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } catch (NotSupportedException) { throw; } catch (Exception e) { config.OnDeserializationError?.Invoke(instance, propType ?? typeAccessor.PropertyType, propertyName.ToString(), propertyValueStr.ToString(), e); if (config.ThrowOnError) { throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString()); } } } else { // the property is not known by the DTO typeConfig.OnDeserializing?.Invoke(instance, propertyName.ToString(), propertyValueStr.ToString()); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } } return(instance); }
internal static object StringToType( TypeConfig typeConfig, StringSegment strType, EmptyCtorDelegate ctorFn, Dictionary <HashedStringSegment, TypeAccessor> typeAccessorMap) { var index = 0; var type = typeConfig.Type; if (!strType.HasValue) { return(null); } // if (!Serializer.EatMapStartChar(strType, ref index)) if (strType.GetChar(index++) != JsWriter.MapStartChar) { throw DeserializeTypeRef.CreateSerializationError(type, strType.Value); } if (JsonTypeSerializer.IsEmptyMap(strType)) { return(ctorFn()); } object instance = null; var propertyResolver = JsConfig.PropertyConvention == PropertyConvention.Lenient ? ParseUtils.LenientPropertyNameResolver : ParseUtils.DefaultPropertyNameResolver; var strTypeLength = strType.Length; while (index < strTypeLength) { var propertyName = Serializer.EatMapKey(strType, ref index); // Serializer.EatMapKeySeperator(strType, ref index); index++; var propertyValueStr = Serializer.EatValue(strType, ref index); var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1; if (possibleTypeInfo && propertyName == new StringSegment(JsWriter.TypeAttr)) { var explicitTypeName = Serializer.ParseString(propertyValueStr); var explicitType = JsConfig.TypeFinder(explicitTypeName); if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract) { Tracer.Instance.WriteWarning($"Could not find type: {propertyValueStr}"); } else if (!type.IsAssignableFrom(explicitType)) { Tracer.Instance.WriteWarning($"Could not assign type: {propertyValueStr}"); } else { JsWriter.AssertAllowedRuntimeType(explicitType); instance = explicitType.CreateInstance(); } if (instance != null) { // If __type info doesn't match, ignore it. if (!type.IsInstanceOfType(instance)) { instance = null; } else { var derivedType = instance.GetType(); if (derivedType != type) { var derivedTypeConfig = new TypeConfig(derivedType); var map = DeserializeTypeRef.GetTypeAccessorMap(derivedTypeConfig, Serializer); if (map != null) { typeAccessorMap = map; } } } } // Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } continue; } if (instance == null) { instance = ctorFn(); } var typeAccessor = propertyResolver.GetTypeAccessorForProperty(propertyName, typeAccessorMap); var propType = possibleTypeInfo && propertyValueStr.GetChar(0) == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null; if (propType != null) { try { if (typeAccessor != null) { var parseFn = Serializer.GetParseStringSegmentFn(propType); var propertyValue = parseFn(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.Value, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } // Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } continue; } catch (Exception e) { if (JsConfig.OnDeserializationError != null) { JsConfig.OnDeserializationError(instance, propType, propertyName.Value, propertyValueStr.Value, e); } if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName.Value, propertyValueStr.Value, propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr); } } } if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null) { try { var propertyValue = typeAccessor.GetProperty(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.Value, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } catch (NotSupportedException) { throw; } catch (Exception e) { if (JsConfig.OnDeserializationError != null) { JsConfig.OnDeserializationError(instance, propType ?? typeAccessor.PropertyType, propertyName.Value, propertyValueStr.Value, e); } if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName.Value, propertyValueStr.Value, propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr); } } } else { // the property is not known by the DTO typeConfig.OnDeserializing?.Invoke(instance, propertyName.Value, propertyValueStr); } // Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); if (index != strType.Length) { index++; } } return(instance); }
internal static object StringToType(ReadOnlySpan <char> strType, TypeConfig typeConfig, EmptyCtorDelegate ctorFn, KeyValuePair <string, TypeAccessor>[] typeAccessors) { var index = 0; var type = typeConfig.Type; if (strType.IsEmpty) { return(null); } var buffer = strType; var strTypeLength = strType.Length; //if (!Serializer.EatMapStartChar(strType, ref index)) for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline if (buffer[index] != JsWriter.MapStartChar) { throw DeserializeTypeRef.CreateSerializationError(type, strType.ToString()); } index++; if (JsonTypeSerializer.IsEmptyMap(strType, index)) { return(ctorFn()); } var config = JsConfig.GetConfig(); object instance = null; var lenient = config.PropertyConvention == PropertyConvention.Lenient; for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline while (index < strTypeLength) { var propertyName = JsonTypeSerializer.UnescapeJsString(strType, JsonUtils.QuoteChar, removeQuotes: true, ref index); //Serializer.EatMapKeySeperator(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline if (strTypeLength != index) { index++; } var propertyValueStr = Serializer.EatValue(strType, ref index); var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1; //if we already have an instance don't check type info, because then we will have a half deserialized object //we could throw here or just use the existing instance. if (instance == null && possibleTypeInfo && propertyName.Equals(typeAttr.Span, StringComparison.OrdinalIgnoreCase)) { var explicitTypeName = Serializer.ParseString(propertyValueStr); var explicitType = config.TypeFinder(explicitTypeName); if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract) { Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr.ToString()); } else if (!type.IsAssignableFrom(explicitType)) { Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr.ToString()); } else { JsWriter.AssertAllowedRuntimeType(explicitType); instance = explicitType.CreateInstance(); } if (instance != null) { //If __type info doesn't match, ignore it. if (!type.IsInstanceOfType(instance)) { instance = null; } else { var derivedType = instance.GetType(); if (derivedType != type) { var map = DeserializeTypeRef.GetCachedTypeAccessors(derivedType, Serializer); if (map != null) { typeAccessors = map; } } } } Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); continue; } if (instance == null) { instance = ctorFn(); } var typeAccessor = typeAccessors.Get(propertyName, lenient); var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null; if (propType != null) { try { if (typeAccessor != null) { //var parseFn = Serializer.GetParseFn(propType); var parseFn = JsonReader.GetParseStringSpanFn(propType); var propertyValue = parseFn(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline if (index != strTypeLength) { var success = buffer[index] == JsWriter.ItemSeperator || buffer[index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline } } continue; } catch (Exception e) { config.OnDeserializationError?.Invoke(instance, propType, propertyName.ToString(), propertyValueStr.ToString(), e); if (config.ThrowOnError) { throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString()); } } } if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null) { try { var propertyValue = typeAccessor.GetProperty(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } catch (NotSupportedException) { throw; } catch (Exception e) { config.OnDeserializationError?.Invoke(instance, propType ?? typeAccessor.PropertyType, propertyName.ToString(), propertyValueStr.ToString(), e); if (config.ThrowOnError) { throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), typeAccessor.PropertyType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString()); } } } else { // the property is not known by the DTO typeConfig.OnDeserializing?.Invoke(instance, propertyName.ToString(), propertyValueStr.ToString()); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline if (index != strType.Length) { var success = buffer[index] == JsWriter.ItemSeperator || buffer[index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strTypeLength; index++) { if (!JsonUtils.IsWhiteSpace(buffer[index])) { break; } } //Whitespace inline } } } return(instance); }