JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

Support JSON5 #323

Open Alxandr opened 10 years ago

Alxandr commented 10 years ago

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:

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 commented 10 years ago

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):

JamesNK commented 10 years ago

I think the only things to do are:

JamesNK commented 10 years ago

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

marcselman commented 7 years ago

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

JamesNK commented 7 years ago

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

marcselman commented 7 years ago

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

kasajian commented 6 years ago

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.

Mardoxx commented 6 years ago

Pretty sure it's caught on as a thing!

Fireboyd78 commented 6 years ago

@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!

eyadmba commented 2 years ago

@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.

kasajian commented 2 years ago

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

Visne commented 1 year ago

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).

JamesNK commented 1 year ago

What are the JSON5 features that aren’t supported?

Visne commented 1 year ago

Going over the summary provided on JSON5's website:

Objects

Arrays

Strings

Numbers

Comments

White Space

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.

JamesNK commented 1 year ago

Mostly right

I think these are supported today:

Not supported today:

Visne commented 1 year ago

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
}
Oliver-makes-code commented 7 months ago

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