Home > Mobile >  How to Save Large Json Data?
How to Save Large Json Data?

Time:01-04

i have a large ObservableCollection that I want to get out as Json file. I used the following code, But I get an error out of memory

string json = JsonConvert.SerializeObject(content, Formatting.Indented);
await File.WriteAllTextAsync("file.json");

How can I save this huge ObservableCollection in a json file?

CodePudding user response:

Instead of serializing to a string, and then writing the string to a stream, stream it directly:

using var stream = File.Create("file.json");
JsonSerializer.Serialize(stream, content, new JsonSerializerOptions
{
    WriteIdented = true 
});

CodePudding user response:

try to serialize directly to the file

using (StreamWriter file = File.CreateText(@"c:\file.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        serializer.Serialize(file, content);
    }
  •  Tags:  
  • Related