locked
How to know character encoding of Newtonsoft.Json? RRS feed

  • Question

  • Dear All,

    We serialize object to Json format by Newtonsoft Json and if we deserialize the file from other device, how do we know the character encoding? utf-8 or utf-16 or utf-32?

    For XML file, there is a field to describe the character encoding, like <?xml version="1.0" encoding="utf-8"?>

    Thanks and Best regards,

    E-John




    • Edited by E-John Thursday, June 20, 2019 6:21 AM
    Thursday, June 20, 2019 6:19 AM

Answers

  • Note that Newtonsoft.Json always serializes to a String. The string does not have any encoding, it is always Unicode internally in memory. You then feed that string to something else (maybe a FileStream, maybe a File.WriteAlltext, maybe some other code), and that is what applies the encoding. So, to know the encoding you need to look at what else you have in your code besides the Newtonsoft serializer. This additional code is the one that applies the encoding.
    • Marked as answer by E-John Friday, June 21, 2019 1:14 AM
    Thursday, June 20, 2019 6:36 AM
  • Dear All,

    I think I found it, if there is any wrong please correct me, thanks.

    It could be done by specifying the parameter "Encoding.UTF8" in the System.IO.File.WriteAllText method

                System.IO.File.WriteAllText(SaveFileName, json, Encoding.UTF8);

    and stored this string in one field of json file, something like "Encoding": "utf-8",


    Thus, other device can read this field and decode the string by correct way.



    Thanks and Best regards,

    E-John






    • Edited by E-John Thursday, June 20, 2019 7:41 AM
    • Marked as answer by E-John Thursday, June 20, 2019 8:06 AM
    Thursday, June 20, 2019 7:24 AM

All replies

  • Note that Newtonsoft.Json always serializes to a String. The string does not have any encoding, it is always Unicode internally in memory. You then feed that string to something else (maybe a FileStream, maybe a File.WriteAlltext, maybe some other code), and that is what applies the encoding. So, to know the encoding you need to look at what else you have in your code besides the Newtonsoft serializer. This additional code is the one that applies the encoding.
    • Marked as answer by E-John Friday, June 21, 2019 1:14 AM
    Thursday, June 20, 2019 6:36 AM
  • Dear All,

    I think I found it, if there is any wrong please correct me, thanks.

    It could be done by specifying the parameter "Encoding.UTF8" in the System.IO.File.WriteAllText method

                System.IO.File.WriteAllText(SaveFileName, json, Encoding.UTF8);

    and stored this string in one field of json file, something like "Encoding": "utf-8",


    Thus, other device can read this field and decode the string by correct way.



    Thanks and Best regards,

    E-John






    • Edited by E-John Thursday, June 20, 2019 7:41 AM
    • Marked as answer by E-John Thursday, June 20, 2019 8:06 AM
    Thursday, June 20, 2019 7:24 AM
  • Hi E-John,

    Thank you for posting here.

    I want to know if you have any question about it. If so, please free to let us know. If not, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 20, 2019 8:04 AM
  • Hi Jack,

    Understood.

    Thanks and Best regards,

    E-John

    Thursday, June 20, 2019 8:11 AM
  • Hi Alberto Poblacion,

    Thanks for your explanations.

    Best regards,

    E-John

    Friday, June 21, 2019 1:14 AM