2014-11-15 2 views
0

일부 텍스트가 포함 된 검색 트리를 구현했습니다. 자, 클라이언트가이 트리를 디스크에 저장하고 serilize 할 수있게하려고합니다. 그래서 JSON.Net을 사용하여이 객체를 디스크에 저장합니다. 거대한 텍스트 파일 (50MB 이상)을 다루기 때문에이 객체를 DISK (이 JSON이 포함 된 문자열을 가져 오는 대신)에 직접 저장합니다.JSON.NET : 유니 코드 문자를 번역 할 수 없습니다.

이 내 현재 코드입니다 :

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); 
    } 
} 

이 내가 가지고있어 예외 :

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() 
    ... 

파일 테스트를 통과하지 파일이 있는지 확인하지 chanise 문자를 (포함 이게 문제 야).

어떻게 해결할 수 있습니까?

+1

잘 U + D859는 고유 한 유효한 유니 코드 문자가 아니며 서로 게이트 쌍의 일부입니다. 내 생각 엔 데이터의 일부가 유효하지 않다는 것입니다. 문제가있는 곳에서 해결해야합니다. –

답변

1

JSON 문제가 아니라 StreamWriter 문제이므로 인코딩 할 수없는 문자가 발생합니다. 다음과 같이 열어보십시오.

using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode)) 
관련 문제