Example #1
0
        private void WriteEscape(string str)
        {
            _textWriter.Write('"');
            var beginIndex = 0;

            for (var i = 0; i < str.Length; i++)
            {
                var c = str[i];
                if (c != '"' && c != '\\' && c >= 0x20)
                {
                    continue;
                }

                var length = i - beginIndex;
                if (length > 0)
                {
                    _textWriter.Write(str.AsSpan(beginIndex, length));
                }

                _textWriter.Write(SsslObject.EscapeChar(c));
                beginIndex = i + 1;
            }

            {
                var length = str.Length - beginIndex;
                if (length > 0)
                {
                    _textWriter.Write(str.AsSpan(beginIndex, length));
                }
            }
            _textWriter.Write('"');
        }
Example #2
0
        public static bool TryConvertTo(ISsslConverter converter, SsslObject sssl, Type type, ObjectConversionOptions options, [NotNullWhen(true)] out object result)
        {
            var allowUnknownMember = options.HasFlag(ObjectConversionOptions.AllowUnknownMember);
            var allowMissingMember = options.HasFlag(ObjectConversionOptions.AllowMissingMember);

            result = null !;

            if (!(sssl is SsslRecord record))
            {
                return(false);
            }

            if (record.Name != "" && !type.IsSealed && record.Name != type.GetFullName())
            {
                if (type.Assembly.GetType(record.Name) is { } assemblyInnerType&&
                    type.IsAssignableFrom(assemblyInnerType))
                {
                    type = assemblyInnerType;
                }
                if (Type.GetType(record.Name) is { } coreType&&
                    type.IsAssignableFrom(coreType))
                {
                    type = coreType;
                }
                else if (AppDomain.CurrentDomain
                         .GetAssemblies()
                         .Select(x => x.GetType(record.Name))
                         .FirstOrDefault(x => x is { } && type.IsAssignableFrom(x)) is { } otherType&&
Example #3
0
 public bool TryConvertFrom(object?value, [NotNullWhen(true)] out SsslObject result)
 {
     return(value switch
     {
         null => Success(SsslValue.Null, out result),
         SsslObject ssslObject => Success(ssslObject, out result),
         _ => TryConvertFromCore(value, out result),
     });
Example #4
0
        public static T ConvertTo <T>(this ISsslConverter converter, SsslObject ssslObject)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            return((T)converter.ConvertTo(ssslObject, typeof(T)) !);
        }
Example #5
0
        public static object?ConvertTo(this ISsslConverter converter, SsslObject ssslObject, Type type)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            return(converter.TryConvertTo(ssslObject, type, out var result)
                ? result
                : throw new InvalidCastException());
        }
Example #6
0
        public static bool TryConvertTo <T>(this ISsslConverter converter, SsslObject ssslObject, [MaybeNull] out T result)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            if (converter.TryConvertTo(ssslObject, typeof(T), out var obj))
            {
                result = (T)obj !;
                return(true);
            }

            result = default !;
Example #7
0
        public static bool TryConvertFrom(ISsslConverter converter, object value, [NotNullWhen(true)] out SsslObject result)
        {
            var type   = value.GetType();
            var obj    = new SsslRecord(SsslRecordType.Braces, type.GetFullName());
            var values = GetModelInfo(type).ConvertFrom(value);

            foreach (var(name, val) in values)
            {
                if (!converter.TryConvertFrom(val, out var ssslVal))
                {
                    result = null !;
                    return(false);
                }

                obj[name] = ssslVal;
            }

            result = obj;
            return(true);
        }
Example #8
0
        private bool TryConvertToGenerated(SsslObject ssslObject, Type type, out object?result)
        {
            if (type == typeof(double))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(bool))
            {
                var canConvert = TryConvertTo(ssslObject, out bool val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(string))
            {
                var canConvert = TryConvertTo(ssslObject, out string val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(char))
            {
                var canConvert = TryConvertTo(ssslObject, out char val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(DateTime))
            {
                var canConvert = TryConvertTo(ssslObject, out DateTime val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(DateTimeOffset))
            {
                var canConvert = TryConvertTo(ssslObject, out DateTimeOffset val);
                result = canConvert ? (object)val : null !;
                return(canConvert);
            }

            if (type == typeof(byte))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(byte)val : null !;
                return(canConvert);
            }

            if (type == typeof(sbyte))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(sbyte)val : null !;
                return(canConvert);
            }

            if (type == typeof(short))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(short)val : null !;
                return(canConvert);
            }

            if (type == typeof(ushort))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(ushort)val : null !;
                return(canConvert);
            }

            if (type == typeof(int))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(int)val : null !;
                return(canConvert);
            }

            if (type == typeof(uint))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(uint)val : null !;
                return(canConvert);
            }

            if (type == typeof(long))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(long)val : null !;
                return(canConvert);
            }

            if (type == typeof(ulong))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(ulong)val : null !;
                return(canConvert);
            }

            if (type == typeof(float))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(float)val : null !;
                return(canConvert);
            }

            if (type == typeof(decimal))
            {
                var canConvert = TryConvertTo(ssslObject, out double val);
                result = canConvert ? (object)(decimal)val : null !;
                return(canConvert);
            }

            result = null;
            return(false);
        }
Example #9
0
        private bool TryConvertFromGenerated(object value, [NotNullWhen(true)] out SsslObject result)
        {
            switch (value)
            {
            case double v:
                result = ConvertFrom(v);
                return(true);

            case bool v:
                result = ConvertFrom(v);
                return(true);

            case string v:
                result = ConvertFrom(v);
                return(true);

            case char v:
                result = ConvertFrom(v);
                return(true);

            case DateTime v:
                result = ConvertFrom(v);
                return(true);

            case DateTimeOffset v:
                result = ConvertFrom(v);
                return(true);

            case byte v:
                result = ConvertFrom((double)v);
                return(true);

            case sbyte v:
                result = ConvertFrom((double)v);
                return(true);

            case short v:
                result = ConvertFrom((double)v);
                return(true);

            case ushort v:
                result = ConvertFrom((double)v);
                return(true);

            case int v:
                result = ConvertFrom((double)v);
                return(true);

            case uint v:
                result = ConvertFrom((double)v);
                return(true);

            case long v:
                result = ConvertFrom((double)v);
                return(true);

            case ulong v:
                result = ConvertFrom((double)v);
                return(true);

            case float v:
                result = ConvertFrom((double)v);
                return(true);

            case decimal v:
                result = ConvertFrom((double)v);
                return(true);

            default:
                result = null !;
                return(false);
            }
        }
Example #10
0
 internal SsslDynamicProvider(SsslObject ssslObject, ISsslConverter converter)
 {
     _ssslObject = ssslObject;
     _converter  = converter;
 }
Example #11
0
 internal SsslDynamicProvider(SsslObject ssslObject)
 {
     _ssslObject = ssslObject;
     _converter  = SsslConverter.Default;
 }