Reputation: 6903
I implemented a search tree that contain some texts. Now, I'm want to allow the client to save this tree to disk - serilize. So, I'm using JSON.Net to serilize this object to disk. Since I'm handlig huge text files (50MB+), I'm serilize this object directly to DISK (instead of get string that contain this JSON).
This is my current code:
public void Save(string path)
{
using (FileStream fs = File.Open(path, FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, this);
}
}
This is the exception I'm got:
exception:
System.Text.EncoderFallbackException: Unable to translate Unicode character \uD859 at index 485 to specified code page.
Result StackTrace:
at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
at System.Text.UTF8Encoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount, EncoderNLS baseEncoder)
at System.Text.EncoderNLS.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount, Boolean flush)
at System.Text.EncoderNLS.GetBytes(Char[] chars, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex, Boolean flush)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TextWriter.Dispose()
...
The file that not pass the test is file that contains chanise characters (not sure that this is the problem).
How I'm can fix this?
Upvotes: 0
Views: 3078
Reputation: 11903
It's not a JSON problem, it's a StreamWriter problem, it encounters a character that it cannot encode. Try to open it like this:
using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode))
Upvotes: 3