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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:18:17+00:00 2026-05-30T10:18:17+00:00

I’m deserializing some properties to a Dictionary<string, object> . When I deserialize some json,

  • 0

I’m deserializing some properties to a Dictionary<string, object>.

When I deserialize some json, it populates the Dictionary with Int64 objects rather than Int32. I would like it to choose Int32 as the default well knowing that I could have javascript Numerics that would overflow on conversion. Throwing an exception in that case would be entirely acceptable.

Is there any way to achieve that? I’m hoping for some nice attributes or a convenient interface that could be implemented and added to the JsonSerializer. And I fear that I have to go deep down into the depths of Json.NET.

Basically I would like to have some way to control the known types for the objects so that I could get Int32‘s instead of Int64 and DateTimes instead of Strings.

  • 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-30T10:18:19+00:00Added an answer on May 30, 2026 at 10:18 am

    As far as I know, there is no built-in way to do that.

    There was an issue on this subject, but it has been closed.
    Some comments from the author on the issue:

    Json.NET by default reads integer values as Int64 because there is no way to know whether the value should be Int32 or Int64, and Int64 is less likely to overflow. For a typed property the deserializer knows to convert the Int64 to a Int32 but because your property is untyped you are getting an Int64. […] It is just the way Json.NET has to work.

    The easiest solution would of coure be to change the type to Dictionary<string, int>, but I suppose you are not only reading numerics and thus are stuck with object.

    Another option would be to either use Serialization Callbacks and manually convert those Int64s to Int32 or create your own Contract Resolver JsonConverter and directly control the (de-)serialization.


    Edit: I created a little example to be more specific.

    Here is a very basic converter that only works with your specifc Dictionary:

    public class Int32Converter : JsonConverter {
        public override bool CanConvert(Type objectType) {
            // may want to be less concrete here
            return objectType == typeof(Dictionary<string, object>);
        }
    
        public override bool CanWrite {
            // we only want to read (de-serialize)
            get { return false; }
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
            // again, very concrete
            Dictionary<string, object> result = new Dictionary<string, object>();
            reader.Read();
    
            while (reader.TokenType == JsonToken.PropertyName) {
                string propertyName = reader.Value as string;
                reader.Read();
    
                object value;
                if (reader.TokenType == JsonToken.Integer)
                    value = Convert.ToInt32(reader.Value);      // convert to Int32 instead of Int64
                else
                    value = serializer.Deserialize(reader);     // let the serializer handle all other cases
                result.Add(propertyName, value);
                reader.Read();
            }
    
            return result;
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
            // since CanWrite returns false, we don't need to implement this
            throw new NotImplementedException();
        }
    }
    

    You can either use attributes to decorate members with your converter or pass it as parameter to a (de-)serialize method. Here’s an example where I used an attribute:

        [JsonObject]
    public class MyObject {
        [JsonConverter(typeof(Int32Converter))]
        public Dictionary<string, object> Properties { get; set; }
    }
    

    And here’s the code I used to test the implementation:

    class Program {
        static void Main(string[] args) {
            MyObject test = new MyObject();
            test.Properties = new Dictionary<string, object>() { { "int", 15 }, { "string", "hi" }, { "number", 7 } };
            Print("Original:", test);
    
            string json = JsonConvert.SerializeObject(test);
            Console.WriteLine("JSON:\n{0}\n", json);
    
            MyObject parsed = JsonConvert.DeserializeObject<MyObject>(json);
            Print("Deserialized:", parsed);
        }
    
        private static void Print(string heading, MyObject obj) {
            Console.WriteLine(heading);
            foreach (var kvp in obj.Properties)
                Console.WriteLine("{0} = {1} of {2}", kvp.Key, kvp.Value, kvp.Value.GetType().Name);
            Console.WriteLine();
        }
    }
    

    Without the converter, the result would be:

    Deserialized:
    int = 15 of Int64
    string = hi of String
    number = 7 of Int64
    

    and with the converter it is:

    Deserialized:
    int = 15 of Int32
    string = hi of String
    number = 7 of Int32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.