shamblett / xml2json

XML to JSON conversion for Dart
MIT License
54 stars 19 forks source link

"\" in xml should be escaped #30

Closed simonkimi closed 3 years ago

simonkimi commented 3 years ago

Hello Steve Hamblett In this test:

import 'package:test/test.dart';
import 'package:xml2json/xml2json.dart';

void main(List<String> arguments) async {
  test('\\ should be escaped', () {
    var xml = r'<post tag="\m/"/>';
    final xml2json = Xml2Json()..parse(xml);
    final data = xml2json.toGData();

    final expected = r'{"post": {"tag": "\\m/"}}';
    expect(data, equals(expected));
  });
}

Failed here

00:00 +0 -1: \ should be escaped [E]

  Expected: '{"post": {"tag": "\\\\m/"}}'
    Actual: '{"post": {"tag": "\\m/"}}'
     Which: is different.
            Expected: ... "tag": "\\\\m/"}}
              Actual: ... "tag": "\\m/"}}
                                    ^
             Differ at offset 20

I expected that \m/ in xml should be escaped in the json output as \\\\m/, but got \\m/, So a subsequent jsonDecode() will fail at

  FormatException: Unrecognized string escape (at character 21)
  {"post": {"tag": "\m/"}}
                      ^

Running on xml2json: ^4.4.0

omarkammoun12 commented 3 years ago

same issue for me ...

shamblett commented 3 years ago

OK, but you can't use

r''<post tag="\\m/"/>';

as your input test string, this package has its data passed to it in Dart string form from the xml package, not as a raw string, if you supply the string not in its raw format -

<post tag="\m/"/>

You get '{"post": {"tag": "m/"}}' which is valid json, if you double escape it -

<post tag="\\m/"/>

you get '{"post": {"tag": "\m/"}}' which is also valid json.

I'm not sure what you want this package to do here if it can't be supplied with the raw string from the xml package.

simonkimi commented 3 years ago

The XML I tested came from https://safebooru.org/index.php?page=dapi&s=post&q=index&tags=%20m/ Please try the following code:

import 'dart:convert';
import 'dart:io';

import 'package:xml2json/xml2json.dart';

void main() async {
  final uri = Uri.parse('https://safebooru.org/index.php?page=dapi&s=post&q=index&tags=%20m/');
  final httpClient = HttpClient();
  final req = await httpClient.getUrl(uri);
  final rsp = await req.close();
  final xml = await rsp.transform(utf8.decoder).join();

  final xml2json = Xml2Json()..parse(xml);
  final data = xml2json.toGData();
  jsonDecode(data);
}

Failed here

Unhandled exception:
FormatException: Unrecognized string escape (at character 471)
...0e.jpg", "rating": "s", "tags": " \m/ blush breasts erect_nipples finger...
                                       ^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
#1      _ChunkedJsonParser.parseStringEscape (dart:convert-patch/convert_patch.dart:1184:9)
#2      _ChunkedJsonParser.parseStringToBuffer (dart:convert-patch/convert_patch.dart:1123:18)
#3      _ChunkedJsonParser.parseString (dart:convert-patch/convert_patch.dart:1038:16)
#4      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:859:22)
#5      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
#6      JsonDecoder.convert (dart:convert/json.dart:505:36)
#7      JsonCodec.decode (dart:convert/json.dart:156:41)
#8      jsonDecode (dart:convert/json.dart:96:10)
#9      main (file:///F:/dart/test/main.dart:14:3)
<asynchronous suspension>
#10     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
shamblett commented 3 years ago

Thanks for the test, this should now be fixed and we shouldn't now be generating invalid json, package released at version 5.0.0