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

Trailing commas #802

Open
Shahroz16 opened this issue Jan 31, 2019 · 10 comments · May be fixed by #1688
Open

Trailing commas #802

Shahroz16 opened this issue Jan 31, 2019 · 10 comments · May be fixed by #1688
Milestone

Comments

@Shahroz16
Copy link

Shahroz16 commented Jan 31, 2019

Thank you for a very helpful library. Is there any way to get past the trailing comma issue with moshi?

Sample json
{ "name": "Khan", "greeting": "Hello, how are you", }

If you can see there is an extra comma and moshi rightly sends out an exception.
com.squareup.moshi.JsonEncodingException: Expected name at path $.greeting

Looking for any workaround for this issue. TIA

@ZacSweers
Copy link
Collaborator

I think that's an issue whatever service is sending you json right?

@NightlyNexus
Copy link
Contributor

maybe we should make lenient readers read this?

@Shahroz16
Copy link
Author

@ZacSweers trailing commas are usually not allowed in Json, but since they are allowed in ECMA5 it would be really helpful if we have a feature of allowing it.

something along this Jackson trailing commas support

@ZacSweers
Copy link
Collaborator

ECMA5 is 10 years old, is that still relevant today?

@Shahroz16
Copy link
Author

Shahroz16 commented Feb 21, 2019 via email

@JakeWharton
Copy link
Member

I agree that leniency could handle this if it's easy but it should remain invalid by default.

@swankjesse
Copy link
Member

Lenient mode handles this. Only problem is we treat the comma as an absent null like org.json.. This is probably not what you want.

@swankjesse swankjesse added this to the Icebox milestone Nov 17, 2019
@killvetrov
Copy link

So lenient mode should handle trailing commas now (in v1.9.3) or by Icebox milestone? I'm trying to parse this JSON:

{
    "id": 1,
    "watermark": {
        "x": 772,
        "y": 1130,
        "width": 221,
        "height": 70,
    }
}

Moshi throws JsonEncodingException even in lenient mode. I tried various ways: calling lenient() on adapter, making a JsonReader instance with setLenient(true) ending up with the same result. The thing is I don't even need watermark object, I only care about some other fields (I cut them out for brevity, minimum reproducible example). Does it mean that lenient mode only allows commas in arrays, but not in objects?

Also, what is best practice workaround for this (considering there is no control over JSON source)? Modifying adapter, or sanitizing JSON before reading (regexp)?

@swankjesse
Copy link
Member

@killvetrov we’re strict about these currently, even in lenient mode. We should fix this.

@mipo256 mipo256 linked a pull request Jun 4, 2023 that will close this issue
@Fintasys
Copy link

Fintasys commented May 8, 2024

There is an PR since last year, why isn't it being reviewed? This issue is quite annoying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants