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

  • Home
  • SEARCH
  • 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 8810475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:07:36+00:00 2026-06-14T03:07:36+00:00

At the project I am working on, we started to use the Json.Net library.

  • 0

At the project I am working on, we started to use the Json.Net library.

However, I just found out that json.net is ‘loose’ on string type.

Here’s an example:

The DTO class

[JsonObject]
public class DTO
{
    [JsonProperty]
    public string type;
}

The deserialization

byte[] rawBody = GetBytes(@"{""type"":true}");
using (MemoryStream ms = new MemoryStream(rawBody))
{
    using (StreamReader sr = new StreamReader(ms))
    {
        var serializer = new JsonSerializer();

        return serializer.Deserialize(sr, typeof(DTO));
     }
 }

This will deserialize the ‘type’ attribute as “True”. However, I would expect it to fail and throw an exception as there’s a type mismatch.
It does the same if I replace true by 1 in the json. The property ‘type’ value will be “1”.

questions:

  1. Is there a way to enforce strict serialization?

  2. Is there other types than string that have implicit conversion like what we see here?

Thank you.

JF

  • 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-14T03:07:39+00:00Added an answer on June 14, 2026 at 3:07 am

    I came up with a workaround.

    Although it works, I don’t know if it is the good way to solve my ‘problem’.

    I used converters to convert from

    Here’s what I did:

    [JsonObject]
    public class DTO
    {
        [JsonProperty]
        public string type;
    }
    

    The custom converter:

    class JsonStrictConverter<T> : JsonConverter
        {
            public JsonToken[] TokenTypes { get; set; }
    
            public JsonStrictConverter(params JsonToken[] tokenTypes)
            {
                TokenTypes = tokenTypes;
            }
    
            public override bool CanConvert(Type objectType)
            {
                return objectType == typeof(T);
            }
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                if (reader.TokenType == JsonToken.Null)
                {
                    if (objectType.IsValueType)
                    {
                        return Activator.CreateInstance(objectType);
                    }
                    return null;
                }
    
                var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
                return (T)converter.ConvertFromString(reader.Value.ToString());
            }
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                throw new NotImplementedException("The converter '" + this.GetType().Name + "' is not intended to be used when serializing.");
            }
            public override bool CanWrite { get { return false; } }
        }
    

    The deserialization:

    XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
    bodyReader.ReadStartElement("Binary");
    byte[] rawBody = bodyReader.ReadContentAsBase64();
    using (MemoryStream ms = new MemoryStream(rawBody))
    {
        using (StreamReader sr = new StreamReader(ms))
        {
            var serializer = new JsonSerializer();
            /* These converter are present to enforce strict data type in the json. */ 
            /* by default, newtonsoft can serialize Numbers as strings, strings as boolean, etc.... */
            serializer.Converters.Add(new JsonStrictConverter<string>(JsonToken.String));
            serializer.Converters.Add(new JsonStrictConverter<bool>(JsonToken.Boolean));
            serializer.Converters.Add(new JsonStrictConverter<short>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<ushort>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<int>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<uint>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<long>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<ulong>(JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<float>(JsonToken.Float, JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<double>(JsonToken.Float, JsonToken.Integer));
            serializer.Converters.Add(new JsonStrictConverter<decimal>(JsonToken.Float, JsonToken.Integer));
    
            return serializer.Deserialize(sr, typeof(DTO));                    
        }
    }
    

    Is there any type missing in this approach?

    Does any one around have a better solution?

    Thank you.

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

Sidebar

Related Questions

I just started working on a project that will run on google app engine
In the project I'm working on, we've just started to use Flex 4. We
I've just started working on a project that requires me to do lots of
I just started working on my first Visual Studio project, and I imported all
I've started working on a little ruby project that will have sample implementations of
I currently started working on a maven web-app project that needs to be launched
I've just started working on an existing CakePHP project. I will be developing a
I've started working on some enhancements to an ASP.NET web forms e-commerce project, one
I've only just started to use Git and think it is wonderful, however I'm
I have a project that I have recently started working on seriously but had

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.