示例#1
0
        public Task <object> DeserializeAsync(Type type, SmppReader reader, SmppSerializationSettings serializationSettings, CancellationToken cancellationToken)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (serializationSettings == null)
            {
                throw new ArgumentNullException(nameof(serializationSettings));
            }

            if (serializationSettings.SerializerResolver == null)
            {
                throw new InvalidOperationException($"Cannot resolve the serializer for type {type}: the resolver is not set");
            }

            var serializer = serializationSettings.SerializerResolver.ResolveForType(type);

            // TODO: if serializer not found, try to construct it from the type ...
            if (serializer == null)
            {
                throw new InvalidOperationException($"No serializer was resolved for the type {type} from the available ones");
            }

            return(serializer.DeserializeAsync(reader, serializationSettings, cancellationToken));
        }
示例#2
0
        public static ISmppSerializer GetForType(Type type, SmppSerializationSettings serializationSettings)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (Attribute.GetCustomAttribute(type, typeof(SerializableAttribute), false) is SmppSerializerAttribute attr)
            {
                return(GetFromAttribute(attr));
            }

            var schema = serializationSettings.Schemata[type];

            if (schema == null)
            {
                schema = SmppObjectSchema.CreateFrom(type);
            }

            if (schema == null)
            {
                throw new InvalidOperationException($"Could not find or generate a schema for the type {type}");
            }

            return(new SchemaBasedSerializer(schema));
        }
示例#3
0
        public Task SerializeAsync(object obj, SmppWriter writer, SmppSerializationSettings serializationSettings, CancellationToken cancellationToken)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj), "Cannot determine the type from a null object");
            }

            return(SerializeAsync(obj.GetType(), obj, writer, serializationSettings, cancellationToken));
        }
示例#4
0
        static SmppSerializationSettings()
        {
            var settings = new SmppSerializationSettings {
                DefaultEncoding = Encoding.ASCII
            };

            settings.Serializers.Add(new BooleanSerializer());
            settings.Serializers.Add(new Int16Serializer());
            settings.Serializers.Add(new Int32Serializer());
            settings.Serializers.Add(new StringSerializer());
            settings.Serializers.Add(new ByteSerializer());
            settings.Serializers.Add(new SmppTimeSerializer());

            settings.SerializerResolver = new DefaultSerializerResolver(settings);

            Default = settings;
        }
示例#5
0
        public Task SerializeAsync(Type type, object obj, SmppWriter writer, SmppSerializationSettings serializationSettings,
                                   CancellationToken cancellationToken)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (serializationSettings == null)
            {
                throw new ArgumentNullException(nameof(serializationSettings));
            }

            if (!type.IsInstanceOfType(obj))
            {
                throw new ArgumentException($"The provided object is not of type {type} and cannot be serialized");
            }

            if (serializationSettings.SerializerResolver == null)
            {
                throw new InvalidOperationException($"Cannot resolve the serializer for type {type}: the resolver is not set");
            }

            var serializer = serializationSettings.SerializerResolver.ResolveForType(type);

            // TODO: if serializer not found, try to construct it from the type ...
            if (serializer == null)
            {
                throw new InvalidOperationException($"No serializer was resolved for the type {type} from the available ones");
            }

            if (!serializer.CanSerialize(obj))
            {
                throw new InvalidOperationException($"The serializer for the type {type} cannot serialize the value provided");
            }

            return(serializer.SerializeAsync(obj, writer, serializationSettings, cancellationToken));
        }
 /// <inheritdoc />
 public Task SerializeAsync(object obj, SmppWriter writer, SmppSerializationSettings serializationSettings, CancellationToken cancellationToken)
 {
     return(_schema.WriteAsync(obj, writer, serializationSettings, cancellationToken));
 }
 /// <inheritdoc />
 public Task <object> DeserializeAsync(SmppReader reader, SmppSerializationSettings serializationSettings, CancellationToken cancellationToken)
 {
     return(_schema.ReadAsync(reader, serializationSettings, cancellationToken));
 }
 internal DefaultSerializerResolver(SmppSerializationSettings serializationSettings)
 {
     _serializationSettings = serializationSettings;
 }
示例#9
0
 public Task SerializeAsync(object obj, SmppWriter writer, SmppSerializationSettings serializationSettings)
 {
     return(SerializeAsync(obj, writer, serializationSettings, CancellationToken.None));
 }
示例#10
0
 public Task <T> DeserializeAsync <T>(SmppReader reader, SmppSerializationSettings serializationSettings)
 {
     return(DeserializeAsync <T>(reader, serializationSettings, CancellationToken.None));
 }