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

PopulateObject won't throw Exception on invalid JSON #2924

Open
arnaudgout opened this issue Dec 20, 2023 · 0 comments
Open

PopulateObject won't throw Exception on invalid JSON #2924

arnaudgout opened this issue Dec 20, 2023 · 0 comments

Comments

@arnaudgout
Copy link

arnaudgout commented Dec 20, 2023

EDIT: adding CheckAdditionalContent = true in the settings make the PopulateObject throw for this case, but it's still confusing that both methods won't have the same default behavior

Source/destination types

class Test
{
    public int test;
}

Source/destination JSON

"{\"test\":1}gg"

Expected behavior

Should throw JsonReaderException

Actual behavior

Will deserialize the proper value. (Test.test == 1)

Steps to reproduce

// here is a test case that fails in the last step
[Test]
public void PopulateObjectInvalidJson()
{
    Assert.Throws(typeof(JsonReaderException), () =>
    {
        var o = JsonConvert.DeserializeObject<Test>("{\"test\":1}invalidtrailingchar");
    });   // will pass
    Assert.Throws(typeof(JsonReaderException), () =>
    {
        var o = new Test() { test = 0 };
        JsonConvert.PopulateObject("{\"test\":1}invalidtrailingchar", o);
    });   // will fail here
}

class Test
{
    public int test;
}
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

1 participant