I geting data from API in JSON and storing it localy in file using JSON.NET but there is up to 2,5 million records. It works like this :
- get small amount of data from API and add it to List object
- save in new file (if file not exists) or OVERRIDE it (if file exists)
My problem is that List of objects must store large amount od data (millions)
My save looks like this
public bool SaveJSON(string path, TData data,Formatting formatting = Formatting.Indented)
{
bool success = false;
while(!success)
{
try
{
using (StreamWriter file = File.CreateText(path))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, data);
success = true;
}
}
catch(Exception ex)
{
Console.WriteLine("Error : " ex.Message);
}
}
return true;
}
With this JSON file looks like
[
{
...
},
...
{
...
}
]
I know there is File method File.AppendText but with it i get file like this
[
{
...
},
...
{
...
}
]
[
{
...
}
]
so non-separated arrays and it can't be deserialized correctly.
How can I deal with this? I thought about removing last line
]
but all solutions I found were about reading whole file to the string and then making changes in it.