【特性】json数据源支持从json子字段读入bean或者list,bean,支持用*@xxx.json形式将json当作一个记录列表读入list,bean形式的数据
parent
390a72747d
commit
9cc489ecfd
|
|
@ -12,23 +12,43 @@ namespace Luban.Job.Cfg.DataSources.Json
|
||||||
{
|
{
|
||||||
class JsonDataSource : AbstractDataSource
|
class JsonDataSource : AbstractDataSource
|
||||||
{
|
{
|
||||||
JsonElement _data;
|
private JsonElement _data;
|
||||||
|
|
||||||
public override void Load(string rawUrl, string sheetName, Stream stream)
|
public override void Load(string rawUrl, string sheetName, Stream stream)
|
||||||
{
|
{
|
||||||
RawUrl = rawUrl;
|
RawUrl = rawUrl;
|
||||||
this._data = JsonDocument.Parse(stream).RootElement;
|
this._data = JsonDocument.Parse(stream).RootElement;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(sheetName))
|
||||||
|
{
|
||||||
|
if (sheetName.StartsWith("*"))
|
||||||
|
{
|
||||||
|
sheetName = sheetName.Substring(1);
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrEmpty(sheetName))
|
||||||
|
{
|
||||||
|
foreach (var subField in sheetName.Split('.'))
|
||||||
|
{
|
||||||
|
_data = _data.GetProperty(subField);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override List<Record> ReadMulti(TBean type)
|
public override List<Record> ReadMulti(TBean type)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var records = new List<Record>();
|
||||||
|
foreach (var ele in _data.EnumerateArray())
|
||||||
|
{
|
||||||
|
records.Add(ReadBean(ele, type));
|
||||||
|
}
|
||||||
|
return records;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Record ReadOne(TBean type)
|
private Record ReadBean(JsonElement ele, TBean type)
|
||||||
{
|
{
|
||||||
List<string> tags;
|
List<string> tags;
|
||||||
if (_data.TryGetProperty(TAG_KEY, out var tagEle))
|
if (ele.TryGetProperty(TAG_KEY, out var tagEle))
|
||||||
{
|
{
|
||||||
var tagName = tagEle.GetString();
|
var tagName = tagEle.GetString();
|
||||||
if (DataUtil.IsIgnoreTag(tagName))
|
if (DataUtil.IsIgnoreTag(tagName))
|
||||||
|
|
@ -42,8 +62,13 @@ namespace Luban.Job.Cfg.DataSources.Json
|
||||||
tags = null;
|
tags = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = (DBean)type.Apply(JsonDataCreator.Ins, _data, (DefAssembly)type.Bean.AssemblyBase);
|
var data = (DBean)type.Apply(JsonDataCreator.Ins, ele, (DefAssembly)type.Bean.AssemblyBase);
|
||||||
return new Record(data, RawUrl, tags);
|
return new Record(data, RawUrl, tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override Record ReadOne(TBean type)
|
||||||
|
{
|
||||||
|
return ReadBean(_data, type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,16 @@ namespace Luban.Job.Cfg.Utils
|
||||||
return allFiles;
|
return allFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsMultiRecordField(string sheet)
|
||||||
|
{
|
||||||
|
return !string.IsNullOrEmpty(sheet) && sheet.StartsWith("*");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsMultiRecordFile(string file, string sheetOrFieldName)
|
||||||
|
{
|
||||||
|
return FileUtil.IsExcelFile(file) || IsMultiRecordField(sheetOrFieldName);
|
||||||
|
}
|
||||||
|
|
||||||
public static async Task GenerateLoadRecordFromFileTasksAsync(IAgent agent, DefTable table, string dataDir, List<string> inputFiles2, List<Task<List<Record>>> tasks)
|
public static async Task GenerateLoadRecordFromFileTasksAsync(IAgent agent, DefTable table, string dataDir, List<string> inputFiles2, List<Task<List<Record>>> tasks)
|
||||||
{
|
{
|
||||||
var inputFileInfos = await CollectInputFilesAsync(agent, inputFiles2, dataDir);
|
var inputFileInfos = await CollectInputFilesAsync(agent, inputFiles2, dataDir);
|
||||||
|
|
@ -86,7 +96,7 @@ namespace Luban.Job.Cfg.Utils
|
||||||
file.OriginFile,
|
file.OriginFile,
|
||||||
file.SheetName,
|
file.SheetName,
|
||||||
await agent.GetFromCacheOrReadAllBytesAsync(file.ActualFile, file.MD5),
|
await agent.GetFromCacheOrReadAllBytesAsync(file.ActualFile, file.MD5),
|
||||||
FileUtil.IsExcelFile(file.ActualFile));
|
IsMultiRecordFile(file.ActualFile, file.SheetName));
|
||||||
|
|
||||||
FileRecordCacheManager.Ins.AddCacheLoadedRecords(table, file.MD5, file.SheetName, res);
|
FileRecordCacheManager.Ins.AddCacheLoadedRecords(table, file.MD5, file.SheetName, res);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue