Example #1
0
        public static T ReadObject <T>(this string json)
        {
            if (json == null)
            {
                return(default(T));
            }

            var val = Script.Evaluate(json);

            return(Valizer.Devalize <T>(val));
        }
Example #2
0
        public static VAL functions(string func, VAL parameters, Memory DS)
        {
            if (func != "devalize" && parameters.Size != 2)
            {
                return(null);
            }

            var L0 = parameters[0];
            var L1 = parameters[1];

            Valizer.Devalize(L0, L1.Value);
            return(L1);
        }
Example #3
0
        public bool TryGetValue <T>(string variable, VAL val, out T result)
        {
            Type type = typeof(T);

            if (typeof(T) == typeof(VAL))
            {
                result = (T)(object)val;
                return(true);
            }
            else if (val.Undefined || val.IsNull)
            {
                result = default(T);
                return(false);
            }
            else if (typeof(T) == typeof(Guid) && val.Value is string)
            {
                string s = (string)val.Value;
                if (Guid.TryParse(s, out Guid uid))
                {
                    result = (T)(object)uid;
                    return(true);
                }
            }
            else if (val.HostValue is T)
            {
                result = (T)val.HostValue;
                return(true);
            }
            else if (typeof(T).IsEnum && val.HostValue is int)
            {
                result = (T)val.HostValue;
                return(true);
            }

            try
            {
                object obj = Valizer.Devalize(val, type);
                result = (T)obj;
                return(true);
            }
            catch (Exception ex)
            {
                clog.WriteLine($"cannot cast key={variable} value {val} to type {typeof(T).FullName}: {ex.Message}");
                result = default(T);
                return(true);
            }
        }