0

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?

1
  • 1
    Well U+D859 isn't a valid Unicode character on its own - it's part of a surrogate pair. My guess is that some part of your data is invalid... you should work out where the problem is. Commented Nov 15, 2014 at 19:58

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.