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

Seems impossible to sleep a thread using boost::thread. Method sleep requires a system_time but
I ran to this problem hard, it seems impossible to render. How can one
I wish to set some properties in MyFilter with constructor injection but it seems
I need to delete a trigger in SQL Server. Seems like it should be
I have an XML Schema element like this: <xs:element type=xs:string name=IsActive minOccurs=0> </xs:element> I'm
Goal When the user clicks an element (with class="menu" ), the default action should
Is there something like a class that might be used to store Files and
I'm toying around with MacFUSE, and it's pretty neat. (I'm using the Objective-C API.)
This post is somewhat similar to this other post: How can I make Ruby's
I realize this is very much of a long shot, but... In shell scripts

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.