Skip to content

Centralize and improve String literal handling #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions json_serializable/lib/src/generator_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ void $toJsonMapHelperName(String key, dynamic value) {

String _safeNameAccess(FieldElement field) {
var name = jsonKeyFor(field).name ?? field.name;
// TODO(kevmoo): JsonKey.name could also have quotes and other silly.
return name.contains(r'$') ? "r'$name'" : "'$name'";
return escapeDartString(name);
}

JsonSerializable _valueForAnnotation(ConstantReader annotation) =>
Expand Down
37 changes: 4 additions & 33 deletions json_serializable/lib/src/json_literal_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:source_gen/source_gen.dart';

import 'package:json_annotation/json_annotation.dart';

import 'utils.dart';

class JsonLiteralGenerator extends GeneratorForAnnotation<JsonLiteral> {
const JsonLiteralGenerator();

Expand Down Expand Up @@ -44,7 +46,7 @@ class JsonLiteralGenerator extends GeneratorForAnnotation<JsonLiteral> {
String _jsonLiteralAsDart(dynamic value, bool asConst) {
if (value == null) return 'null';

if (value is String) return _jsonStringAsDart(value);
if (value is String) return escapeDartString(value);

if (value is bool || value is num) return value.toString();

Expand Down Expand Up @@ -74,7 +76,7 @@ String _jsonMapAsDart(Map<String, dynamic> value, bool asConst) {
} else {
buffer.writeln(',');
}
buffer.write(_jsonStringAsDart(k));
buffer.write(escapeDartString(k));
buffer.write(':');
buffer.write(_jsonLiteralAsDart(v, asConst));
});
Expand All @@ -83,34 +85,3 @@ String _jsonMapAsDart(Map<String, dynamic> value, bool asConst) {

return buffer.toString();
}

String _jsonStringAsDart(String value) {
var containsSingleQuote = value.contains("'");
var contains$ = value.contains(r'$');

if (containsSingleQuote) {
if (value.contains('"')) {
// `value` contains both single and double quotes as well as `$`.
// The only safe way to wrap the content is to escape all of the
// problematic characters.
var string = value
.replaceAll('\$', '\\\$')
.replaceAll('"', '\\"')
.replaceAll("'", "\\'");
return "'$string'";
} else if (contains$) {
// `value` contains "'" and "$", but not '"'.
// Safely wrap it in a raw string within double-quotes.
return 'r"$value"';
}
return '"$value"';
} else if (contains$) {
// `value` contains "$", but no "'"
// wrap it in a raw string using single quotes
return "r'$value'";
}

// `value` contains no problematic characters - except for '"' maybe.
// Wrap it in standard single-quotes.
return "'$value'";
}
37 changes: 37 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,43 @@ import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'

import 'package:source_gen/source_gen.dart';

/// Returns a quoted String literal for [value] that can be used in generated
/// Dart code.
// TODO: still need handle triple singe/double quotes within `value`
String escapeDartString(String value) {
if (value.contains('\n')) {
return "r'''\n$value'''";
}

var containsDollar = value.contains(r'$');

if (value.contains("'")) {
if (value.contains('"')) {
// `value` contains both single and double quotes.
// The only safe way to wrap the content is to escape all of the
// problematic characters.
var string = value
.replaceAll(r'$', r'\$')
.replaceAll('"', r'\"')
.replaceAll("'", r"\'");
return "'$string'";
} else if (containsDollar) {
// `value` contains "'" and "$", but not '"'.
// Safely wrap it in a raw string within double-quotes.
return 'r"$value"';
}
return '"$value"';
} else if (containsDollar) {
// `value` contains "$", but no "'"
// wrap it in a raw string using single quotes
return "r'$value'";
}

// `value` contains no problematic characters - except for '"' maybe.
// Wrap it in standard single-quotes.
return "'$value'";
}

String commonNullPrefix(
bool nullable, String expression, String unsafeExpression) =>
nullable
Expand Down
1 change: 1 addition & 0 deletions json_serializable/test/test_files/data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
"\nnew lines are fun!\n",
"simple string",
"'string with single quotes'",
"\"string with double quotes\"",
Expand Down
8 changes: 8 additions & 0 deletions json_serializable/test/test_files/json_literal.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ part of 'json_literal.dart';
// **************************************************************************

final _$dataJsonLiteral = [
r'''

new lines are fun!
''',
'simple string',
"'string with single quotes'",
'"string with double quotes"',
Expand Down Expand Up @@ -38,6 +42,10 @@ final _$dataJsonLiteral = [
}
];
const _$asConstJsonLiteral = const [
r'''

new lines are fun!
''',
'simple string',
"'string with single quotes'",
'"string with double quotes"',
Expand Down