Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

Support JSON5

Open Alxandr opened this issue 10 years ago • 18 comments

JSON5 is a proposed extension to JSON that brings ES5 enhancements to its syntax. It remains a strict subset of JavaScript, adds no new data types, and is a strict superset of existing JSON. - json5

Json5 basically adds support for comments, un-quoted keys, and trailing comas. In addition to:

  • Strings can be single-quoted.
  • Numbers can be hexadecimal (base 16).
  • Numbers can begin or end with a (leading or trailing) decimal point.
  • Numbers can include Infinity and -Infinity.
  • Numbers can begin with an explicit plus (+) sign.

I'm not saying that by default it should just be parsed, but if a option to the parser/deserializer could be provided that allowed these features, that would be really awesome :).

And given that it's a strict superset of existing JSON, this should not break anything, even if people were to run with the new flag turned on for old JSON files.

Alxandr avatar Jul 02 '14 06:07 Alxandr

After reading through most of your feature-list on http://james.newtonking.com/json, it would seem that a lot of the requested features are already present in Newtonsoft.Json. What you already support are (according to your own list):

  • Handles NaN, Infinity, -Infinity and undefined
  • Unquoted property names support
  • Supports reading and writing comments
  • Single or double quote JSON content

Alxandr avatar Jul 02 '14 07:07 Alxandr

I think the only things to do are:

  • Numbers can begin or end with a (leading or trailing) decimal point.
  • Numbers can begin with an explicit plus (+) sign.

JamesNK avatar Jul 02 '14 20:07 JamesNK

I've thought about this and if JSON5 catches on as a thing I'll do more to support it then.

JamesNK avatar Jul 06 '14 08:07 JamesNK

@JamesNK What's you stance on this now, two years later? Especially supporting comments make documenting JSON files alot easier.

marcselman avatar Dec 01 '16 19:12 marcselman

JSON5 hasn't caught on as a thing. I already support comments.

JamesNK avatar Dec 01 '16 21:12 JamesNK

Oh, I did not know that. It's not that easy to find actually. Thanks.

marcselman avatar Dec 01 '16 22:12 marcselman

I'm not sure how we're measuring "catching on as a thing" (legitimate question), but I just noticed that the json5 node package was downloaded 3.5 million times this week. The trend shows steady progress since the last comment here in December of 2016:

Trend: http://www.npmtrends.com/json5

Looks like it'll be 4.5M downloads per week soon.

stats: https://gist.github.com/anvaka/8e8fa57c7ee1350e3491

Might be nice to have it as a check-mark item.

kasajian avatar Jun 24 '18 07:06 kasajian

Pretty sure it's caught on as a thing!

Mardoxx avatar Jul 05 '18 11:07 Mardoxx

@JamesNK Came here to say it's definitely caught on as a thing! We're working on a project in Unity where there's different configuration files, and JSON5 is about the closest thing you can get to an actual object's representation in memory:

{
  VehicleInfo : {
    Name : 'Big Truck',
    Colors : [
      'Rusted Out',
    ],
    Flags : 0x7, // is_player | is_unlockable | is_heavy
    TopSpeed : 89.9,
    Horsepower : 457.925, // leave this alone, jeff!!
    Mass : 12500.0,
  }
}

As you would imagine, VehicleInfo would be a class containing those particular members with easy to deduce types.

I actually wrote another data storage format for exactly this purpose, which looks like this:

VehicleInfo : {
  Name = "Big Truck"
  // 'Colors' is a string list, so we use a string tuple instead of a generic array
  Colors = (
    "Rusted Out",
  )
  Flags = 0x7 // is_player | is_unlockable | is_heavy
  TopSpeed = 89.9f
  Horsepower = 457.925f // leave this alone, jeff!!
  Mass = 12500.0f
}

While serialization wasn't originally planned, it somehow ended up turning into a C-like JSON format...and the code is a mess and just doesn't make sense.

Instead of reinventing the wheel, JSON5 perfectly aligns with our needs. I see JSON used everywhere these days, and I'm sure a lot of people would happily use JSON5 without needing to download a different library, and needing only to make very few code changes to support it.

Tldr; JSON5 is amazing for game developers, please consider adding support!

Fireboyd78 avatar Jul 11 '18 23:07 Fireboyd78

@JamesNK I think you're underestimating the role this library plays in it "catching on as a thing". Support has to come first before people can start using it.

eyadmba avatar Aug 26 '22 18:08 eyadmba

Is there a PR for this? If so, we could fork it and make a new version available.

kasajian avatar Aug 26 '22 21:08 kasajian

Maybe promising to officially support JSON5 is too big of a step, but I'm wondering whether this issue could be at least reopened? From what I can see in this thread, the only valid reason for closing this was that it hadn't caught on, but at this point that is clearly not true anymore (besides the >60 million downloads/week it gets on NPM now, it is used in Chromium source code, Jetbrains IDEs support it natively, and Apple's JSON decoder also supports it).

Visne avatar Jan 14 '23 01:01 Visne

What are the JSON5 features that aren’t supported?

JamesNK avatar Jan 14 '23 02:01 JamesNK

Going over the summary provided on JSON5's website:

Objects

  • Object keys may be an ECMAScript 5.1 IdentifierName. ✔️
  • Objects may have a single trailing comma. ✔️

Arrays

  • Arrays may have a single trailing comma. ✔️

Strings

  • Strings may be single quoted. ✔️
  • Strings may span multiple lines by escaping new line characters. ❌
  • Strings may include character escapes. ❌

Numbers

  • Numbers may be hexadecimal. ✔️
  • Numbers may have a leading or trailing decimal point. ✔️
  • Numbers may be IEEE 754 positive infinity, negative infinity, and NaN. ✔️
  • Numbers may begin with an explicit plus sign. ❌

Comments

  • Single and multi-line comments are allowed. ✔️

White Space

  • Additional white space characters are allowed. ❌ (byte order mark isn't allowed, possibly others)

There might be something else in the spec, but it's quite short and from quickly scrolling through it it doesn't seem like the summary misses anything.

Visne avatar Jan 14 '23 03:01 Visne

Mostly right

I think these are supported today:

  • Strings may span multiple lines by escaping new line characters
  • Strings may include character escapes.

Not supported today:

  • Numbers may have a leading or trailing decimal point.

JamesNK avatar Jan 14 '23 05:01 JamesNK

Strings may span multiple lines by escaping new line characters

This should be valid according to the JSON5 example:

using Newtonsoft.Json;

Console.WriteLine(JsonConvert.DeserializeObject(@"
{
  lineBreaks: ""Look, Mom! \
No \\n's!"",
}
"));

however I get Unhandled exception. Newtonsoft.Json.JsonReaderException: Bad JSON escape sequence: \.

Strings may include character escapes.

According to the JSON5 spec, these should be allowed:

using Newtonsoft.Json;

Console.WriteLine(JsonConvert.DeserializeObject(@"
{
  ""a"": ""\v"",
  ""b"": ""\A\C\/\D\C"",
}
"));

but both generate exceptions.

Numbers may have a leading or trailing decimal point.

Seems to work just fine:

using Newtonsoft.Json;

Console.WriteLine(JsonConvert.DeserializeObject(@"
{
  ""a"": .5,
  ""b"": 5.
}
"));

prints

{
  "a": 0.5,
  "b": 5.0
}

Visne avatar Jan 18 '23 14:01 Visne

What's the state on this? I'd like to use json5 for a project of mine

Oliver-makes-code avatar Feb 08 '24 03:02 Oliver-makes-code