/// <summary> /// Parse loopstr like "range:0,123456" to a <see cref="PsbList"/> /// </summary> /// <param name="loopStr"></param> /// <returns></returns> public static PsbList ParseLoopStr(string loopStr) { if (string.IsNullOrWhiteSpace(loopStr) || loopStr == "none") { return(null); } if (loopStr.StartsWith("range:")) { loopStr = loopStr.Substring(6); var loopPoints = loopStr.Split(','); PsbList list = new PsbList(); foreach (var loopPoint in loopPoints) { if (int.TryParse(loopPoint, out var loopInt)) { list.Add(new PsbNumber(loopInt)); } else if (double.TryParse(loopPoint, out var loopNum)) { list.Add(new PsbNumber(loopNum)); } else { return(null); } } return(list); } return(null); }
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(Consts.NumberStringPrefix)) { var prefixLen = Consts.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(Consts.ResourceIdentifier)) { return(new PsbResource(uint.Parse(str.Replace(Consts.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 PsbList(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"); } }