Proposal: JSON string support in the Dart language

662 views
Skip to first unread message

James deBoer

unread,
Oct 15, 2013, 1:39:11 PM10/15/13
to mi...@dartlang.org
I have been hacking with Dart for a few months, mostly Angular.dart.  My apps are maturing: while focusing on processes I am writing Dart code generators.

Since we are the web platform, I deal with JSON often.  It would be very useful if the Dart language supported JSON so that I could write Dart programs like:

main() {
  String data = <cut-and-paste JSON from somewhere>;
  print(data);
}

That, of course, is the appeal of JSON in the Javascript world.  There is an awkward bug in the JSON spec (https://medium.com/joys-of-javascript/42a28471221d) which means that JSON is not actually Javascript.

But, Dart is a new language, so JSON *could* be a subset of Dart.  JSON is almost a double-quoted Dart string, except for Dart's '$' string interpolation.

Raw strings (r'string$') have string interpolation disabled. Yet, in practice, raw strings are useless since it is impossible to represent both single and double quotes in the same raw string.

There are two options for supporting JSON:

1. introduce a new string format j"json string"

2. change the spec for raw strings to allow escape characters.  This would break the spec, but it would make Dart strings much more useful.

Thoughts?

Specifically for option (2), are there developers that are using raw strings and depend
on them not being escaped?  I know, in Angular.dart, we use them in our test case names since we prepend a $ to some of our service names.


James deBoer

W. Brian Gourlie

unread,
Oct 15, 2013, 1:51:37 PM10/15/13
to mi...@dartlang.org
I'm confused by what you're proposing.  Are you really just proposing a JSON literal?

Brian
Message has been deleted

Lasse R.H. Nielsen

unread,
Oct 15, 2013, 2:33:22 PM10/15/13
to mi...@dartlang.org
You can use raw double-quoted multi-line strings:
  String data = r"""{"foo": ["This isn't a problem", " and ''' tripple-single quotes too!"]}""";
I don't think you can do anything in JSON that isn't accepted.

Raw strings are definitely not useless. I use them for, e.g., RegExp strings. They have their uses, but they can't be used for everything.
Sometimes the right tool for the job is a couple of adjacent string literals with different quotings or rawness.

/L


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Lasse R.H. Nielsen

unread,
Oct 15, 2013, 2:42:35 PM10/15/13
to mi...@dartlang.org
Oh, and for the record, JSON *is* a subset of Dart. If you drop the quotes, it will be valid list and map literals.

We can still grasp defeat from the jaws of victory and make U+2028 and U+2029 line terminators that are not allowed in string literals. It's already on the drawing board - stay posted :)

/L


On Tue, Oct 15, 2013 at 7:39 PM, James deBoer <deb...@google.com> wrote:

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

James deBoer

unread,
Oct 15, 2013, 3:14:29 PM10/15/13
to mi...@dartlang.org
Ah, I wasn't aware of raw multiline strings, which definitely solve the quotes-in-a-string problem.  But, since raw strings don't \-escape, they aren't JSON: the JSON string "\u0061" is "a", but r"""\u0061"""" is not.

List and map literals suffer from the same problem as strings.  e.g. ["$a"] is valid JSON but the Dart version would be ["\$a"].


Lasse R.H. Nielsen

unread,
Oct 16, 2013, 1:22:16 AM10/16/13
to mi...@dartlang.org
On Tue, Oct 15, 2013 at 9:14 PM, James deBoer <deb...@google.com> wrote:
Ah, I wasn't aware of raw multiline strings, which definitely solve the quotes-in-a-string problem.  But, since raw strings don't \-escape, they aren't JSON: the JSON string "\u0061" is "a", but r"""\u0061"""" is not.

Technically, the JSON string  "\u0061"  is a different JSON source code than  "a" , they just represent the same string value.

But yes, a lone double-quoted string is a problem. You can use multi-line single-quoted strings for that, if it doesn't contain a tripple-single-quote itself. 

All said and done, a generic string literal syntax can't contain its own delimiter without escapes, and then it can't contain its escape character without escapes either.
Dart has two ways to handle this: strings with escapes, but then you have to escape everything with meaning, and raw strings, but then you can't escape the delimiter, so you have to fiddle around with the delimiters if you have incompatible content.

 

List and map literals suffer from the same problem as strings.  e.g. ["$a"] is valid JSON but the Dart version would be ["\$a"].

Ack, yes, or [r"$a"]. So JSON is already not a subset of Dart.

dangli...@gmail.com

unread,
Oct 16, 2013, 3:30:59 AM10/16/13
to mi...@dartlang.org
>> My apps are maturing: while focusing on processes I am writing Dart code generators.

Can I ask you a question? What kind of generators you wrote?

Lasse R.H. Nielsen

unread,
Oct 16, 2013, 3:46:40 AM10/16/13
to mi...@dartlang.org
So, back to the original request: A way to delimit JSON source as a string literal without needing to escape anything.

I don't expect us to add another type of string literal.

The format  j"json string"  isn't really buying us much. Since the JSON source can contain double quotes, we would need to parse it, as JSON, in order to know which double-quote ends the string. That would mean putting a JSON parser into the language, only to always convert it to strings.

The double-quotes are not the only problem with using Dart strings directly. The string 
'["\u0000"]' isn't valid JSON. The escape is handled by Dart, so the string has five characters: [, ", 0x00, " and ]. The NUL character (or any literal control character) isn't valid in JSON string literals, it must be escaped. That means to make it valid JSON, you must do '["\\u0000"]' - or use a raw string.

I guess the full answer is: Don't copy any text blindly into any kind of string literal - eventually it will break.

Since JSON has almost arbitrary characters in strings, no choice of matched delimiters will always work. The raw double-quoted multi-line string actually works for anything that is *not* a single string literal (and then you can add a space after the close-quote).

I.e., wrap in  r"""  and  <space>""" and it should always be valid JSON content.

/L



On Tue, Oct 15, 2013 at 7:39 PM, James deBoer <deb...@google.com> wrote:

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
Reply all
Reply to author
Forward
0 new messages