Skip to content
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

CDL.toJSONArray(String csvString) won't work with Windows style line breaks (CR LF) #320

Closed
valdisa opened this issue Feb 17, 2017 · 4 comments

Comments

@valdisa
Copy link

valdisa commented Feb 17, 2017

Discovered this when using json-20090211.jar and reading a csv file created and saved in Windows 7.

Can be reproduced with an actual CSV file that contains CR LF line breaks or a crafted test string.

Example:

String csv = "label1,label2,code1\r\n" +
"name,surname,12345\r\n" +
"name1,surname1,12346\r\n" +
"name2,surname2,12347";

JSONArray json= CDL.toJSONArray(csvAsString);

In the above example the 'json' object is going to be null.
The current workaround for me is calling csvAsString.replace("\r\n", "\n") before I feed the string into CDL.

Let me know if you need more info.

Thanks,
Valdis

@stleary
Copy link
Owner

stleary commented Feb 18, 2017

It would be reasonable for CDL.java to support Windows line breaks.

@stleary
Copy link
Owner

stleary commented Feb 27, 2017

Please provide a pull request if you still want this change to be in the code.

@apolo92
Copy link

apolo92 commented Mar 1, 2017

CDL export to CSV format need field1;field2;field2;.... but in CDL.rowToString() is "," harcoded parser

public static String rowToString(JSONArray ja) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < ja.length(); i += 1) {
        if (i > 0) {
            sb.append(',');
        }
        Object object = ja.opt(i);
        if (object != null) {
            String string = object.toString();
            if (string.length() > 0 && (string.indexOf(',') >= 0 ||
                    string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
                    string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
                sb.append('"');
                int length = string.length();
                for (int j = 0; j < length; j += 1) {
                    char c = string.charAt(j);
                    if (c >= ' ' && c != '"') {
                        sb.append(c);
                    }
                }
                sb.append('"');
            } else {
                sb.append(string);
            }
        }
    }
    sb.append('\n');
    return sb.toString();
}

change this "," with a parameter in this function ?

@stleary
Copy link
Owner

stleary commented Jun 8, 2017

No objections if someone wants to try a solution. If so, please post a pull request.

@stleary stleary closed this as completed Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants