Exemplo n.º 1
0
        public void Setup()
        {
            _options = Options.Create();
            _source = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _target = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _readers = new ReaderConventions(_options);

            _visitorIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrementValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return s.Value == null ? 1 : (int)s.Value + 1;
            };

            _readerIncrementNullableValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return s.Value == null ? 1 : (int?)s.Value + 1;
            };
        }
Exemplo n.º 2
0
        public void Setup()
        {
            _options = Options.Create();
            _source  = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _target = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _readers = new ReaderConventions(_options);

            _visitorIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrementValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return((int?)s.Value + 1 ?? 1);
            };

            _readerIncrementNullableValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return(s.Value == null ? 1 : (int?)s.Value + 1);
            };
        }
Exemplo n.º 3
0
        public DeserializationOptions(Options options)
        {
            NameComparison      = StringComparison.Ordinal;
            EnumValueComparison = StringComparison.OrdinalIgnoreCase;
            IgnoreXmlAttributes = false;

            FriendlyParseErrorMessages = new Dictionary <Type, string>
            {
                [typeof(Enum)]     = "Option '{0}' is not valid.",
                [typeof(char)]     = "Char '{0}' not valid, must be exactly one character.",
                [typeof(bool)]     = "Boolean '{0}' not formatted correctly, must be 'true' or 'false'.",
                [typeof(sbyte)]    = "Byte '{0}' not formatted correctly, must be an integer between -128 and 127.",
                [typeof(byte)]     = "Unsigned byte '{0}' not formatted correctly, must be an integer between 0 and 255.",
                [typeof(short)]    = "Integer '{0}' not formatted correctly, must be an integer between -32,768 and 32,767.",
                [typeof(ushort)]   = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 65,535.",
                [typeof(int)]      = "Integer '{0}' not formatted correctly, must be an integer between -2,147,483,648 and 2,147,483,647.",
                [typeof(uint)]     = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 4,294,967,295.",
                [typeof(long)]     = "Integer '{0}' not formatted correctly, must be an integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.",
                [typeof(ulong)]    = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 18,446,744,073,709,551,615.",
                [typeof(float)]    = "32 bit float '{0}' not formatted correctly, must be a single-precision 32 bit float between -3.402823e38 and 3.402823e38.",
                [typeof(double)]   = "64 bit float '{0}' not formatted correctly, must be a double-precision 64-bit float between -1.79769313486232e308 and 1.79769313486232e308.",
                [typeof(decimal)]  = "Decimal '{0}' not formatted correctly, must be a decimal number between -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335.",
                [typeof(DateTime)] = "Date '{0}' not formatted correctly, must be formatted as m/d/yyy h:m:s AM.",
                [typeof(Guid)]     = "UUID '{0}' not formatted correctly, should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).",
                [typeof(TimeSpan)] = "Timespan '{0}' not formatted correctly, must be formatted as 'd.h:m:s'.",
                [typeof(Uri)]      = "Uri '{0}' not formatted correctly, must be formatted as 'scheme://host'.",

                [typeof(Version)]     = "Version '{0}' not formatted correctly, must be formatted as '1.2.3.4'.",
                [typeof(MailAddress)] = "Email address '{0}' not formatted correctly, must be formatted as '*****@*****.**'.",
                [typeof(IPAddress)]   = "IP address '{0}' not formatted correctly, must be formatted as '1.2.3.4'."
            };

            FriendlyParseErrorMessages[typeof(IntPtr)]  = FriendlyParseErrorMessages[typeof(int)];
            FriendlyParseErrorMessages[typeof(UIntPtr)] = FriendlyParseErrorMessages[typeof(ushort)];

            Readers = new ReaderConventions(options);

            IgnoreUnmatchedElements  = true;
            IgnoreUnmatchedMembers   = true;
            IgnoreNullsForValueTypes = false;
            IgnoreEmptyCsvValues     = false;

            Readers.AddValueReader((v, s, t, o) => Version.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new MailAddress(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => IPAddress.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => Convert.FromBase64String(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new SqlConnectionStringBuilder(v.ToString()));
        }
Exemplo n.º 4
0
        public DeserializationOptions(Options options)
        {
            NameComparison = StringComparison.Ordinal;
            EnumValueComparison = StringComparison.OrdinalIgnoreCase;
            IgnoreXmlAttributes = false;

            FriendlyParseErrorMessages = new Dictionary<Type, string>
            {
                [typeof(Enum)] = "Option '{0}' is not valid.",
                [typeof(char)] = "Char '{0}' not valid, must be exactly one character.",
                [typeof(bool)] = "Boolean '{0}' not formatted correctly, must be 'true' or 'false'.",
                [typeof(sbyte)] = "Byte '{0}' not formatted correctly, must be an integer between -128 and 127.",
                [typeof(byte)] = "Unsigned byte '{0}' not formatted correctly, must be an integer between 0 and 255.",
                [typeof(short)] = "Integer '{0}' not formatted correctly, must be an integer between -32,768 and 32,767.",
                [typeof(ushort)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 65,535.",
                [typeof(int)] = "Integer '{0}' not formatted correctly, must be an integer between -2,147,483,648 and 2,147,483,647.",
                [typeof(uint)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 4,294,967,295.",
                [typeof(long)] = "Integer '{0}' not formatted correctly, must be an integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.",
                [typeof(ulong)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 18,446,744,073,709,551,615.",
                [typeof(float)] = "32 bit float '{0}' not formatted correctly, must be a single-precision 32 bit float between -3.402823e38 and 3.402823e38.",
                [typeof(double)] = "64 bit float '{0}' not formatted correctly, must be a double-precision 64-bit float between -1.79769313486232e308 and 1.79769313486232e308.",
                [typeof(decimal)] = "Decimal '{0}' not formatted correctly, must be a decimal number between -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335.",
                [typeof(DateTime)] = "Date '{0}' not formatted correctly, must be formatted as m/d/yyy h:m:s AM.",
                [typeof(Guid)] = "UUID '{0}' not formatted correctly, should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).",
                [typeof(TimeSpan)] = "Timespan '{0}' not formatted correctly, must be formatted as 'd.h:m:s'.",
                [typeof(Uri)] = "Uri '{0}' not formatted correctly, must be formatted as 'scheme://host'.",

                [typeof(Version)] = "Version '{0}' not formatted correctly, must be formatted as '1.2.3.4'.",
                [typeof(MailAddress)] = "Email address '{0}' not formatted correctly, must be formatted as '*****@*****.**'.",
                [typeof(IPAddress)] = "IP address '{0}' not formatted correctly, must be formatted as '1.2.3.4'."
            };

            FriendlyParseErrorMessages[typeof(IntPtr)] = FriendlyParseErrorMessages[typeof(int)];
            FriendlyParseErrorMessages[typeof(UIntPtr)] = FriendlyParseErrorMessages[typeof(ushort)];

            Readers = new ReaderConventions(options);

            IgnoreUnmatchedElements = true;
            IgnoreUnmatchedMembers = true;

            Readers.AddValueReader((v, s, t, o) => Version.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new MailAddress(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => IPAddress.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => Convert.FromBase64String(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new SqlConnectionStringBuilder(v.ToString()));
        }