C# Newtonsoft JSON library outputs different wrong

2020-04-16 04:20发布

I have no clue whatsoever on how to resolve this. All I can figure out as a way of starting to resolve this is that my dev machine where things work perfectly is a 32 bit and the other tested computers are 64 bit (incidentally there are no other 32 bit machines around me to test with). Regardless, this is a strange scenario.

Now to the issue:

I am using Newtonsoft JSON library in my c# application. I have the following c# class to serialize:

class sync_object
    {

        //newtonJson give bad keys to these object names on 64bit systems

        public Dictionary<string, List<Dictionary<string, string>>> table_records { get; set; }
        public Dictionary<string, List<string>> class_list_with_subjects_offered { get; set; }
        public Dictionary<string, List<string>> class_list_with_scoreheads_offered { get; set; }
        public List<string> class_groups_ordered { get; set; }
    }

After creating an instance of sync_object and filling it with data, I did JsonConvert.SerializeObject(instance_of_sync_object) and send to my server. Then when I log the JSON received at server side (Laravel PHP), I get a perfect JSON dumped in PHP like below:

array ( 'table_records' => array ( 'personal_db' => array (...) ) , 'class_groups' => array(...) )

However, on 2 other computers (my app users), I get the same JSON structure like above, but the only difference is that all first-level JSON keys (like 'table_groups' and 'class_groups_ordered' in the above) gets replaced with the following strange characters (you may want to use your cursor to select it to observe that it contains a lot of non-printable chars):

'‮‫‫‮‮‌​‎​‭‎‏‮‎​‎‪‭‪​‍‮‬‌‏‬‮' => 

As I mentioned earlier, the only obvious difference between those computers and mine is that mine is a 32 bit PC while theirs is 64 bit.

Also, as a way of providing more information, since the JSON dump was done in my Laravel backend with Monolog, I get a peek at the DOM inspector in Chrome, as annotated below:

My normal JSON: enter image description here

The weird JSON from the other guys: enter image description here

The same Newtonsoft Json library is running on all these machines.

标签: c# json.net
1条回答
够拽才男人
2楼-- · 2020-04-16 04:46

Instinctively, I changed all my snake_case object property names to PascalCase and the nightmare was over!

(Note: I do not find anything on Newtonsoft JSON documentation that suggests that the case of property names matter in data serialization)

查看更多
登录 后发表回答