Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:11:37+00:00 2026-06-01T15:11:37+00:00

I’m writing some code that will serialize a C# object to JSON, send it

  • 0

I’m writing some code that will serialize a C# object to JSON, send it over the wire and deserialize the JSON to a Python object.

The reverse will also be done, i.e. serialize a Python object to JSON, send it over the wire and deserialize the JSON to a C# object.

On the C# side, I use the ServiceStack JSON libraries. In Python, I’m using the built-in json libraries. The C# library can be changed easily if necessary, the Python one far less so.


// C#
var serializer = new JsonSerializer();
var json = serializer.SerializeToString(request);

// Python
// JsonCodec inherits from JSONEncoder and hooks to 'default' and just 
// returns obj.__dict__ where obj is the object to be serialized
actualSerializedResponse = JsonCodec().encode(response)

I’ve written a unit test in C# to verify that the ServiceStack serialized JSON is as expected by the Python side. In the test, an instance of Foo is created, populated with some hardcoded values, then serialized. To ensure validity, I compare the serialized JSON to some JSON saved in a file, where the contents of the file represent what the Python side expects.

Similarly, there’s a unit test in Python to verify that the built-in json library’s serialized JSON is as expected by the C# side. Again, to ensure validity, I compare the actual serialized JSON to some JSON saved in a file.

In both cases, the fact that I’m comparing the serialized JSON to some JSON saved in a file implies that the order in which properties are serialized to JSON must be consistent every time and every where the tests are run.

My questions:

  • In the C# unit test, it seems that the order of the properties in the JSON match the order in which the properties were defined in the C# class whose instance is being serialized. Can this be relied on?

  • In the Python unit test, the order of the properties is consistent but arbitrary. This makes sense since it relies on dict and the Python dictionaries are unordered. But can this be relied every time/where?

  • Is there a better way to do all this?

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:11:38+00:00Added an answer on June 1, 2026 at 3:11 pm

    Regarding the Python side: you can pass the JSON decoder an option to use an “ordered dictionary” (requires python 2.7 however):

    from http://docs.python.org/library/json.html#json.load:

    object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.