public ObjectProperty(JObject o) { properties = new List <C_DataType>(); foreach (var prop in o) { properties.Add(C_DataType_Helpers.Create(prop)); } }
public ListProperty(JArray arr) { list = new List <C_DataType>(); foreach (var item in arr) { list.Add(C_DataType_Helpers.Create(item)); } list_size = list.Count; }
/// <summary> /// Creates C_DataType based on object type /// </summary> public static C_DataType Create(object o) { if (o is JArray arr) { return(new ListProperty(arr)); } else if (o is JProperty prop) { return(null); } else if (o is JObject obj) { return(new ObjectProperty(obj)); } else if (o is KeyValuePair <string, JToken> objProp) { return(C_DataType_Helpers.Create(objProp.Value)); } else if (o is JValue val) { if (val.Value is String strVal) { List <int> codeList; if (strVal.Length == 1) { return(new CharProperty(strVal[0])); } else if (DataExtensions.IsCode(strVal, out codeList)) { return(new ListProperty(JArray.FromObject(codeList))); } else { return(new StringProperty(strVal)); } } else if (val.Value == null) { return(new IntProperty(0)); } else if (val.Value is Int32 int32Val) { return(new IntProperty((int)int32Val)); } else if (val.Value is Int64 int64Val) { return(new IntProperty((int)int64Val)); } else if (val.Value is Double doubleVal) { return(new FloatProperty((float)doubleVal)); } else if (val.Value is Boolean boolVal) { int boolValInt = boolVal ? 1 : 0; return(new IntProperty((int)boolValInt)); } else { return(null); } } return(null); }
static void Main(string[] args) { if (args.Length != 2) { throw new Exception("Invalid arguments."); } string input_file_path = args[0]; string output_file_path = args[1]; if (input_file_path == null) { throw new Exception("Missing input file path."); } if (output_file_path == null) { throw new Exception("Missing output file path."); } if (!File.Exists(input_file_path)) { throw new Exception("Unable to locate input file path."); } C_DataType c; JArray areas = JsonConvert.DeserializeObject <JArray>(File.ReadAllText(input_file_path)); c = C_DataType_Helpers.Create(areas); int size = c.total_size(); //estimate total required size size = GetAligned_u32(size); byte[] b = new byte[size]; c.allocate_struct(b, 0); int dynamic_memory_start = 0; for (int i = 0; i < b.Length; i++) { if (b[i] == 0) { dynamic_memory_start = i; break; } } c.fill_memory(b, ref dynamic_memory_start); //initial data size was an estimate. Reduce byte size if possible int last_byte_of_data = -1; for (int i = size - 1; i >= 0; i--) { if (b[i] != 0) { last_byte_of_data = i; break; } } last_byte_of_data += 32; //add 32 bytes of padding 0s for safety last_byte_of_data = GetAligned_u32(last_byte_of_data); last_byte_of_data = Math.Min(last_byte_of_data, size); Array.Resize(ref b, last_byte_of_data); File.WriteAllBytes(output_file_path, b); }