1

I previously asked a question here on SO about this issue, but now I observed that I did not investigate much about the issue, thus I wrongly concluded that the error was in the JSON library I was using (Newtonsoft), as specified in that previous question.

After testing with Microsoft's Web.Script.Serialization and exactly the same issue persists, I thus deem it fit to rather ask it as a separate SO question; so that the question will attract the appropriate audience, with the appropriate title.

Aside from the above clarification, the issue still remains the same, with more details below:

In my c# application. I have the following c# class to serialize:

class resultCommentsJsonMask
{
    public List<resultCommentAuthorityEntry> authorities { get; set; }
    public resultCommentTypes globalComments { get; set; }
    public List<string> commentTypes { get; set; }
}


public class resultCommentAuthorityEntry
{
    public string name { get; set; }
    public string description { get; set; }
    public string commentType { get; set; }
    public bool useGlobalSelectableComments { get; set; }
    public bool useGlobalAutomatedComments { get; set; }
    public resultCommentTypes comments { get; set; }
}

public class resultCommentTypes
{
    public string selectable { get; set; }
    public List<resultCommentBoundary> automated { get; set; }
}

public class resultCommentBoundary
{
    public decimal lowerRange { get; set; }
    public decimal upperRange { get; set; }
    public string comment { get; set; }
}

After creating an instance of resultCommentsJsonMask and filling it with data, I did JsonConvert.SerializeObject(instance_of_resultCommentsJsonMask)

However, on 2 other computers (my app users), all JSON keys were replaced with strange characters.

The only obvious differences between those computers and mine is that I am using a 32 bit Windows 7 PC while theirs is 64 bit Windows 10. Even more confusing now is the fact that I got another Windows 10 PC where everything works perfectly exactly like my own Win 7 computer.

I converted the app to a console app and logged the serialized JSON output on my PC to console, and I got the following, correct, JSON output:

enter image description here

However, the console output on those other (Windows 10) PCs had garbled text in form of question marks where JSON keys are meant to be (like "Authorities", "Name", etc. in my console output above), as shown below:

enter image description here

Also note that this issue persists even when I used Microsoft's Javascriptserializer JSON library instead of Newtonsoft, making me to want to conclude that it is a "library-agnostic" issue.

Edit

The relevant code used to generate the console output above:

var json = serializer.Serialize(commentsStore);
Console.WriteLine(string.Format("Setting store: {0}", json));
Damilola Olowookere
  • 2,253
  • 2
  • 23
  • 33
  • 3
    This looks like an encoding issue. Are you using the same encoding on the sending side that you are on the receiving side? You should probably be using UTF-8 rather than "Default" encoding. – Brian Rogers Mar 18 '19 at 14:49
  • I think your question will be much more approachable if you don't complicate the matter by bringing your PHP server into the fray. If you have a server that's producing wonky JSON, then you should focus on that, not on how another server is interpreting it. – JLRishe Mar 18 '19 at 14:53
  • @BrianRogers Does this encoding issue also apply to the Console output which doesn't involve any PHP or web server at all? (also note that the garbling only applies to the JSON keys. The JSON values are all correct, and both the JSON keys and values contain only ASCII characters) – Damilola Olowookere Mar 18 '19 at 14:53
  • @JLRishe I have edited as per your advise. Thanks – Damilola Olowookere Mar 18 '19 at 14:57
  • 1
    Please share complete example how you get these results. – Renatas M. Mar 18 '19 at 15:07
  • @Reniuz I have edited with more details. Thanks – Damilola Olowookere Mar 18 '19 at 15:14
  • I believe @BrianRogers is on the right track. Can you show us the encoding specified for the web client? – Todd Copeland Mar 18 '19 at 15:16
  • Sorry but this is not complete example. Please add code how you initialize serializer and sample object. I suggest to add code so we could only copy paste to get what you have. – Renatas M. Mar 18 '19 at 15:17
  • You should check the default encoding on the machines with the issue (`System.Text.Encoding.Default.EncodingName`). You might also try manually encoding the JSON as UTF8 and sending those bytes to your PHP server instead of a string. – JLRishe Mar 18 '19 at 15:18
  • @DamilolaOlowookere the console can't display non US-ASCII characters without setting the `Console.OutputEncoding` setting to `Encoding.UTF8`. The Windows 10 console works a LOT better in that respect compared to Windows 7. You still can't use it to check the output of JSON.NET when non-English characters are involved. Save the string to a file instead. – Panagiotis Kanavos Mar 18 '19 at 15:20
  • @DamilolaOlowookere there's no issue with Json.NET, nor any difference between computers in the way it works. What you see is an artifact of your testing method, ie printing to the console – Panagiotis Kanavos Mar 18 '19 at 15:22
  • @BrianRogers So how do I set the encoding of the serialized JSON? (either in Json.NET or Microsoft's JavascriptSerializer) – Damilola Olowookere Mar 18 '19 at 15:25
  • @DamilolaOlowookere You wouldn't set it in either of them. .NET strings don't have an encoding. As I already suggested, you can manually encode the string to UTF8 (using `var bytes = System.Text.Encoding.UTF8.GetBytes(json)`) – JLRishe Mar 18 '19 at 15:29
  • @PanagiotisKanavos OP's original question also indicated that the PHP server receiving the JSON also received it in an invalid form (I suggested that they remove that part of the question to avoid overcomplicating it), so this isn't just an issue with the console. – JLRishe Mar 18 '19 at 15:44
  • @JLRishe I knew I've seen something like this in the morning, when I told the OP to post the *PHP* code or use Fiddler to see what's *actually* being sent. As long as the OP tries to find nonexistent encoding bugs, it's impossible to diagnose the problem. WIthout *any* code, it's impossible to help – Panagiotis Kanavos Mar 18 '19 at 15:47
  • Can this question not be marked as duplicate of console encoding please? The problem generally relates to encoding. The initial problem, as outlined in the previous/linked question initially originated from a client-server communication with a remote PHP server. The console output was just a debug attempt. I will now take differences in machine encoding to consideration and then revert back here. Thanks @everyone – Damilola Olowookere Mar 18 '19 at 16:21

0 Answers0