// например ColumnInfo FromName("Field1.int.4.0") public static ColumnInfo FromName(string columnInfo) { string[] cInfo = (columnInfo + "....").Split('.'); string typeName = cInfo[1].IsEmpty() ? "string" : cInfo[1].ToLower(); Type t = MdbTypeMap.GetType(typeName, typeof(string)); return(new ColumnInfo() { DataType = t, ColumnSize = cInfo[2].IsEmpty() ? MdbTypeMap.GetTypeInfo(t).Size : cInfo[2].ToIntOrDefault(), AllowDBNull = cInfo[3].ToBoolOrDefault() }); }
ColumnInfo ReadColumnInfo(BinaryDataReader br) { try { ColumnInfo ci = new ColumnInfo() { DataType = MdbTypeMap.GetType((MdbType)br.Read <byte>()), ColumnSize = br.Read <int>(), AllowDBNull = br.Read <bool>() }; return(ci); } catch (Exception e) { throw new DataPackageMessageFormatException(e.Message); } }
/// <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(); }