private JSDType ObjSpec(JToken obj, string propName, JSONParseOptions opts) { if (obj is JObject) { TypeDef current = new TypeDef(); JObject jobj = (JObject)obj; if (opts.DetectFakeArrays) { JArray tryArr = FakeArrayConvert(jobj); if (tryArr != null) { return(ArrSpec(tryArr, propName, opts)); } } var props = jobj.Properties(); foreach (var prop in props) { JSDType jsdt = ObjSpec(prop.Value, prop.Name, opts); current.Properties.Add(prop.Name, jsdt); } return(SquashType(current, propName, opts).JSDType); } else if (obj is JArray) { return(ArrSpec((JArray)obj, propName, opts)); } else if (obj is JValue) { return(JSDType.TranslatePrimitive(((JValue)obj).Value?.GetType())); } throw new Exception("obj must be either a JObject, JArray, or JValue"); }
public bool TrySquash(PropertyList otherpl, int maxMultiType = int.MaxValue) { var big = (otherpl.Count > Count ? otherpl : this).ToArray(); var small = (otherpl.Count > Count ? this : otherpl).ToArray(); bool success = true; foreach (var smallmatch in small) { Property bigmatch = big.FirstOrDefault(x => smallmatch.Name == x.Name); if (bigmatch == null) { success = false; break; } if (!bigmatch.Type.Equals(smallmatch.Type)) { JSDType bmt = bigmatch.Type, smt = smallmatch.Type; if (JSDType.TryMerge(bmt, smt, out JSDType merged) && merged.Types.Length < maxMultiType) { bigmatch.Type = merged; } else { success = false; break; } } } if (success) { Clear(); AddRange(big); } return(success); }
public static JSDScope GenerateFrom(JToken obj, JSONParseOptions opts = null) { JSDScope scope = new JSDScope(); JSONParseOptions optsval = opts ?? new JSONParseOptions(); JSDType rootType = scope.ObjSpec(obj, null, optsval); TypeDef rootTypeDef = scope.TypeDefinitions.First(x => x.JSDType.Equals(rootType)); scope.TypeDefinitions.Remove(rootTypeDef); scope.TypeDefinitions.Add(rootTypeDef); scope.TypeDefinitions.Reverse();// move root typedef to the front of list return(scope); }
public void Add(string Name, JSDType Type, string Description = "") { Add(new Property(Name, Type, Description)); }
public Property(string Name, JSDType Type, string Description = "") { this.Name = Name; this.Type = Type; this.Description = Description; }