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 1008311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:45:09+00:00 2026-05-16T08:45:09+00:00

It seems impossible to (de-)serialize a generic dictionary using Json.NET out of the box.

  • 0

It seems impossible to (de-)serialize a generic dictionary using Json.NET out of the box.

class KeyClass
{
    public KeyClass(string p1, string p2) { prop1 = p1; prop2 = p2;}
    public string prop1 { get; set; }
    public string prop2 { get; set; }
}

class ValueClass
{
    public ValueClass(string pa, string pb) { propA = pa; propB = pb;}
    public string propA { get; set; }
    public string propB { get; set; }
}

...

Dictionary<KeyClass, ValueClass> d = new Dictionary<KeyClass, ValueClass>();
d.Add(new KeyClass("k1", "k2"), new ValueClass("v1", "v2"));
d.Add(new KeyClass("k3", "k4"), new ValueClass("v3", "v4"));
string json = JsonConvert.SerializeObject(d);

returns

{"ConsoleApplication1.KeyClass":{"propA":"v1","propB":"v2"},
 "ConsoleApplication1.KeyClass":{"propA":"v3","propB":"v4"}}

where I expected something like:

{{"prop1":"k1","prop2":"k2"}:{"propA":"v1","propB":"v2"},
 {"prop1":"k3","prop2":"k4"}:{"propA":"v3","propB":"v4"}}

So ToString() is called on the key (KeyClass). (an override that generates json doesn’t help: escapes the json and just doesn’t feel right).

Is it possible to write a CustomCreationConverter for this case?
Is this a hard limitation of json.NET?
If so, do other libraries support this?

  • 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-05-16T08:45:10+00:00Added an answer on May 16, 2026 at 8:45 am

    What you’re trying to do is not supported in JSON, it has little to do with JSON.NET: a “dictionary” (it’s called an object in JSON) must always have string keys. Just look at the JSON specs.

    What JSON.NET is doing here is a “right” way to handle a C# dictionary (another is to throw an error).

    Note that JavaScript (of which JSON is a very restricted subset) also does not allow your proposed syntax.

    One way to serialize a generic dictionary (or non-generic dictionaries with non-string keys) is by serializing it as a list of key-value-pairs, e.g.,

    [ { "key": {"prop1":"k1","prop2":"k2"}, "value": {"propA":"v1","propB":"v2"} },
      { "key": {"prop1":"k3","prop2":"k4"}, "value": {"propA":"v3","propB":"v4"} } ]
    

    How to that with JSON.NET is another matter, but if it’s possible you can try to expose the dictionary as an IEnumerable<KeyValuePair<X,Y>>.

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

Sidebar

Related Questions

It seems impossible to me to have a natural-id in a component. public class
Seems impossible to sleep a thread using boost::thread. Method sleep requires a system_time but
I'm using db2 version 9.7* and it seems impossible to make a NOT NULL
It seems impossible to define PUT when I use Html.BeginForm to submit a form
It seems impossible to use the webkitRequestFileSystem method from a local file without some
Monitoring my global exception logs this error seems to be impossible to remove no
This seems impossible directly from flash, but if there are any other suggestions, I'd
I'm very surprised that it seems impossible to upload more than a few megabytes
Situation: It seems impossible to store any objects in the CI_Session since, reading them
I have this classic problem that seems impossible to solve for me. I just

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.