Ejemplo n.º 1
0
        internal IPsbValue ConvertToken(JToken token, Dictionary <string, PsbString> context)
        {
            switch (token.Type)
            {
            case JTokenType.Null:
                return(PsbNull.Null);

            case JTokenType.Integer:
                var l = token.Value <long>();
                if (l > Int32.MaxValue || l < Int32.MinValue)
                {
                    return(new PsbNumber(l));
                }
                return(new PsbNumber(token.Value <int>()));

            case JTokenType.Float:
                //var numberStr = token.Value<string>();
                var d = token.Value <double>();
                if (UseDoubleOnly)
                {
                    return(new PsbNumber(d));
                }
                var f = token.Value <float>();
                if (Math.Abs(f - d) < 1E-08)     //float //pcc: 1E-05
                //if (Math.Abs(f - d) < float.Epsilon)
                {
                    return(new PsbNumber(f));
                }
                //if (d < float.MaxValue && d > float.MinValue)
                //{
                //    return new PsbNumber(token.Value<float>());
                //}
                return(new PsbNumber(d));

            case JTokenType.Boolean:
                return(new PsbBool(token.Value <bool>()));

            case JTokenType.String:
                string str = token.Value <string>();
                if (str.StartsWith(PsbResCollector.NumberStringPrefix))
                {
                    var prefixLen = PsbResCollector.NumberStringPrefix.Length;
                    if (str.EndsWith("f"))
                    {
                        return(new PsbNumber(int.Parse(str.Substring(prefixLen, 8), NumberStyles.AllowHexSpecifier))
                        {
                            NumberType = PsbNumberType.Float
                        });
                    }
                    if (str.EndsWith("d"))
                    {
                        return(new PsbNumber(long.Parse(str.Substring(prefixLen, 16), NumberStyles.AllowHexSpecifier))
                        {
                            NumberType = PsbNumberType.Double
                        });
                    }
                    return(new PsbNumber(long.Parse(str.Substring(prefixLen), NumberStyles.AllowHexSpecifier)));
                }
                if (str.StartsWith(PsbResCollector.ResourceIdentifier))
                {
                    return(new PsbResource(uint.Parse(str.Replace(PsbResCollector.ResourceIdentifier, ""))));
                }
                var psbStr = new PsbString(str, (uint)context.Count);
                if (context.ContainsKey(str))
                {
                    return(context[str]);
                }
                else
                {
                    context.Add(str, psbStr);
                }
                return(psbStr);

            case JTokenType.Array:
                var array      = (JArray)token;
                var collection = new PsbCollection(array.Count);
                foreach (var val in array)
                {
                    var o = ConvertToken(val, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = collection;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(collection);
                    }
                    collection.Add(o);
                }
                return(collection);

            case JTokenType.Object:
                var obj        = (JObject)token;
                var dictionary = new PsbDictionary(obj.Count);
                foreach (var val in obj)
                {
                    var o = ConvertToken(val.Value, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = dictionary;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(dictionary);
                    }
                    dictionary.Add(val.Key, o);
                }
                return(dictionary);

            default:
                throw new FormatException("Unsupported json element");
            }
        }
Ejemplo n.º 2
0
        internal IPsbValue ConvertToken(JToken token, List <PsbString> context)
        {
            switch (token.Type)
            {
            case JTokenType.Null:
                return(PsbNull.Null);

            case JTokenType.Integer:
                return(new PsbNumber(token.Value <int>()));

            case JTokenType.Float:
                var d = token.Value <double>();
                var f = token.Value <float>();
                if (Math.Abs(f - d) < 1E-05)     //float //pcc: 1E-05
                {
                    return(new PsbNumber(token.Value <float>()));
                }
                //if (d < float.MaxValue && d > float.MinValue)
                //{
                //    return new PsbNumber(token.Value<float>());
                //}
                return(new PsbNumber(d));

            case JTokenType.Boolean:
                return(new PsbBool(token.Value <bool>()));

            case JTokenType.String:
                string str = token.Value <string>();
                if (str.StartsWith(PsbResCollector.ResourceIdentifier))
                {
                    return(new PsbResource(uint.Parse(str.Replace(PsbResCollector.ResourceIdentifier, ""))));
                }
                var psbStr = new PsbString(str, (uint)context.Count);
                if (context.Contains(psbStr))
                {
                    return(context.Find(ps => ps.Value == str));
                }
                else
                {
                    context.Add(psbStr);
                }
                return(psbStr);

            case JTokenType.Array:
                var array      = (JArray)token;
                var collection = new PsbCollection(array.Count);
                foreach (var val in array)
                {
                    var o = ConvertToken(val, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = collection;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(collection);
                    }
                    collection.Add(o);
                }
                return(collection);

            case JTokenType.Object:
                var obj        = (JObject)token;
                var dictionary = new PsbDictionary(obj.Count);
                foreach (var val in obj)
                {
                    var o = ConvertToken(val.Value, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = dictionary;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(dictionary);
                    }
                    dictionary.Add(val.Key, o);
                }
                return(dictionary);

            default:
                throw new FormatException("Unsupported json element");
            }
        }