/// <summary> /// Adds a new converter that can be used to customize how an object is serialized and /// deserialized. /// </summary> public void AddConverter(fsConverter converter) { if (converter.Serializer != null) { throw new InvalidOperationException("Cannot add a single converter instance to " + "multiple JsonConverters -- please construct a new instance for " + converter); } _converters.Insert(0, converter); converter.Serializer = this; // We need to reset our cached converter set, as it could be invalid with the new // converter. Ideally, _cachedConverters should be empty, but there is no guarantee. _cachedConverters = new Dictionary <Type, fsConverter>(); }
/// <summary> /// Adds a new converter that can be used to customize how an object is serialized and /// deserialized. /// </summary> public void AddConverter(fsConverter converter) { if (converter.Serializer != null) { throw new InvalidOperationException("Cannot add a single converter instance to " + "multiple fsConverters -- please construct a new instance for " + converter); } // TODO: wrap inside of a ConverterManager so we can control _converters and _cachedConverters lifetime _converters.Insert(0, converter); converter.Serializer = this; // We need to reset our cached converter set, as it could be invalid with the new // converter. Ideally, _cachedConverters should be empty (as the user should fully setup // the serializer before actually using it), but there is no guarantee. _cachedConverters = new Dictionary <Type, fsConverter>(); }
/// <summary> /// Fetches a converter that can serialize/deserialize the given type. /// </summary> private fsConverter GetConverter(Type type) { fsConverter converter = null; // Check to see if the user has defined a custom converter for the type. If they // have, then we don't need to scan through all of the converters to check which // one can process the type; instead, we directly use the specified converter. var attr = fsPortableReflection.GetAttribute <fsObjectAttribute>(type); if (attr != null && attr.Converter != null) { converter = (fsConverter)Activator.CreateInstance(attr.Converter); converter.Serializer = this; _cachedConverters[type] = converter; } // There is no specific converter specified; try all of the general ones to see // which ones matches. else { if (_cachedConverters.TryGetValue(type, out converter) == false) { for (int i = 0; i < _converters.Count; ++i) { if (_converters[i].CanProcess(type)) { converter = _converters[i]; _cachedConverters[type] = converter; break; } } } } if (converter == null) { throw new InvalidOperationException("Internal error -- could not find a converter for " + type); } return(converter); }
/// <summary> /// Register the given converter for usage in the serializer. /// </summary> public static void AddConverter(fsConverter converter) { _serializer.AddConverter(converter); }