/// <summary> /// Create a new instance of <see cref="DataPackage"/> with headerSpaceSize from list of columns definitions and list of values /// </summary> /// <param name="headerSpaceSize">Header size (default 512 bytes)</param> /// <param name="columns">list of columns definitions strings in format ColimnName[.ColumnType][.ColumnWidth]</param> /// <param name="values">list of values</param> public DataPackage(int headerSpaceSize, string[] columns, object[] values) : base(null) { _headerSpaceSize = headerSpaceSize <= 0 ? header_space_size_default : headerSpaceSize; _b = new BinaryDataBuffer(_headerSpaceSize * 2); _bw = new BinaryDataWriter(_b); _br = new BinaryDataReader(_b); _colCount = columns.Length; bool hasValues = values != null && values.Length == _colCount; _headers = new MapTable <string, object>(StringComparer.Ordinal); //Create col info _indexes = new string[_colCount]; _colInfo = new ColumnInfo[_colCount]; for (int i = 0; i < _colCount; i++) { ColumnInfo info; string name; if (columns[i].IndexOf('.') > 0) { info = ColumnInfo.FromName(columns[i]); name = columns[i].GetToken(0, "."); } else if (hasValues) { name = columns[i]; info = ColumnInfo.FromValue(values[i]); } else { name = columns[i]; info = ColumnInfo.FromType(typeof(string)); } _indexes[i] = name; _colInfo[i] = info; } WritePackageHeader(); if (hasValues) { AddNew(); SetValues(values); Update(); } else if (values != null) { throw new ArgumentException("The length of the data array must be equal to the length of the array of field names"); } }
/// <summary> /// Create <see cref="DataPackage"/> from full json format /// </summary> /// <param name="source">json string whit Headers, Columns and Data rows </param> /// <param name="jsonFormat"><see cref="TsJsonFormat"/></param> /// <param name="headerSpaceSize">For no headers package set headerSpaceSize = 8</param> internal DataPackage(JsonValue source, TsJsonFormat jsonFormat = TsJsonFormat.Full, int headerSpaceSize = 0) : base(source) { if (jsonFormat != TsJsonFormat.Simple) { var j = source as JsonObject; if (headerSpaceSize == 0) { headerSpaceSize = (int)j.GetIntOrDefault("HeaderSize", header_space_size_default); } JsonArray columns = j.GetArray("Columns"); JsonArray rows = j.GetArray("Rows"); _b = new BinaryDataBuffer(headerSpaceSize * 2); _bw = new BinaryDataWriter(_b); _br = new BinaryDataReader(_b); _colCount = columns.Count; bool hasValues = rows != null; //headers _headerSpaceSize = headerSpaceSize; _headers = new MapTable <string, object>(StringComparer.Ordinal); JsonObject headers = (JsonObject)j["Headers"]; foreach (var pair in headers) { _headers.Add(pair.Key, pair.Value?.GetValue()); } //Create col info _indexes = new string[_colCount]; _colInfo = new ColumnInfo[_colCount]; for (int i = 0; i < _colCount; i++) { var c = columns[i]; _indexes[i] = (string)c["Name"]; _colInfo[i] = new ColumnInfo() { DataType = MdbTypeMap.GetType((string)c["Type"], typeof(string)), ColumnSize = (int)c["Size"], AllowDBNull = (bool)c["AllowNull"] }; } WritePackageHeader(); //values if (hasValues) { if (jsonFormat == TsJsonFormat.Full) { foreach (JsonObject r in rows) { AddNew(); foreach (var o in r) { SetValue(o.Key, o.Value?.GetValue()); } Update(); } } else { //!!! Not tested foreach (JsonArray a in rows) { AddNew(); foreach (var o in a) { for (int i = 0; i < _colCount; i++) { SetValue(_indexes[i], o?.GetValue()); } } Update(); } } } } else //simple { if (headerSpaceSize == 0) { headerSpaceSize = header_space_size_default; } _b = new BinaryDataBuffer(headerSpaceSize * 2); _bw = new BinaryDataWriter(_b); _br = new BinaryDataReader(_b); JsonObject dataRow; bool isArray = false; if (source is JsonObject j) { dataRow = j; } else if (source is JsonArray a) { if (a.Count == 0) { throw new ArgumentException("Parameter array cannot be empty"); } dataRow = (JsonObject)a[0]; isArray = true; } else { throw new ArgumentException("The constructor of DataPackage requires JsonObject or JsonArray parameter type."); } _headerSpaceSize = headerSpaceSize; _headers = new MapTable <string, object>(StringComparer.Ordinal); _colCount = dataRow.Count; _indexes = new string[_colCount]; _colInfo = new ColumnInfo[_colCount]; int i = 0; foreach (var kvp in dataRow) { _indexes[i] = kvp.Key; _colInfo[i] = ColumnInfo.FromValue(kvp.Value?.GetValue()); i++; } WritePackageHeader(); if (isArray) { foreach (var r in (source as JsonArray)) { AddNew(); foreach (var o in (JsonObject)r) { SetValue(o.Key, o.Value?.GetValue()); } Update(); } } else { AddNew(); foreach (var o in dataRow) { SetValue(o.Key, o.Value?.GetValue()); } Update(); } } GoDataTop(); }