Search code examples
c#jsonrutf-8javascriptserializer

serializing C# objects with JavaScriptSerializer produces invalid bytes in UTF8 string


I am serializing a C# object with the following code:

            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(rawFile);
            Console.WriteLine(serializedResult);
            return;

Tried serialization with Target Frameworks from 4.5 up to 4.6.1.

Then I am trying deserialize it into R:

     > x <-  read_json("C:\\dump.txt")
     Error in parse_con(txt, bigint_as_char) : 
     lexical error: invalid bytes in UTF8 string.
             ": "Sample pickup:\nVolume [æl]         :     1.00\nFlow [æl
                        (right here) ------^

I am guessing that these are a micro liter. How to best handle these special characters?

Thank you.


Solution

  • The crucial hint was from Sergey "If you output it to console, well... it can be broken."

    Using File.WriteAllText solves the problem:

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string serializedResult = serializer.Serialize(rawFile);
                // byte[] bytes = Encoding.Default.GetBytes(serializedResult);
                // string myString = Encoding.UTF8.GetString(bytes);
                File.WriteAllText("dump2.txt", serializedResult);