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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:58:30+00:00 2026-06-12T14:58:30+00:00

Most Json parsers don’t serialize NaN, because in Javascript, NaN is not a constant.

  • 0

Most Json parsers don’t serialize NaN, because in Javascript, NaN is not a constant. Json.Net, however, does serialize NaN values into NaN, which means it outputs invalid Json; attempting to deserialize this Json will fail with most parsers. (We’re deserializing in WebKit.)

We have hacked the Json.Net code to output null values when passed NaN, but this seems like a poor solution. Douglas Crockford (once) recommended using nulls in place of NaNs:

http://www.json.org/json.ppt (Look at slide 16)

Clearly this won’t work in all cases, but it would be ok for our purposes. We’d just rather not have to modify the source code of Json.Net. Does anyone know how to use Json.Net to convert NaN inputs into null outputs?

  • 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-12T14:58:32+00:00Added an answer on June 12, 2026 at 2:58 pm

    The author advises us to “Write a JsonConverter for float/double to make NaN safe if this is important for you,” so that’s what you can do:

    class LawAbidingFloatConverter : JsonConverter {
        public override bool CanRead
        {
            get
            {
                return false;
            }
        }
        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var val = value as double? ?? (double?) (value as float?);
            if (val == null || Double.IsNaN((double)val) || Double.IsInfinity((double)val))
            {
                writer.WriteNull();
                return;
            }
            writer.WriteValue((double)val);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(double) || objectType == typeof(float);
        }
    }
    

    and then use it:

    var settings = new JsonSerializerSettings();
    var floatConverter = new LawAbidingFloatConverter();
    settings.Converters.Add(floatConverter);
    var myConverter = new JsonNetSerializer(settings);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does python support random json serialization? I get this error: Traceback (most recent call
When using: var dataToSave = ko.toJSON(myViewModel); .. is it possible to not serialize values
I'm using .NET's JavascriptSerializer to deserialize JSON into runtime objects and for the most
In most JSON serializers/deserializers, the key part in a javascript dictionary/hash array is written
Does Twitter's streaming API (stream.twitter.com/1/statuses/sample.json) give the most recent tweets? For example, current new
The app I'm working on responds to most requests with JSON objects or collections
How do I transform this JSON object: context:{#text:[Most Visited Pages , Hall Residents Advanced
What is the most efficient way to compare two JSON-formatted objects data in node.js?
I'm making pretty heavy use of JSON parsing in an app I'm writing. Most
I need to start a project that will be json-API-centric, which means that most

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.