I have been trying to figure out the best way to store and retrieve data from a file containing Json using JSon.net. During my research i found couple of ways.
- http://www.drdobbs.com/windows/parsing-big-records-with-jsonnet/240165316
- http://www.ngdata.com/parsing-a-large-json-file-efficiently-and-easily/ Jackson Api ( only available for Java )
Can any of this method be used with Json.Net? Is there an actual implementation available for the article at DrDoobs? I could not figure out the HandleToken(reader.TokenType, reader.Value) method.
Current Stats:
Around 5 min to write the Json of size ~ 60Mb
Around 3 min to read the Json.
Current Code:
public static T DeserializeJsonFromStream<T>(Stream s)
{
using (StreamReader reader = new StreamReader(s))
{
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer ser = new JsonSerializer();
ser.Formatting = Newtonsoft.Json.Formatting.None;
ser.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
ser.TypeNameHandling = TypeNameHandling.All;
ser.NullValueHandling = NullValueHandling.Ignore;
ser.Error += ReportJsonErrors;
ser.DateFormatHandling = DateFormatHandling.IsoDateFormat;
return ser.Deserialize<T>(jsonReader);
}
}
}
public static void SerializeJsonIntoStream(object value, Stream s)
{
using (StreamWriter writer = new StreamWriter(s))
{
using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer ser = new JsonSerializer();
ser.Formatting = Newtonsoft.Json.Formatting.None;
ser.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
ser.TypeNameHandling = TypeNameHandling.All;
ser.NullValueHandling = NullValueHandling.Ignore;
ser.Error += ReportJsonErrors;
ser.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
}
}